我正在 Avalonia 编写一个应用程序,并使用 OpenCvSharp 从相机获取帧。这适用于 WPF - 我刚刚打电话
Image.Source = Mat.ToBitmapSource();
但这在 Avalonia 中不起作用,因为有不同类型的 Image 控件,以及不同类型的 Source 属性。
我尝试通过 MemoryStream 执行此操作,但随后 Bitmap 构造函数因 ArgumentNullException 崩溃(尽管流不为空)。
最佳答案
当然,我一问你这个问题,我就找到了解决办法。将图像加载到内存中,然后回调。
我遇到了相同类型的错误,但在我实现 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);
}
如果您有疑问或如何将其具体绑定(bind)到您的应用程序中,请告诉我。之前没有提供很多代码,因此我不知道如何为您的特定用例命名变量。
关于c# - 是否可以从 System.Drawing.Bitmap 创建 Avalonia.Media.Imaging.Bitmap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63391133/