emgu cv 在WPF上显示图像

来源:互联网 发布:欧洲 旅游 大学生 知乎 编辑:程序博客网 时间:2024/05/17 04:19


xmal页代码

<Window x:Class="显示图片.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="MainWindow" Height="350" Width="525">    <Grid>        <Image Height="287" HorizontalAlignment="Left" Margin="34,12,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="380" />        <Button Content="Button" Height="20" HorizontalAlignment="Left" Margin="432,40,0,0" Name="button1" VerticalAlignment="Top" Width="46" Click="button1_Click" />    </Grid></Window>

xmal.cs代码

需要在dll里面添加System.Drawing

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.Runtime.InteropServices;using Emgu.CV;using Emgu.CV.Structure;namespace 显示图片{    /// <summary>    /// MainWindow.xaml 的交互逻辑    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();        }        private void display()        {            Image<Bgr, Byte> image = new Image<Bgr, byte>("d:/0.bmp");            MCvFont f = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_PLAIN, 3.0, 3.0);            image.Draw("Hello, world", ref f, new System.Drawing.Point(10, 50), new Bgr(255.0, 0.0, 0.0));            image1.Source = ToBitmapSource(image);        }        /// <summary>        /// emgu cv  显示        /// </summary>        /// <param name="o"></param>        /// <returns></returns>        [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(); //obtain the Hbitmap                BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(                    ptr,                    IntPtr.Zero,                    Int32Rect.Empty,                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());                DeleteObject(ptr); //release the HBitmap                return bs;            }        }        private void button1_Click(object sender, RoutedEventArgs e)        {            display();        }    }}

结果如图


原创粉丝点击