diff --git a/src/Eto.WinForms/Win32.gdi.cs b/src/Eto.WinForms/Win32.gdi.cs index cdbe978a6..527641bf0 100755 --- a/src/Eto.WinForms/Win32.gdi.cs +++ b/src/Eto.WinForms/Win32.gdi.cs @@ -86,5 +86,8 @@ public static List GetUnicodeRangesForFont(this sd.Font font) g.Dispose(); return fontRanges; } + + [DllImport("gdi32.dll")] + public static extern bool DeleteObject(IntPtr hObject); } } diff --git a/src/Eto.Wpf/Eto.Wpf.csproj b/src/Eto.Wpf/Eto.Wpf.csproj index 22a881abd..011c4634e 100755 --- a/src/Eto.Wpf/Eto.Wpf.csproj +++ b/src/Eto.Wpf/Eto.Wpf.csproj @@ -101,6 +101,9 @@ You do not need to use any of the classes of this assembly (unless customizing t Win32.dpi.cs + + Win32.gdi.cs + WinConversions.shared.cs diff --git a/src/Eto.Wpf/Forms/ScreenHandler.cs b/src/Eto.Wpf/Forms/ScreenHandler.cs index b6453869f..ed6e602a1 100755 --- a/src/Eto.Wpf/Forms/ScreenHandler.cs +++ b/src/Eto.Wpf/Forms/ScreenHandler.cs @@ -56,16 +56,24 @@ public Image GetImage(RectangleF rect) using (var bmpGraphics = sd.Graphics.FromImage(screenBmp)) { bmpGraphics.CopyFromScreen(realRect.X, realRect.Y, 0, 0, realRect.Size.ToSD()); - var bitmapSource = sw.Interop.Imaging.CreateBitmapSourceFromHBitmap( - screenBmp.GetHbitmap(), - IntPtr.Zero, - sw.Int32Rect.Empty, - sw.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); - - if (oldDpiAwareness != Win32.DPI_AWARENESS_CONTEXT.NONE) - Win32.SetThreadDpiAwarenessContextSafe(oldDpiAwareness); - - return new Bitmap(new BitmapHandler(bitmapSource)); + var hbmp = screenBmp.GetHbitmap(); + try + { + var bitmapSource = sw.Interop.Imaging.CreateBitmapSourceFromHBitmap( + hbmp, + IntPtr.Zero, + sw.Int32Rect.Empty, + sw.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); + + if (oldDpiAwareness != Win32.DPI_AWARENESS_CONTEXT.NONE) + Win32.SetThreadDpiAwarenessContextSafe(oldDpiAwareness); + + return new Bitmap(new BitmapHandler(bitmapSource)); + } + finally + { + Win32.DeleteObject(hbmp); + } } } }