调用WriteableBitmap.WritePixels方法

Gil*_*man 11 c# wpf bitmap

我正在尝试调用WriteableBitmap.WritePixels方法,但我似乎无法理解参数.MSDN文章非常沉闷(或者说我说... null?),我无法理解如何使用该方法.

编辑: 我试图修改本文中的代码:http: //msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap%28v=VS.90%29.aspx

        PixelFormat pf = PixelFormats.Rgba128Float;
        WriteableBitmap wb = new WriteableBitmap(width * 5, height * 5, 100, 100, pf, new BitmapPalette(new List<Color> { Color.FromArgb(255, 255, 0, 0) }));
        byte[] ColorData = { 0, 0, 0, 0 };
        wb.WritePixels(new Int32Rect(0, 0, 1, 1), ColorData, 4, 0);
        Background.Source = wb;
Run Code Online (Sandbox Code Playgroud)

在最后一行之前的行中,调试器声称缓冲区(ColorData)大小不足.

Edit2: 我再试一次:

        void RefreshGraphics()
    {
        PixelFormat pf = PixelFormats.Pbgra32;
        WriteableBitmap wb = new WriteableBitmap(width * 5, height * 5, 100, 100, pf, new BitmapPalette(new List<Color> { Color.FromArgb(255, 255, 0, 0) }));
        byte[] ColorData = new byte[1000];
        byte t = 0;
        for (int i = 0; i < 1000; i++)
            ColorData[i] = t++;
        wb.WritePixels(new Int32Rect(0, 0, 10, 10), ColorData, 10, 0);
        Background.Source = wb;
    }
Run Code Online (Sandbox Code Playgroud)

现在"价值不在预期的范围内".stackTrace不知道哪一个......

编辑3: 解决了问题!不知道如何或为什么,但代码工作(我害怕改变它......)

Jac*_*obJ 7

本文(针对.NET 3)在示例中有一些注释,这些注释有助于解释不同参数是什么(像"stride"这样的概念)以及如何计算它们.滚动到实际代码示例所在的底部.我不确定为什么他们将这些评论从.NET 4版本中删除.

编辑:您能与WritePixels分享您的目标吗?如果您正在尝试绘制图像,形状等,我通常使用DrawingContext写入位图,然后将其渲染为DrawingVisual.


tug*_*dum 7

这是我理解它的方式:

  • System.Windows.Int32Rect sourceRect:最明显的参数.表示您尝试写入的位图子集的位置,宽度和高度.
  • System.Array pixels:一维或二维字节数组.您在位图构造函数中使用的像素格式将确定每个像素使用的字节条目数.
  • System.Int32 stride:定义每行使用的像素数组条目的数量(宽度)
  • System.Int32 offset:输入像素数组中的偏移量,如果要将数组重用于多个WritePixels调用.


Ern*_*rno 3

检查高度和宽度的值。也许字节数组根本不够大!