카메라 에서 받은 프레임을 이미지 컨트롤에 넣으려는데...

MemoryStream을 이용한 BitmapSource 변환은 성능 이슈와 메모리 누수 이슈가 있다고 합니다. (메모리 누수는 솔루션이 있지만 테스트 해보지 않았습니다.)

https://www.google.com/search?q=bitmapimage+streamsource+memory+leak

관련 내용은 참고하시고 아래 코드를 사용해 보세요. 메모리 누수 없이 안정적으로 동작했던 코드입니다.

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

public static BitmapSource ToBitmapSource(Bitmap bitmap)
{            
    var ptr = bitmap.GetHbitmap(); //obtain the Hbitmap
    try 
    {
        return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
            ptr,
            IntPtr.Zero,
            Int32Rect.Empty,
            BitmapSizeOptions.FromEmptyOptions());
    }
    finally
    {
        DeleteObject(ptr); //release the HBitmap

        bitmap.Dispose();
    }
}
3개의 좋아요