0

I'm trying to do a method that can convert a Emgu.Cv.Mat image into a System.Drawing.Bitmap image.

public Bitmap convertCvToBitmap(Mat img)
        {
            byte[] temp_img = this.convertCvToImage(img);
            Bitmap mp;
            using (var ms = new MemoryStream(temp_img))
            {
                mp = new Bitmap(ms);
            }
            return mp;
        }

Firstly I the convert Emgu.Cv.Mat image into a byte[] image, and then I convert this byte[] image into a System.Drawing.Bitmap image.

This method works on a desktop but doesn't when used in a Xamarin Android App, I've got this error: "System.PlatformNotSupportedException: 'Operation is not supported on this platform.'".

I know it's coming from this line of code: mp = new Bitmap(ms); (I checked it before using Console.WriteLine)

Can anyone knows the problem or if there is another path to convert an Emgu.Cv.Mat image into a System.Drawing.Bitmap image?

Thanks!

2 Answers2

0

System.Drawing.Bitmap is not supported in Xamarin.Android or Xamarin.iOS.

If you are going to display it on screen, then you need the Android variant of Bitmap and the iOS UIImage.

Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
  • Thanks for your quick reply ! I didn't know that. I use Bitmap to compare two images pixels by pixels using System.Drawing.Bitmap.GetPixel(x, y) , do you know if I can use another image type to make this ? – Nathan Trouillet Dec 13 '21 at 10:20
0

Android Bitmap also has the method GetPixel , just replace System.Drawing.Bitmap with Android.Graphics.Bitmap , and you need to do this in android platform (with dependency service).

public Android.Graphics.Bitmap convertCvToBitmap(Mat img)
{
   byte[] temp_img = this.convertCvToImage(img);
   Android.Graphics.Bitmap mp = BitmapFactory.DecodeByteArray(temp_img, 0, temp_img.Length);
   return mp;
}

Refer to

Comparing Bitmap images in Android

How to convert image file data in a byte array to a Bitmap?

ColeX
  • 14,062
  • 5
  • 43
  • 240