C# 中的 BitmapSource 对象出现问题
我正在尝试操作图像,当涉及到位图和图像时,我相当新,所以我的问题和代码很简单。我正在初始化一个字节数组来保存 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“值不在预期范围内”是当 WIC 函数(底层 WPF 成像功能的本机 API)返回
WINCODEC_ERR_INVALIDPARAMETER
时HRESULT.Check
抛出的 ArgumentException 消息。在这种情况下,问题在于
BitmapSource.Create
的最终参数应该是位图的“步幅”(而不是宽度)。位图的“步幅”是存储位图每行所需的(整数)字节数。根据这篇 MSDN 杂志文章,计算步幅的一般公式为步幅 = (宽度 * BitsPerPixel + 7) / 8;
。对于 24bpp 位图,这简化为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) returnsWINCODEC_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 isstride = (width * bitsPerPixel + 7) / 8;
. For a 24bpp bitmap, this simplifies towidth * 3
.To prevent the exception, pass in the correct stride value:
这是我在使用位图时使用的一些代码......
Here is some code I use when working with Bitmaps...