첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

728x90
반응형
728x170

TestProject.zip
다운로드
TestProject.z01
다운로드

▶ BitmapSourceConverter.cs

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
    }
}

 

728x90

 

▶ MainWindow.xaml

<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>

 

300x250

 

▶ MainWindow.xaml.cs

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
    }
}
728x90
반응형
그리드형(광고전용)

'EmguCV > WPF' 카테고리의 다른 글

[EMGUCV/WPF] Hello, world!  (0) 2018.05.12
Posted by icodebroker

댓글을 달아 주세요