[상단] emgu cv WPF에 이미지 표시

2989 단어 C#그림WPFcvEmgu
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();
        }
    }
}

결과는 그림과 같다

좋은 웹페이지 즐겨찾기