ImageData.Stride calculation
MAtt5816 opened this issue · 0 comments
MAtt5816 commented
I'm using example code for WPF:
using System.Windows.Media.Imaging;
...
// ImageData -> BitmapSource (unsafe)
public static unsafe BitmapSource ToBitmap(this ImageData bitmapData)
{
fixed(byte* ptr = bitmapData.Data)
{
return BitmapSource.Create(bitmapData.ImageSize.Width, bitmapData.ImageSize.Height, 96, 96, PixelFormats.Bgr32, null, new IntPtr(ptr), bitmapData.Data.Length, bitmapData.Stride);
}
}
// BitmapSource -> ImageData (safe)
public static ImageData ToImageData(this BitmapSource bitmap)
{
var wb = new WriteableBitmap(bitmap);
return ImageData.FromPointer(wb.BackBuffer, ImagePixelFormat.Bgra32, wb.PixelWidth, wb.PixelHeight);
}
I use ToImageData()
to save bitmaps to .avi file. Then I decode .avi with ToBitmap()
and I get the error code "Value does not fall within the expected range". I concluded that the error is in the stride calculation. For me, for an image width of 1076 px, stride in bitmapData.Stride
is calculated 3228 (3 * width) but should be 4304 px (4 * width).