gpt4 book ai didi

c# - 带有 C# WPF 的 EMGU

转载 作者:太空宇宙 更新时间:2023-11-03 21:56:06 28 4
gpt4 key购买 nike

我正在尝试按照以下教程进行操作,但使用的是 WPF 而不是 Win Forms:

A Basic Program

WPF 不使用 PictureBox,而是使用 Image

所以这里尝试加载一个 Image

XAML

<Image x:Name="srcImg" Width="400" Height="300"></Image>

CS 尝试 1:

Image<Bgr, Byte> My_Image = new Image<Bgr, byte>(Openfile.FileName);
srcImg.Source = My_Image.ToBitmap();

错误信息

Cannot implicitly convert type 'System.Drawing.Bitmap' 
to 'System.Windows.Media.ImageSource'

CS 尝试 2:

Image<Bgr, Byte> My_Image = new Image<Bgr, byte>(Openfile.FileName);
srcImg.Source = new BitmapImage(My_Image);

错误信息

Error   1   The best overloaded method match for 'System.Windows.Media.Imaging.BitmapImage.BitmapImage(System.Uri)' has some invalid arguments  
Error 2 Argument 1: cannot convert from 'Emgu.CV.Image<Emgu.CV.Structure.Bgr,byte>' to 'System.Uri'

知道我做错了什么吗?

最佳答案

问题已解决。要转换图像:

Image<Bgr, Byte> My_Image = new Image<Bgr, byte>(Openfile.FileName);
srcImg.Source = BitmapSourceConvert.ToBitmapSource(myImage);

BitmapSourceConvert 类:

public static class BitmapSourceConvert
{
[DllImport("gdi32")]
private static extern int DeleteObject(IntPtr o);

public static BitmapSource ToBitmapSource(IImage image)
{
using (System.Drawing.Bitmap source = image.Bitmap)
{
IntPtr ptr = source.GetHbitmap();

BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
ptr,
IntPtr.Zero,
Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

DeleteObject(ptr);
return bs;
}
}
}

关于c# - 带有 C# WPF 的 EMGU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16596915/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com