'Stride' Woes from a TransformedBitmap Object

狂风中的少年 提交于 2019-12-05 05:22:22

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!