2012-09-11 6 views
3

저는 Kinect를 사용하여 얼굴 인식을 수행하는 Winforms의 Emgu Cv로 작업하고 있습니다. 자, WPF로 옮기고 싶습니다. 그러나 EmguCv 라이브러리는 비트 맵 클래스 만 지원합니다.WPF의 비트 맵 클래스

WPF에서 Winforms에서 사용되는 Bitmap 클래스를 사용할 수 있습니까? 그렇지 않다면, WPF에서 kinect와 Emgu cv를 사용하는 다른 방법이 있습니까?

감사합니다.

답변

9

System.Drawing.Bitmap은 WPF의 이미지 원본으로 직접 사용할 수 없으므로 System.Windows.Media.Imaging.BitmapSource으로 변환해야합니다.

가장 좋은 방법은 Imaging.CreateBitmapSourceFromHBitmap을 사용하는 것입니다.

당신은 확장 메서드 사용할 수 있습니다

[DllImport("gdi32")] 
private static extern int DeleteObject(IntPtr o); 

public static BitmapSource ToBitmapSource(this System.Drawing.Bitmap source) 
{ 
    if (source == null) 
    { 
     throw new ArgumentNullException("source"); 
    } 

    IntPtr ip = source.GetHbitmap(); 
    try 
    { 
     return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, 
      IntPtr.Zero, Int32Rect.Empty, 
      System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); 
    } 
    finally 
    { 
     DeleteObject(ip); 
    } 
} 

Bitmap.GetHbitmap()는 GDI 핸들 누수 때문에, DeleteObject를 호출하셔야을 (this 대답을 참조).

일단 BitmapSource이 있으면 Image 컨트롤을 사용하고 Source 속성을 설정하여 표시 할 수 있습니다.

이 문서에서 WPF 이미징에 대한 자세한 내용을보실 수 있습니다 : Imaging Overview

+0

안녕하세요, system.Drawing에 대한 참조를 추가해야합니까? –

+0

@RafikHaceb 네, 프로젝트에'System.Drawing.dll'과'PresentationCore.dll'에 대한 참조가 있는지 확인하십시오. 그런데 도움이되는 답변을 찾으면 '수락'으로 표시 할 수 있습니다. [답변 수락 방법은 무엇입니까?] (http://meta.stackexchange.com/a/5235/164263)를 참조하십시오. –

관련 문제