/// <summary> /// Retrieve the DPI value for the supplied window handle /// </summary> /// <param name="hWnd">IntPtr</param> /// <returns>dpi value</returns> public static int GetDpi(IntPtr hWnd) { // Use the easiest method, but this only works for Windows 10 if (WindowsVersion.IsWindows10OrLater) { return(GetDpiForWindow(hWnd)); } // Use the second easiest method, but this only works for Windows 8.1 or later if (WindowsVersion.IsWindows81OrLater) { var hMonitor = User32Api.MonitorFromWindow(hWnd, MonitorFrom.DefaultToNearest); uint dpiX; uint dpiY; if (GetDpiForMonitor(hMonitor, MonitorDpiType.EffectiveDpi, out dpiX, out dpiY)) { return((int)dpiX); } } // Fallback to the global DPI settings using (var hdc = SafeDeviceContextHandle.FromHWnd(hWnd)) { return(Gdi32Api.GetDeviceCaps(hdc, DeviceCaps.LOGPIXELSX)); } }
/// <summary> /// Retrieve the DPI value for the supplied window handle /// </summary> /// <param name="hWnd">IntPtr</param> /// <returns>dpi value</returns> public static uint GetDpi(IntPtr hWnd) { if (!User32Api.IsWindow(hWnd)) { return(DpiHandler.DefaultScreenDpi); } // Use the easiest method, but this only works for Windows 10 if (WindowsVersion.IsWindows10OrLater) { return(GetDpiForWindow(hWnd)); } // Use the second easiest method, but this only works for Windows 8.1 or later if (WindowsVersion.IsWindows81OrLater) { var hMonitor = User32Api.MonitorFromWindow(hWnd, MonitorFrom.DefaultToNearest); // ReSharper disable once UnusedVariable if (GetDpiForMonitor(hMonitor, MonitorDpiType.EffectiveDpi, out var dpiX, out var dpiY)) { return(dpiX); } } // Fallback to the global DPI settings using (var hdc = SafeDeviceContextHandle.FromHWnd(hWnd)) { if (hdc == null) { return(DpiHandler.DefaultScreenDpi); } return((uint)Gdi32Api.GetDeviceCaps(hdc, DeviceCaps.LOGPIXELSX)); } }
public void TestSafeDeviceContextHandle_IntPtrZero() { using (var safeDeviceContextHandle = SafeDeviceContextHandle.FromHWnd(IntPtr.Zero)) { Assert.Null(safeDeviceContextHandle); } }
public static Bitmap getIconBits(IntPtr hIcon, int iconSize) { ICONINFO iconInfo; if (GetIconInfo(hIcon, out iconInfo)) { using (SafeDeviceContextHandle dc = SafeDeviceContextHandle.Get()) { BITMAPINFO bmi = new BITMAPINFO(); bmi.biSize = 40; bmi.biWidth = iconSize; bmi.biHeight = -iconSize; bmi.biPlanes = 1; bmi.biBitCount = 32; bmi.biCompression = 0; int intArrSize = iconSize * iconSize; int[] iconBits = new int[intArrSize]; GetDIBits(dc, iconInfo.hbmColor, 0, (uint)iconSize, iconBits, ref bmi, 0); bool hasAlpha = false; if (isXP) { for (int i = 0; i < iconBits.Length; i++) { if ((iconBits[i] & 0xFF000000) != 0) { hasAlpha = true; break; } } } if (!hasAlpha) { int[] maskBits = new int[intArrSize]; GetDIBits(dc, iconInfo.hbmMask, 0, (uint)iconSize, maskBits, ref bmi, 0); for (int i = 0; i < iconBits.Length; i++) { if (maskBits[i] == 0) { iconBits[i] = (int)((uint)iconBits[i] | 0xFF000000); } } } DeleteObject(iconInfo.hbmColor); DeleteObject(iconInfo.hbmMask); DeleteObject(hIcon); Bitmap bitmap = new Bitmap(iconSize, iconSize, PixelFormat.Format32bppArgb); BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, iconSize, iconSize), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); try { Marshal.Copy(iconBits, 0, bitmapData.Scan0, iconBits.Length); } finally { bitmap.UnlockBits(bitmapData); } return(bitmap); } } return(null); }
public static Bitmap getFileChooserBitmap() { if (hmodShell32 != IntPtr.Zero) { // Code copied from ShellFolder2.cpp Java_sun_awt_shell_Win32ShellFolder2_getFileChooserBitmapBits // Get a handle to an icon. SafeGdiObjectHandle hBitmap = null; try { hBitmap = isVista ? LoadImage(hmodShell32, "IDB_TB_SH_DEF_16", IMAGE_BITMAP, 0, 0, 0) : LoadImage(hmodShell32, (IntPtr)216, IMAGE_BITMAP, 0, 0, 0); if (hBitmap == null && hmodComctl32 != IntPtr.Zero) { hBitmap = LoadImage(hmodComctl32, (IntPtr)124, IMAGE_BITMAP, 0, 0, 0); } if (hBitmap != null) { BITMAPINFO bmi = new BITMAPINFO(); GetObject(hBitmap, Marshal.SizeOf(bmi), ref bmi); int width = bmi.biWidth; int height = bmi.biHeight; bmi.biSize = 40; bmi.biHeight = -bmi.biHeight; bmi.biPlanes = 1; bmi.biBitCount = 32; bmi.biCompression = 0; if (width == 0 || height == 0) { return(null); } using (SafeDeviceContextHandle dc = SafeDeviceContextHandle.Get()) { int[] data = new int[width * height]; GetDIBits(dc, hBitmap, (uint)0, (uint)height, data, ref bmi, 0); Bitmap bitmap = new Bitmap(data.Length / 16, 16, PixelFormat.Format32bppArgb); BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); Marshal.Copy(data, 0, bitmapData.Scan0, data.Length); bitmap.UnlockBits(bitmapData); return(bitmap); } } } finally { if (hBitmap != null) { hBitmap.Close(); } } } return(null); }
private static int[] getIconBits(IntPtr hIcon, int iconSize) { ICONINFO iconInfo; if (GetIconInfo(hIcon, out iconInfo)) { using (SafeDeviceContextHandle dc = SafeDeviceContextHandle.Get()) { BITMAPINFO bmi = new BITMAPINFO(); bmi.biSize = 40; bmi.biWidth = iconSize; bmi.biHeight = -iconSize; bmi.biPlanes = 1; bmi.biBitCount = 32; bmi.biCompression = 0; int intArrSize = iconSize * iconSize; int[] iconBits = new int[intArrSize]; GetDIBits(dc, iconInfo.hbmColor, 0, (uint)iconSize, iconBits, ref bmi, 0); bool hasAlpha = false; if (isXP) { for (int i = 0; i < iconBits.Length; i++) { if ((iconBits[i] & 0xFF000000) != 0) { hasAlpha = true; break; } } } if (!hasAlpha) { int[] maskBits = new int[intArrSize]; GetDIBits(dc, iconInfo.hbmMask, 0, (uint)iconSize, maskBits, ref bmi, 0); for (int i = 0; i < iconBits.Length; i++) { if (maskBits[i] == 0) { iconBits[i] = (int)((uint)iconBits[i] | 0xFF000000); } } } DeleteObject(iconInfo.hbmColor); DeleteObject(iconInfo.hbmMask); return(iconBits); } } return(null); }
private static extern int GetDIBits(SafeDeviceContextHandle hdc, SafeGdiObjectHandle hbmp, uint uStartScan, uint cScanLines, int[] lpvBits, ref BITMAPINFO lpbmi, uint uUsage);
/// <summary> /// Get a SafeHandle for the GetHdc, so one can use using to automatically cleanup the devicecontext /// </summary> /// <param name="graphics">Graphics</param> /// <returns>SafeDeviceContextHandle</returns> public static SafeDeviceContextHandle GetSafeDeviceContext(this Graphics graphics) { return(SafeDeviceContextHandle.FromGraphics(graphics)); }