【问题标题】:Convert byte[] to System.Drawing.Bitmap C# Xamarin.NET将 byte[] 转换为 System.Drawing.Bitmap C# Xamarin.NET
【发布时间】:2022-01-16 19:51:43
【问题描述】:

我正在尝试做一个可以将 Emgu.Cv.Mat 图像转换为 System.Drawing.Bitmap 图像的方法。

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;
        }

首先我将 Emgu.Cv.Mat 图像转换为 byte[] 图像,然后将此 byte[] 图像转换为 System.Drawing.Bitmap 图像。

此方法适用于桌面,但在 Xamarin Android 应用程序中使用时不起作用,我收到此错误:“System.PlatformNotSupportedException: '此平台不支持操作。' ”。

我知道它来自这行代码:mp = new Bitmap(ms);(我在使用Console.WriteLine之前检查过)

谁能知道问题所在,或者是否有其他路径可以将 Emgu.Cv.Mat 图像转换为 System.Drawing.Bitmap 图像?

谢谢!

【问题讨论】:

  • 对于逐个像素的比较,请使用 SkiaSharp

标签: c# android xamarin bitmap emgucv


【解决方案1】:

Xamarin.Android 或 Xamarin.iOS 不支持 System.Drawing.Bitmap。

如果要在屏幕上显示,则需要 Android 版本的 Bitmap 和 iOS UIImage。

【讨论】:

  • 感谢您的快速回复!我不知道。我使用 Bitmap 使用 System.Drawing.Bitmap.GetPixel(x, y) 逐像素比较两个图像,你知道我是否可以使用另一种图像类型来制作这个吗?
【解决方案2】:

Android Bitmap 也有GetPixel 方法,只需将System.Drawing.Bitmap 替换为Android.Graphics.Bitmap 即可,需要在android 平台上进行(有依赖服务)。

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;
}

参考

Comparing Bitmap images in Android

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

【讨论】:

  • 你解决了@Nathan Trouillet 的问题吗?
猜你喜欢
  • 2020-06-11
  • 2011-08-07
  • 1970-01-01
  • 1970-01-01
  • 2019-10-21
  • 2010-11-15
  • 1970-01-01
  • 2020-02-03
  • 1970-01-01
相关资源
最近更新 更多