Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageData.Stride calculation #135

Open
MAtt5816 opened this issue Dec 4, 2023 · 0 comments
Open

ImageData.Stride calculation #135

MAtt5816 opened this issue Dec 4, 2023 · 0 comments

Comments

@MAtt5816
Copy link

MAtt5816 commented Dec 4, 2023

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant