Home > Back-end >  Convert byte[] to System.Drawing.Bitmap C# Xamarin.NET
Convert byte[] to System.Drawing.Bitmap C# Xamarin.NET

Time:12-14

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!

CodePudding user response:

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.

CodePudding user response:

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?

  • Related