-
[WPF] Bitmap to Image ConverterProgramming/C#, WPF 2019. 5. 26. 03:14반응형
Converter에서의 이미지 변환후 메모리 해제는 자동으로 이루어지지 않습니다.
꼭 GC.WaitForPendingFinalizers(); 를 호출하여 메모리 해제를 해줘야 합니다.
GC.Collect 만으로는 제대로 해제되지 않습니다.
123456789101112131415161718192021222324252627[System.Runtime.InteropServices.DllImport("gdi32.dll")]private static extern bool DeleteObject(IntPtr hObject);public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture){if (value == null)return null;try{IntPtr hBitmap = ((System.Drawing.Bitmap)value).GetHbitmap();BitmapSource retval = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty,System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());DeleteObject(hBitmap);GC.Collect(System.GC.MaxGeneration, System.GCCollectionMode.Forced);GC.WaitForPendingFinalizers();return retval;}catch (Exception ex){Console.WriteLine(ex.ToString());return null;}}cs 반응형'Programming > C#, WPF' 카테고리의 다른 글
[WPF] WPF에서 Lottie 사용하기 (0) 2019.08.02 [C# / WinForm] HWP to IMAGE, 한글문서 이미지 변환 (0) 2019.07.30 마샬링(Marshalling) (0) 2019.06.12