来自TransformedBitmap对象的“跨越"困境 [英] 'Stride' Woes from a TransformedBitmap Object

查看:121
本文介绍了来自TransformedBitmap对象的“跨越"困境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2208 x 3000 TransformedBitmap对象,其格式为{Indexed8},我在上面执行.CopyPixels().我正在使用

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)

(假设"formattedBitmap"是我要从中复制像素的图像的名称)在我的方法调用中的步幅"值和一个字节数组,其长度为2208.我在代码的其他地方也有类似的工作(图像的格式为{Gray8}.但是,在尝试对上述图像执行相同操作的地方,我不断收到"Argument Out of Range"

(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!

推荐答案

我已经弄清楚了(哇...有点难以置信,我花了将近一个小时弄弄了这个!).问题在于字节数组必须具有大小

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

其中

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

它可以在我的代码的其他位置工作的原因是,我没有复制整个图像的像素(因为我试图在遇到问题的地方进行操作),而是仅复制了一个像素.单行...基本上是2008 x 1区域,因此目标字节数组的大小可以恰好是2208,并且可以正常工作.为了将来参考,应该始终或多或少地使用类似这样的内容:

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);

干杯!

这篇关于来自TransformedBitmap对象的“跨越"困境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆