C# WriteableBitmap不';不能在wpf中更改像素颜色
我使用WriteableBitmap在Wpf中设置像素。但当我使用writePixels方法更改像素的颜色时,它将颜色更改为黑色:(.这是我的代码快照:C# WriteableBitmap不';不能在wpf中更改像素颜色,c#,wpf,image-processing,C#,Wpf,Image Processing,我使用WriteableBitmap在Wpf中设置像素。但当我使用writePixels方法更改像素的颜色时,它将颜色更改为黑色:(.这是我的代码快照: ofdOpen.ShowDialog(); BitmapImage img = new BitmapImage(new Uri(ofdOpen.FileName)); WriteableBitmap wbmap = new WriteableBitm
ofdOpen.ShowDialog();
BitmapImage img = new BitmapImage(new Uri(ofdOpen.FileName));
WriteableBitmap wbmap = new
WriteableBitmap(img);
byte[] pixels = new byte[
wbmap.PixelHeight*wbmap.PixelWidth*
wbmap.Format.BitsPerPixel/8];
pixels[0] =255;
pixels[1] = 0;
pixels[2] = 0;
pixels[3] = 255;
wbmap.WritePixels(
new Int32Rect(0, 0,
wbmap.PixelWidth, wbmap.PixelHeight),
pixels,
wbmap.PixelWidth * wbmap.
Format.BitsPerPixel / 8, 0);
image1.Source = wbmap;
我在谷歌上搜索得太多了。但是我找不到关于这个问题的任何来源。
它显示为黑色是因为您正在重写图像的全部内容,而不仅仅是几个像素。您的
像素数组声明为整个图像的大小,但您只设置了颜色数据的前四个字节中的两个。因此,当您调用WritePixels
时,它将使用提供程序颜色数据,几乎都是0
要更正此问题,只需将像素
数组声明为要写入的像素大小。最终我找到了解决方案:
BitmapImage originalIamge = new BitmapImage();
originalIamge.BeginInit();
originalIamge.UriSource = new Uri(ofdOpen.FileName);
originalIamge.EndInit();
BitmapSource bitmapSource = new FormatConvertedBitmap(originalIamge, PixelFormats.Bgr32, null, 0);
wbmap = new WriteableBitmap(bitmapSource);
h = wbmap.PixelHeight;
w = wbmap.PixelWidth;
pixelData = new int[w * h];
widthInByte = 4 * w;
wbmap.CopyPixels(pixelData, widthInByte, 0);
image1.Source = wbmap;
Point absoluteScreenPos = PointToScreen(Mouse.GetPosition((IInputElement)sender));
Image img = new Image();
BitmapImage point = new BitmapImage(
new Uri("Images/Dott.png",
UriKind.Relative));
img.Source = point;
var rt = ((UIElement)image1).RenderTransform;
var offsetX = rt.Value.OffsetX;
var offsetY = rt.Value.OffsetY;
int newX = (int)Mouse.GetPosition((IInputElement)sender).X;
int newY = (int)Mouse.GetPosition((IInputElement)sender).Y;
for (int i = 0; i < 400; i++)
{
pixelData[i] = 0x000000ff;
}
wbmap.WritePixels(new Int32Rect(newX,newY,10,10), pixelData, widthInByte, 0);
image1.Source = wbmap;
BitmapImage originalIamge=new BitmapImage();
originalIamge.BeginInit();
originalIamge.UriSource=新Uri(ofdOpen.FileName);
originalIamge.EndInit();
BitmapSource BitmapSource=新格式转换位图(originalIamge,PixelFormats.Bgr32,null,0);
wbmap=新的可写位图(位图源);
h=wbmap.PixelHeight;
w=wbmap.PixelWidth;
pixelData=新整数[w*h];
宽度单位=4*w;
wbmap.CopyPixels(像素数据,宽度单位,0);
image1.Source=wbmap;
Point absoluteScreenPos=指向屏幕(Mouse.GetPosition((IInputElement)sender));
图像img=新图像();
BitmapImage point=新的BitmapImage(
新Uri(“Images/Dott.png”,
乌里金(亲属);
img.震源=点;
var rt=((UIElement)image1).RenderTransform;
var offsetX=rt.Value.offsetX;
var offsetY=rt.Value.offsetY;
intnewx=(int)Mouse.GetPosition((IInputElement)sender.X;
int newY=(int)Mouse.GetPosition((IInputElement)sender.Y;
对于(int i=0;i<400;i++)
{
pixelData[i]=0x000000ff;
}
WritePixels(新的Int32Rect(newX,newY,10,10),pixelData,widthInByte,0);
image1.Source=wbmap;
Tnx亲爱的。我知道亲爱的。整个图像不是我的问题。我的问题是像素的颜色。我将颜色更改为红色。但它将其更改为黑色。我的问题。Tnx 4您注意。当我使用方法创建位图时,它工作正常。但使用图像中的位图后,它的行为会发生变化。并将像素更改为黑色。Alpha工作正常。但是颜色除黑色外,不会更改为任何颜色:(如果我理解正确,那么您创建的位图的像素格式可能与您加载的图像的像素格式不同。例如,我使用您发布的代码测试了您的SO化身图像,左上角的像素变为蓝色。其像素数据写为BGRA。因此,如果您试图更改pixel变为红色,像素数据为0 0 255 0。是的,这是正确的。工作正常。但当我使用图像源作为位图时,出现了问题。最后我找到了解决方案。tnx 4 ur帮助。