【问题标题】:WriteableBitmap Doesn't change pixel color in wpfWriteableBitmap 不会改变 wpf 中的像素颜色
【发布时间】:2013-07-20 21:57:17
【问题描述】:

我使用 WriteableBitmap 在 Wpf 中设置像素。但是当我使用 writePixels 方法来更改像素的颜色时,它会将颜色更改为黑色:(。这是我的代码快照:

            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;

我在谷歌上搜索太多了。但我找不到有关此问题的任何来源。

【问题讨论】:

    标签: c# wpf image-processing


    【解决方案1】:

    它看起来是黑色的,因为您正在重写图像的全部内容,而不仅仅是几个像素。您的 pixels 数组被声明为整个图像的大小,但您只设置了颜色数据的前四个字节中的两个。因此,当您调用WritePixels 时,它会使用提供者颜色数据重新绘制整个图像,该数据几乎全为 0。

    要更正此问题,只需将 pixels 数组声明为您要写入的像素的大小。

    【讨论】:

    • 亲爱的。情爱的我知道。整个图像不是我的问题。我的问题是像素的颜色。我把颜色改成红色。但它把它改成黑色。它的问题。 tnx 4 你的注意。当我使用方法创建位图时,它工作正常。但是在使用图像中的位图后,它会改变它的行为。并将像素更改为黑色。阿尔法工作正常。但颜色不会变成任何颜色,除了黑色:(
    • 如果我理解正确,那么您创建的位图的像素格式可能与您加载的图像的像素格式不同。例如,我使用您的 SO 头像图像作为您发布的代码的测试,左上角的像素更改为蓝色。其像素数据被写入为 BGRA。因此,如果您尝试将像素更改为红色,您的像素数据将为 0 0 255 0。
    • 是的,这是真的。它的工作正确。但是当我使用图像源作为位图时,问题就出现了。最后我找到了解决方案。 tnx 4 你的帮助。
    【解决方案2】:

    Finllay 我找到了解决方案:

                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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-23
      • 2013-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多