Exemplo n.º 1
0
        public static Bitmap GetBitmapForIcon(Icon icon)
        {
            using (IconInfo iconInfo = new IconInfo(icon))
            {
                if (!iconInfo.Info.fIcon)
                    return null; // this is a cursor!

                if (iconInfo.BitmapStruct.bmBitsPixel!=32)
                    return Bitmap.FromHicon(icon.Handle);  // no alpha blending, so use regular .Net

                Bitmap iconBitmap = Bitmap.FromHbitmap(iconInfo.Info.hbmColor);

                // get broken bitmap (broken - pixelformat doesn't specify alpha channel, even though the alpha data is there),
                // copy to a new bitmap with a healthy (includes the alpha channel) pixelformat
                Bitmap transparentBitmap;
                using (LockedBitMap lockedBitmap = new LockedBitMap(iconBitmap))
                {
                    transparentBitmap = new Bitmap(lockedBitmap.Data.Width, lockedBitmap.Data.Height, lockedBitmap.Data.Stride, PixelFormat.Format32bppArgb, lockedBitmap.Data.Scan0);
                }
                return transparentBitmap;
            }
        }
Exemplo n.º 2
0
        public static Bitmap GetBitmapForIcon(Icon icon)
        {
            using (IconInfo iconInfo = new IconInfo(icon))
            {
                if (!iconInfo.Info.fIcon)
                {
                    return(null);                    // this is a cursor!
                }
                if (iconInfo.BitmapStruct.bmBitsPixel != 32)
                {
                    return(Bitmap.FromHicon(icon.Handle));                     // no alpha blending, so use regular .Net
                }
                Bitmap iconBitmap = Bitmap.FromHbitmap(iconInfo.Info.hbmColor);

                // get broken bitmap (broken - pixelformat doesn't specify alpha channel, even though the alpha data is there),
                // copy to a new bitmap with a healthy (includes the alpha channel) pixelformat
                Bitmap transparentBitmap;
                using (LockedBitMap lockedBitmap = new LockedBitMap(iconBitmap))
                {
                    transparentBitmap = new Bitmap(lockedBitmap.Data.Width, lockedBitmap.Data.Height, lockedBitmap.Data.Stride, PixelFormat.Format32bppArgb, lockedBitmap.Data.Scan0);
                }
                return(transparentBitmap);
            }
        }