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?
views:
27answers:
1
+1
A:
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.
Alex Farber
2010-10-07 13:05:55
So I guess it's pixel size in bytes then?
Tamás Szelei
2010-10-07 14:15:26
Yes, this is pixel size in bytes. BitmapSource.Format property returns PixelFormat structure. For example, pixel size for Bgr32 is 4, for Bgr24 - 3, for Gray8 - 1 etc. Copying bitmap data to array, you need to know exactly resulting array structure, and it is defined by Format property.
Alex Farber
2010-10-07 14:43:01
Thank you very much.
Tamás Szelei
2010-10-07 14:55:40