“大步迈进” TransformedBitmap 对象带来的麻烦

发布于 2024-09-19 17:55:35 字数 494 浏览 4 评论 0原文

我有一个 2208 x 3000 TransformedBitmap 对象,其格式为 {Indexed8},我正在对其进行 .CopyPixels() 操作。我在

(int)((formattedBitmap.PixelWidth * formattedBitmap.Format.BitsPerPixel + 7) / 8)

方法调用中使用(假设“formattedBitmap”是我尝试从中复制像素的图像的名称)作为“stride”值,以及一个长度为 2208 的字节数组。我在代码的其他地方有类似的东西(其中图像的格式是 {Gray8})。但是,当我尝试在上述图像上执行相同的操作时,我不断收到“参数超出范围”异常说“参数值不能小于'6624000'。\r\n参数名称:缓冲区。”

我对此的问题是:为什么完全相同的代码似乎在一个地方工作而不是在另一个地方工作?用外行人的话说,真的是“跨步”吗?我怎样才能获得所需的效果(复制位)而不会出现此错误?我做错了什么

?多谢!

I have a 2208 x 3000 TransformedBitmap object with format {Indexed8} that I'm do .CopyPixels() on. I'm using

(int)((formattedBitmap.PixelWidth * formattedBitmap.Format.BitsPerPixel + 7) / 8)

(assuming 'formattedBitmap' is the name of the image from which I'm trying to copy the pixels) for the 'stride' value in my method call and an array of bytes which is 2208 in length. I have something just like this working elsewhere in the code (where the format of the image is {Gray8}. However, where I'm trying to do this same thing on the aforementioned image, I continually get an "Argument Out of Range" exception saying "The parameter value cannot be less than '6624000'.\r\nParameter name: buffer."

My questions about this are: why in the world does the exact same code seem to work in one place and not the other? What in the world, in layman's terms, really IS the 'stride'? And how can I get the desired affect (of copying the bits) without getting this error? What am I doing wrong?

Any help to this would be very much appreciated. Thanks a lot!

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

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

发布评论

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

评论(1

长途伴 2024-09-26 17:55:35

我已经弄清楚了(哇......有点不敢相信我花了将近一个小时的时间搞乱这个!)。问题是字节数​​组的大小必须是

sourceImage.PixelHeight * stride

其中

int stride = (int)((sourceImage.PixelWidth * sourceImage.Format.BitsPerPixel + 7) / 8);

它在我的代码中的其他位置工作的原因是因为而不是复制整个图像的像素(正如我试图在遇到问题的地方做的那样) ),我只是复制单行的像素...也就是说,基本上是一个 2008 x 1 区域,因此目标字节数组的大小可以恰好是 2208 并且可以正常工作。为了将来的参考,类似这样的东西可能应该总是或多或少地被使用:

int width = source.PixelWidth;
int height = source.PixelHeight;
int stride = width * ((source.Format.BitsPerPixel + 7) / 8);

byte[] bits = new byte[height * stride];

source.CopyPixels(bits, stride, 0);

干杯!

I've figured this out (wow...kinda can't believe I spent something nigh an hour messing with this!). The problem was that the byte array has to be of size

sourceImage.PixelHeight * stride

where

int stride = (int)((sourceImage.PixelWidth * sourceImage.Format.BitsPerPixel + 7) / 8);

The reason that it worked in the other location in my code is because rather than copying the pixels for the entire image (as I'm trying to do where I was having the issue), I was only copying the pixels of a single row...that is, basically a 2008 x 1 area, so that the size of the destination byte array could be exactly 2208 and it would work fine. For future reference, something like this should probably always, more or less, be used:

int width = source.PixelWidth;
int height = source.PixelHeight;
int stride = width * ((source.Format.BitsPerPixel + 7) / 8);

byte[] bits = new byte[height * stride];

source.CopyPixels(bits, stride, 0);

Cheers!

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