Abolfazl Vatanparast
Abolfazl Vatanparast

Reputation: 31

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?

Upvotes: 2

Views: 3337

Answers (1)

Abolfazl Vatanparast
Abolfazl Vatanparast

Reputation: 31

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.

Upvotes: 1

Related Questions