// this code is adapted from Icon.GetIconSize please take this into account when changing this private Size GetIconSize(IntPtr iconHandle) { Size iconSize = Size; NativeMethods.ICONINFO info = new NativeMethods.ICONINFO(); SafeNativeMethods.GetIconInfo(new HandleRef(this, iconHandle), info); NativeMethods.BITMAP bmp = new NativeMethods.BITMAP(); if (info.hbmColor != IntPtr.Zero) { UnsafeNativeMethods.GetObject(new HandleRef(null, info.hbmColor), Marshal.SizeOf(typeof(NativeMethods.BITMAP)), bmp); SafeNativeMethods.IntDeleteObject(new HandleRef(null, info.hbmColor)); iconSize = new Size(bmp.bmWidth, bmp.bmHeight); } else if (info.hbmMask != IntPtr.Zero) { UnsafeNativeMethods.GetObject(new HandleRef(null, info.hbmMask), Marshal.SizeOf(typeof(NativeMethods.BITMAP)), bmp); iconSize = new Size(bmp.bmWidth, bmp.bmHeight / 2); } if (info.hbmMask != IntPtr.Zero) { SafeNativeMethods.IntDeleteObject(new HandleRef(null, info.hbmMask)); } return(iconSize); }
// this code is adapted from Icon.GetIconSize please take this into account when changing this private Size GetIconSize(IntPtr iconHandle) { Size iconSize = Size; NativeMethods.ICONINFO info = default; SafeNativeMethods.GetIconInfo(new HandleRef(this, iconHandle), ref info); NativeMethods.BITMAP bmp = new NativeMethods.BITMAP(); if (info.hbmColor != IntPtr.Zero) { UnsafeNativeMethods.GetObject(new HandleRef(null, info.hbmColor), Marshal.SizeOf <NativeMethods.BITMAP>(), bmp); Interop.Gdi32.DeleteObject(info.hbmColor); iconSize = new Size(bmp.bmWidth, bmp.bmHeight); } else if (info.hbmMask != IntPtr.Zero) { UnsafeNativeMethods.GetObject(new HandleRef(null, info.hbmMask), Marshal.SizeOf <NativeMethods.BITMAP>(), bmp); iconSize = new Size(bmp.bmWidth, bmp.bmHeight / 2); } if (info.hbmMask != IntPtr.Zero) { Interop.Gdi32.DeleteObject(info.hbmMask); } return(iconSize); }