[C#/WPF/EMGUCV] WPF에서 EmguCV 사용하기
■ WPF에서 EmguCV를 사용하는 방법을 보여준다. ▶ BitmapSourceConverter.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
using System; using System.Drawing; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; using System.Windows.Media.Imaging; using Emgu.CV; namespace TestProject { /// <summary> /// 비트맵 소스 변환기 /// </summary> public static class BitmapSourceConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 객체 삭제하기 - DeleteObject(objectHandle) /// <summary> /// 객체 삭제하기 /// </summary> /// <param name="objectHandle">객체 핸들</param> /// <returns>처리 결과</returns> [DllImport("gdi32")] private static extern int DeleteObject(IntPtr objectHandle); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 비트맵 소스 구하기 - GetBitmapSource(image) /// <summary> /// 비트맵 소스 구하기 /// </summary> /// <param name="image">이미지</param> /// <returns>비트맵 소스</returns> public static BitmapSource GetBitmapSource(IImage image) { using(Bitmap source = image.Bitmap) { IntPtr bitmapHandle = source.GetHbitmap(); BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap ( bitmapHandle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions() ); DeleteObject(bitmapHandle); return bitmapSource; } } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Hello, world!" Width="800" Height="600"> <Grid> <Image Name="image" Stretch="Fill" Initialized="image_Initialized" /> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
using System; using System.Drawing; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; using System.Windows.Media.Imaging; using Emgu.CV; namespace TestProject { /// <summary> /// 비트맵 소스 변환기 /// </summary> public static class BitmapSourceConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 객체 삭제하기 - DeleteObject(objectHandle) /// <summary> /// 객체 삭제하기 /// </summary> /// <param name="objectHandle">객체 핸들</param> /// <returns>처리 결과</returns> [DllImport("gdi32")] private static extern int DeleteObject(IntPtr objectHandle); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 비트맵 소스 구하기 - GetBitmapSource(image) /// <summary> /// 비트맵 소스 구하기 /// </summary> /// <param name="image">이미지</param> /// <returns>비트맵 소스</returns> public static BitmapSource GetBitmapSource(IImage image) { using(Bitmap source = image.Bitmap) { IntPtr bitmapHandle = source.GetHbitmap(); BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap ( bitmapHandle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions() ); DeleteObject(bitmapHandle); return bitmapSource; } } #endregion } } |
TestProject.zip