Exemplo n.º 1
0
 public static Bitmap FromHbitmapWithAlpha(IntPtr hbm)
 {
     if (hbm == IntPtr.Zero)
     {
         throw new ArgumentException();
     }
     Microsoft.Win32.BITMAP lpObject = new Microsoft.Win32.BITMAP();
     if (Windows.GetObjectBitmap(hbm, Marshal.SizeOf(lpObject), ref lpObject) != 0)
     {
         Bitmap bitmap2;
         int bmWidth = lpObject.bmWidth;
         int bmHeight = lpObject.bmHeight;
         if ((lpObject.bmBits != IntPtr.Zero) && (lpObject.bmBitsPixel == 0x20))
         {
             bitmap2 = new Bitmap(bmWidth, bmHeight, PixelFormat.Format32bppArgb);
             BitmapData bitmapdata = bitmap2.LockBits(new Rectangle(0, 0, bitmap2.Width, bitmap2.Height), ImageLockMode.WriteOnly, bitmap2.PixelFormat);
             Debug.Assert(lpObject.bmWidthBytes == bitmapdata.Stride);
             byte[] destination = new byte[bitmapdata.Stride];
             IntPtr bmBits = lpObject.bmBits;
             IntPtr ptr2 = bitmapdata.Scan0;
             for (int i = 0; i < bmHeight; i++)
             {
                 Marshal.Copy(bmBits, destination, 0, destination.Length);
                 Marshal.Copy(destination, 0, ptr2, destination.Length);
                 bmBits = bmBits.Offset(destination.Length);
                 ptr2 = ptr2.Offset(destination.Length);
             }
             bitmap2.UnlockBits(bitmapdata);
             return bitmap2;
         }
         if (OS.IsWin2k)
         {
             bitmap2 = new Bitmap(bmWidth, bmHeight, PixelFormat.Format32bppPArgb);
             using (Graphics graphics = Graphics.FromImage(bitmap2))
             {
                 IntPtr hdc = graphics.GetHdc();
                 IntPtr ptr4 = Windows.CreateCompatibleDC(hdc);
                 IntPtr hgdiobj = Windows.SelectObject(ptr4, hbm);
                 BLENDFUNCTION blendFunction = new BLENDFUNCTION(0, 0, 0xff, 1);
                 Windows.AlphaBlend(hdc, 0, 0, bmWidth, bmHeight, ptr4, 0, 0, bmWidth, bmHeight, blendFunction);
                 Windows.SelectObject(ptr4, hgdiobj);
                 Windows.DeleteDC(ptr4);
                 graphics.ReleaseHdc(hdc);
             }
             return bitmap2;
         }
     }
     return Image.FromHbitmap(hbm);
 }
Exemplo n.º 2
0
 public static extern bool AlphaBlend(IntPtr hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest, IntPtr hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, BLENDFUNCTION blendFunction);