'Stride' Woes from a TransformedBitmap Object 'Stride' Woes from a TransformedBitmap Object wpf wpf

'Stride' Woes from a TransformedBitmap Object


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!