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);
public static Bitmap CreateAlphaBitmap(byte alpha, Bitmap original) { if (alpha < 100) { Bitmap b = new Bitmap(original.Width, original.Height); using (Graphics gxSrc = Graphics.FromImage(b)) using (Graphics resultGx = Graphics.FromImage(original)) { gxSrc.Clear(Color.Black); IntPtr hdcDst = resultGx.GetHdc(); IntPtr hdcSrc = gxSrc.GetHdc(); BlendFunction blendFunction = new BlendFunction(); blendFunction.BlendOp = 0; // Only supported blend operation blendFunction.BlendFlags = 0; // Documentation says put 0 here blendFunction.SourceConstantAlpha = 50; // Constant alpha factor blendFunction.AlphaFormat = 0; // Don't look for per pixel alpha Win32.AlphaBlend(hdcDst, 0, 0, original.Width, original.Height, hdcSrc, 0, 0, original.Width, original.Height, blendFunction); gxSrc.ReleaseHdc(hdcSrc); // Required cleanup to GetHdc() resultGx.ReleaseHdc(hdcDst); // Required cleanup to GetHdc() } b.Dispose(); } return original; }
public static Bitmap CreateSnapshot(byte alpha, Point startPosition) { Rectangle rect = Screen.PrimaryScreen.Bounds; rect = new Rectangle(startPosition.X, startPosition.Y, rect.Width - startPosition.X, rect.Height - startPosition.Y); var result = new Bitmap(rect.Width, rect.Height); IntPtr deviceContext = Win32.GetDC(IntPtr.Zero); try { using (Graphics resultGx = Graphics.FromImage(result)) { var resultHdc = resultGx.GetHdc(); Win32.BitBlt(resultHdc, 0, 0, rect.Width, rect.Height, deviceContext, startPosition.X, startPosition.Y, TernaryRasterOperations.SRCCOPY); resultGx.ReleaseHdc(resultHdc); } } finally { Win32.ReleaseDC(IntPtr.Zero, deviceContext); } if (alpha < 100) { //result = AdjustBrightness(result, (decimal)alpha); Bitmap b = new Bitmap(rect.Width, result.Height); using (Graphics gxSrc = Graphics.FromImage(b)) using (Graphics resultGx = Graphics.FromImage(result)) { gxSrc.Clear(Color.Black); IntPtr hdcDst = resultGx.GetHdc(); IntPtr hdcSrc = gxSrc.GetHdc(); BlendFunction blendFunction = new BlendFunction(); blendFunction.BlendOp = 0; // Only supported blend operation blendFunction.BlendFlags = 0; // Documentation says put 0 here blendFunction.SourceConstantAlpha = 50; // Constant alpha factor blendFunction.AlphaFormat = 0; // Don't look for per pixel alpha Win32.AlphaBlend(hdcDst, 0, 0, rect.Width, rect.Height, hdcSrc, 0, 0, rect.Width, rect.Height, blendFunction); gxSrc.ReleaseHdc(hdcSrc); // Required cleanup to GetHdc() resultGx.ReleaseHdc(hdcDst); // Required cleanup to GetHdc() } } return result; }
/// <summary> /// Draw an image with full transparency /// </summary> /// <param name="hdc"></param> /// <param name="transp">Constant alpha factor</param> /// <param name="x">X coord to draw the image</param> /// <param name="y">Y coord to draw the image</param> /// <returns></returns> private int DrawAlpha(IntPtr hdc, byte transp, int x, int y, int width, int height) { var blendFunction = new BlendFunction { BlendOp = ((byte)BlendOperation.AC_SRC_OVER), // Only supported blend operation BlendFlags = 0, // Documentation says put 0 here SourceConstantAlpha = transp, // Constant alpha factor AlphaFormat = (byte)0 }; return Win32.AlphaBlend(_hdc, x, y, width, height, hdc, 0, 0, width, height, blendFunction); }