我正在Avalonia中编写一个应用程序,并使用OpenCvSharp从相机中获取帧。这对WPF起到了作用——我刚刚称之为
Image.Source = Mat.ToBitmapSource();
但这在Avalonia中不起作用,因为存在不同类型的Image控件及其Source属性。
我尝试通过MemoryStream执行此操作,但随后Bitmap构造函数崩溃,并出现ArgumentNullException(尽管流不是null(。
当然,我一问你,就找到了解决方案。将图像加载到内存中,然后调用它。
我得到了相同类型的错误,但在我实现using语句后,它被修复了。
//Place this in your class to create a class variable and create the System.Drawing.Bitmap Bitmap
private System.Drawing.Bitmap irBitmap = new System.Drawing.Bitmap(256, 192, PixelFormat.Format24bppRgb);
//Add this using statement to the function you're converting inside of
//to convert the System.Drawing.Bitmap to Avalonia compatible image for view/viewmodel
using (MemoryStream memory = new MemoryStream())
{
irBitmap.Save(memory, ImageFormat.Png);
memory.Position = 0;
//AvIrBitmap is our new Avalonia compatible image. You can pass this to your view
Avalonia.Media.Imaging.Bitmap AvIrBitmap = new Avalonia.Media.Imaging.Bitmap(memory);
}
如果你有问题,或者如何将其具体绑定到你的应用程序中,请告诉我。之前没有提供太多代码,所以我不知道为您的特定用例命名什么变量。