调用 WriteableBitmap.WritePixels 方法
我正在尝试调用 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;
在最后一行之前的行中,调试器声明缓冲区 (ColorData) 大小不是充足的。
编辑2: 我再次尝试:
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;
}
现在出现“值不在预期范围内”。 stackTrace 无法告诉您是哪一个...
编辑 3: 问题解决了! 不知道如何或为什么,但代码可以工作(而且我害怕改变它......)
I'm trying to call the WriteableBitmap.WritePixels method, but I can't seem to understand the parameters.
The MSDN article is very dull (Or shuold I say... null?), and I couldn't understand how to use the method.
Edit:
I tried to modify a code from this article:
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;
In the line before the last line the debugger claims that the buffer (ColorData) size is not sufficient.
Edit2:
I tried again:
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;
}
Now a "Value is is not in the expected range."
The stackTrace doesn't tell which one...
Edit 3:
Problem Solved!
Don't know how or why, but the code works (And I afraid to change it...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我的理解方式:
Here's the way I understand it:
本文(适用于 .NET 3)示例中有一些注释,有助于解释不同的参数是什么(“步幅”等概念)以及如何计算它们。滚动到实际代码示例所在的底部。我不确定他们为什么将这些注释从 .NET 4 版本中删除。
编辑:
您能分享一下 WritePixels 的目标吗?如果您尝试绘制图像、形状等,我通常使用 DrawingContext 写入位图,然后将其渲染为 DrawingVisual。
This article (for .NET 3) has some comments in the examples that help explain what the different parameters are (concepts like "stride") and how to calculate them. Scroll to the bottom where the actual code examples are. I'm not sure why they dumped those comments out of the .NET 4 version.
Edit:
Can you share what your goal is with WritePixels? If you're trying to draw images, shapes, etc, I usually use DrawingContext to write to the bitmap and then Render it to a DrawingVisual instead.
检查高度和宽度的值。也许字节数组根本不够大!
Check the values of height and width. Perhaps the byte array is simply not big enough!