Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

image

wpf

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?

like image 441
Tamás Szelei Avatar asked Oct 07 '10 12:10

Tamás Szelei


2 Answers

You can use stride = pixel_size * image_width value. For example, for RGBA bitmap with 100 pixel width, stride = 400.

Some applications may require special line alignment. For example, Windows GDI bitmaps require 32-bits line alignment. In this case, for RGB bitmap with width = 33, stride value 33*3=99 should be changed to 100, to have 32-bits line alignment in destination array.

Generally, you should know destination array requirements. In there are no special requirements, use default pixel_size * image_width.

like image 54
Alex F Avatar answered Sep 22 '22 15:09

Alex F


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

or

var stride = ((bitmapSource.PixelWidth * bitmapSource.Format.BitsPerPixel + 31) >> 5) << 2;
like image 26
Andrey Avatar answered Sep 20 '22 15:09

Andrey