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); }