public static System.Drawing.Bitmap ToBitmap(System.Drawing.Icon icon, Skybound.Drawing.Design.IconFormat iconFormat) { Skybound.Drawing.Design.IconFile.BITMAP bitmap; Skybound.Drawing.Design.IconFile.ICONINFO iconinfo; System.Drawing.Bitmap bitmap1 = null; if (icon != null) { if (iconFormat.ColorDepth == System.Windows.Forms.ColorDepth.Depth32Bit) { Skybound.Drawing.Design.IconFile.GetIconInfo(icon.Handle, out iconinfo); bitmap = new Skybound.Drawing.Design.IconFile.BITMAP(); Skybound.Drawing.Design.IconFile.GetObjectBitmap(iconinfo.hbmColor, System.Runtime.InteropServices.Marshal.SizeOf(typeof(Skybound.Drawing.Design.IconFile.BITMAP)), ref bitmap); int i = bitmap.bmWidthBytes * bitmap.bmHeight; int[] iArr = new int[(i / 4)]; Skybound.Drawing.Design.IconFile.GetBitmapBits(iconinfo.hbmColor, i, iArr); System.Runtime.InteropServices.GCHandle gchandle = System.Runtime.InteropServices.GCHandle.Alloc(iArr, System.Runtime.InteropServices.GCHandleType.Pinned); System.Drawing.Bitmap bitmap2 = new System.Drawing.Bitmap(bitmap.bmWidth, bitmap.bmHeight, bitmap.bmWidthBytes, System.Drawing.Imaging.PixelFormat.Format32bppArgb, System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(iArr, 0)); bitmap1 = new System.Drawing.Bitmap(bitmap2.Width, bitmap2.Height); using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap1)) { graphics.DrawImage(bitmap2, 0, 0, bitmap2.Width, bitmap2.Height); } gchandle.Free(); Skybound.Drawing.Design.IconFile.DeleteObject(iconinfo.hbmMask); Skybound.Drawing.Design.IconFile.DeleteObject(iconinfo.hbmColor); } else { bitmap1 = icon.ToBitmap(); } } return(bitmap1); }
private static extern int GetObjectBitmap(System.IntPtr hObject, int nCount, ref Skybound.Drawing.Design.IconFile.BITMAP lpObject);