Search code examples
c#ffmpegcastingbitmaptype-conversion

C# Convert Bitmap to FFmediaToolkits ImageData


I have standard System.Drawing.Bitmap and I need to convert it to FFMediaToolkit.Graphics.ImageData. How can I achieve that ?

I tried:

ImageData.FromArray((byte[]) converter.ConvertTo(frame, typeof(byte[])),
                ImagePixelFormat.Argb32, frame.Width,frame.Height)

but it did just gave me this error:

Unhandled exception. System.ArgumentException: Pixel buffer size doesn't match size required by this image format.

Thank you for help


Solution

  • The solution is:

    private static ImageData FrameToImageData(Bitmap bitmap) 
    {
        Rectangle rect = new Rectangle(System.Drawing.Point.Empty, bitmap.Size);
        BitmapData bitLock = bitmap.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
        ImageData bitmapImageData = ImageData.FromPointer(bitLock.Scan0, ImagePixelFormat.Bgr24, bitmap.Size);
        bitmap.UnlockBits(bitLock);
        return bitmapImageData;
    }