public static extern Int32 AlphaBlend(IntPtr hdcDest, Int32 xDest, Int32 yDest, Int32 cxDest, Int32 cyDest, IntPtr hdcSrc, Int32 xSrc, Int32 ySrc, Int32 cxSrc, Int32 cySrc, BlendFunction blendFunction);
/// <summary> /// Draws an image with transparency. Source must be 32bpp (or 24bpp) /// </summary> /// <param name="gx">Graphics to drawn on.</param> /// <param name="image">Image to draw.</param> /// <param name="transparency">Transparency constant</param> /// <param name="x">X location</param> /// <param name="y">Y location</param> public static void DrawAlpha(this Graphics gx, Bitmap image, byte transparency, int x, int y) { using (Graphics gxSrc = Graphics.FromImage(image)) { IntPtr hdcDst = gx.GetHdc(); IntPtr hdcSrc = gxSrc.GetHdc(); BlendFunction blendFunction = new BlendFunction(); blendFunction.BlendOp = (byte)BlendOperation.AC_SRC_OVER; // Only supported blend operation blendFunction.BlendFlags = (byte)BlendFlags.Zero; // Documentation says put 0 here blendFunction.SourceConstantAlpha = transparency; // Constant alpha factor blendFunction.AlphaFormat = (byte)0; // Don't look for per pixel alpha Win32Helper.AlphaBlend(hdcDst, x, y, image.Width, image.Height, hdcSrc, 0, 0, image.Width, image.Height, blendFunction); gx.ReleaseHdc(hdcDst); // Required cleanup to GetHdc() gxSrc.ReleaseHdc(hdcSrc); // Required cleanup to GetHdc() } }