private static void Initialize() { if (s_isInitialized) { return; } using ScreenDC dc = ScreenDC.Create(); s_deviceDpi = Interop.Gdi32.GetDeviceCaps(dc, Interop.Gdi32.DeviceCapability.LOGPIXELSX); s_isInitialized = true; }
private IntPtr GetCompatibleBitmap(Bitmap bm) { using ScreenDC hDC = ScreenDC.Create(); // GDI+ returns a DIBSECTION based HBITMAP. The clipboard deals well // only with bitmaps created using CreateCompatibleBitmap(). So, we // convert the DIBSECTION into a compatible bitmap. IntPtr hBitmap = bm.GetHbitmap(); // Create a compatible DC to render the source bitmap. IntPtr dcSrc = Gdi32.CreateCompatibleDC(hDC); IntPtr srcOld = Gdi32.SelectObject(dcSrc, hBitmap); // Create a compatible DC and a new compatible bitmap. IntPtr dcDest = Gdi32.CreateCompatibleDC(hDC); IntPtr hBitmapNew = Gdi32.CreateCompatibleBitmap(hDC, bm.Size.Width, bm.Size.Height); // Select the new bitmap into a compatible DC and render the blt the original bitmap. IntPtr destOld = Gdi32.SelectObject(dcDest, hBitmapNew); Gdi32.BitBlt( dcDest, 0, 0, bm.Size.Width, bm.Size.Height, dcSrc, 0, 0, Gdi32.ROP.SRCCOPY); // Clear the source and destination compatible DCs. Gdi32.SelectObject(dcSrc, srcOld); Gdi32.SelectObject(dcDest, destOld); Gdi32.DeleteDC(dcSrc); Gdi32.DeleteDC(dcDest); return(hBitmapNew); }