C# 中的 BitmapSource 对象出现问题

发布于 2024-10-06 19:36:08 字数 963 浏览 3 评论 0原文

我正在尝试操作图像,当涉及到位图和图像时,我相当新,所以我的问题和代码很简单。我正在初始化一个字节数组来保存 Bgr24 像素数据,以便我可以将其传递到 BitmapSource 对象中。但我的像素阵列的大小不是“我认为”的正确大小。

最后一行代码实际上是我的问题所在,参数“pixels”向我抛出以下错误“System.ArgumentException 未处理,值未落在预期范围内”。

我初始化这些变量

int imageSize = 100;
double dpi = 96;
int width = 128;
int height = 128;
byte[] pixels = new byte[width * height * 3];

//Create my image....

for (int i = 0; i < imageSize; i++) 
{
     for (int j = 0; j < imageSize; j++) 
            {
              int ct = myImage[i, j];

              pixels[i * imageSize * 3 + j + 0] = (byte)((ct % 16) * 14);
              pixels[i * imageSize * 3 + j + 1] = (byte)((ct % 32) * 7);
              pixels[i * imageSize * 3 + j + 2] = (byte)((ct % 128) * 2);

            }
}//end for

        //Create the bitmap
        BitmapSource bmpSource = BitmapSource.Create(width, height, dpi, dpi, PixelFormats.Bgr24, null, pixels, width);

,我知道我没有正确设置像素数组。有什么想法吗?

I'm attempting to manipulate an image, I'm fairly new when it comes to Bitmaps and images so bare with my on my question and code. I'm initializing an byte array to hold Bgr24 pixel data so I can pass it into a BitmapSource object. But my pixel array is not the correct size "I think".

The last line of code is actually where my problem is, the parameter "pixels" is throwing me the following error "System.ArgumentException was unhandled Value does not fall within the expected range."

I initialize these variables

int imageSize = 100;
double dpi = 96;
int width = 128;
int height = 128;
byte[] pixels = new byte[width * height * 3];

//Create my image....

for (int i = 0; i < imageSize; i++) 
{
     for (int j = 0; j < imageSize; j++) 
            {
              int ct = myImage[i, j];

              pixels[i * imageSize * 3 + j + 0] = (byte)((ct % 16) * 14);
              pixels[i * imageSize * 3 + j + 1] = (byte)((ct % 32) * 7);
              pixels[i * imageSize * 3 + j + 2] = (byte)((ct % 128) * 2);

            }
}//end for

        //Create the bitmap
        BitmapSource bmpSource = BitmapSource.Create(width, height, dpi, dpi, PixelFormats.Bgr24, null, pixels, width);

I understand that I am not setting up the pixels array correctly. Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

蔚蓝源自深海 2024-10-13 19:36:08

“值不在预期范围内”是当 WIC 函数(底层 WPF 成像功能的本机 API)返回 WINCODEC_ERR_INVALIDPARAMETERHRESULT.Check 抛出的 ArgumentException 消息。

在这种情况下,问题在于 BitmapSource.Create 的最终参数应该是位图的“步幅”(而不是宽度)。位图的“步幅”是存储位图每行所需的(整数)字节数。根据这篇 MSDN 杂志文章,计算步幅的一般公式为步幅 = (宽度 * BitsPerPixel + 7) / 8;。对于 24bpp 位图,这简化为 width * 3

为了防止异常,请传入正确的步幅值:

BitmapSource bmpSource = BitmapSource.Create(width, height, dpi, dpi, PixelFormats.Bgr24, null, pixels, width * 3);

"Value does not fall within the expected range" is the message for the ArgumentException that HRESULT.Check throws when a WIC function (the native API underlying WPF imaging functionality) returns WINCODEC_ERR_INVALIDPARAMETER.

In this case, the problem is that the final parameter to BitmapSource.Create should be the "stride" of the bitmap (not the width). A bitmap's "stride" is the (integral) number of bytes required to store each row of the bitmap. As per this MSDN magazine article, the general formula for calculating the stride is stride = (width * bitsPerPixel + 7) / 8;. For a 24bpp bitmap, this simplifies to width * 3.

To prevent the exception, pass in the correct stride value:

BitmapSource bmpSource = BitmapSource.Create(width, height, dpi, dpi, PixelFormats.Bgr24, null, pixels, width * 3);
孤独岁月 2024-10-13 19:36:08

这是我在使用位图时使用的一些代码......

private const int cRedOffset = 0;
private const int cGreenOffset = 1;
private const int cBlueOffset = 2;
private const int cAlphaOffset = 3;

var width = bmp.Width;
var height = bmp.Height;
var data = bmp.LockBits(Rectangle.FromLTRB(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
var stride = data.Stride;
var pixels = new byte[height * stride];

Marshal.Copy(data.Scan0, pixels, 0, height * stride);

for (var row = 0; row < height; row++)
{
    for (var col = 0; col < width; col++)
    {
        var pixel = (row * stride) + (col * 4);
        var red = pixels[pixel + cRedOffset];
        var green = pixels[pixel + cGreenOffset];
        var blue = pixels[pixel + cBlueOffset];
        var alpha = pixels[pixel + cAlphaOffset];
    }
}

Here is some code I use when working with Bitmaps...

private const int cRedOffset = 0;
private const int cGreenOffset = 1;
private const int cBlueOffset = 2;
private const int cAlphaOffset = 3;

var width = bmp.Width;
var height = bmp.Height;
var data = bmp.LockBits(Rectangle.FromLTRB(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
var stride = data.Stride;
var pixels = new byte[height * stride];

Marshal.Copy(data.Scan0, pixels, 0, height * stride);

for (var row = 0; row < height; row++)
{
    for (var col = 0; col < width; col++)
    {
        var pixel = (row * stride) + (col * 4);
        var red = pixels[pixel + cRedOffset];
        var green = pixels[pixel + cGreenOffset];
        var blue = pixels[pixel + cBlueOffset];
        var alpha = pixels[pixel + cAlphaOffset];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文