"Stride" Woes from a TransformedBitmap Object

I have a TransformedBitmap 2208 x 3000 object with the format {Indexed8}, which I make .CopyPixels () on. I use

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

(it is assumed that "formattedBitmap" is the name of the image from which I am trying to copy pixels) for the value "stride" in my method call and byte array of length 2208. I have something like this working elsewhere in the code (where the image format is {Gray8}. However, when I try to do the same in the above image, I constantly get an "Argument out of range" exception: "The value of the parameter cannot be less than" 6624000 ". \ r \ nParameter: buffer."

My questions are about this: why in the world is the exact same code working in one place and not in another? What in the world, in the conditions of the layman, really is a "step"? And how can I get the desired effect (on copying bits) without getting this error? What am I doing wrong?

Any help on this would be greatly appreciated. Many thanks!

+7
c # image-processing byte wpf pixels
source share
1 answer

I realized this (wow ... I can't seem to believe that I spent something about an hour, messing around with it!). The problem was that the byte array should be sized

 sourceImage.PixelHeight * stride 

Where

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

The reason it worked elsewhere in my code is because instead of copying pixels for the whole image (since I'm trying to do what I have), I only copied the pixels on one line ... that is, basically , area xx 2008, so the size of the destination byte array can be exactly 2208, and it will work fine. For future reference, there should probably be something like this, more or less:

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

Hooray!

+9
source share

All Articles