Search code examples
c#bitmapimagesharp

How to convert Intptr to ImageSharp Image in C#?


In c# (Windows), with the width, height and stride of an image, we can convert an Intptr to Bitmap as follows:

var bitmap = new Bitmap(width, height, stride, PixelFormat.Format24bppRgb, intptrImage);

But System.Drawing.Bitmap is no longer available on Linux and we have to use SixLabors.ImageSharp.Image. How do I convert an Intptr to an image using ImageSharp?


Solution

  • I found this solution:

    // copy data to byte array
    var size = height * stride;
    var managedArray = new byte[size];
    Marshal.Copy(intptrImage, managedArray, 0, size);
    
    var image = SixLabors.ImageSharp.Image.LoadPixelData<Bgr24>(managedArray,width, height);
    

    This solution works fine.