调用 WriteableBitmap.WritePixels 方法

发布于 2024-10-20 02:02:49 字数 1480 浏览 2 评论 0原文

我正在尝试调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

朱染 2024-10-27 02:02:49

这是我的理解方式:

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

Here's the way I understand it:

  • System.Windows.Int32Rect sourceRect: The most obvious argument. Represents the position, width and height of the subset of your bitmap you are trying to write into.
  • System.Array pixels: A one or two dimensional array of bytes. The pixel format you used in the bitmap constructor will determine how many byte entries are used for each pixel.
  • System.Int32 stride: Defines how many of the pixels array entry should each row use (the width)
  • System.Int32 offset: An offset in your input pixels array, if you want to reuse your array for more than one WritePixels call.
夜夜流光相皎洁 2024-10-27 02:02:49

本文(适用于 .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.

蝶…霜飞 2024-10-27 02:02:49

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

Check the values of height and width. Perhaps the byte array is simply not big enough!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文