[英]Is it possible to create Avalonia.Media.Imaging.Bitmap from System.Drawing.Bitmap?
I am writing an application in Avalonia and using OpenCvSharp to get frames from the camera.我正在 Avalonia 中编写一个应用程序并使用 OpenCvSharp 从相机中获取帧。 This worked with WPF - there I just called这适用于 WPF - 我刚刚打电话给
Image.Source = Mat.ToBitmapSource();
but this doesn't work in Avalonia, because there is a different type of Image control, and a different type of its Source property.但这在 Avalonia 中不起作用,因为存在不同类型的Image控件,以及不同类型的Source属性。
I tried doing this via a MemoryStream , but then the Bitmap constructor crashes with an ArgumentNullException (although the stream is not null).我尝试通过MemoryStream执行此操作,但随后Bitmap构造函数因ArgumentNullException而崩溃(尽管 stream 不为空)。
Of course, as soon as I asked you that, I found a solution.当然,我一问你,我就找到了解决办法。 Load the image into memory and then call it back.将镜像加载到 memory 中,然后回调。
I was getting the same type of error but that was fixed after I implemented the using statement.我得到了相同类型的错误,但在我实现 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);
}
Let me know if you have questions or how to specifically tie this into your app.如果您有任何问题或如何将其具体绑定到您的应用程序中,请告诉我。 There wasn't a lot of code provided earlier so I didn't have any idea what to name variables for your specific use case.之前没有提供很多代码,所以我不知道为您的特定用例命名变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.