BitmapSource.CopyPixels -what's a good value for stride?

Viewed 18852

I'm trying to get the pixel data from a WPF BitmapSource object. As I understand, this can be accomplished by calling its CopyPixels method. This method needs a stride parameter, which I don't know how to obtain. As far as I know, stride is value that's used when stepping in the array during reading or copying. What would be an appropriate stride value for any BitmapSource?

2 Answers
var stride = ((bitmapSource.PixelWidth * bitmapSource.Format.BitsPerPixel + 31) / 32) * 4;

or

var stride = ((bitmapSource.PixelWidth * bitmapSource.Format.BitsPerPixel + 31) >> 5) << 2;
Related