public AlphaLayerControl(Graphics parentGraphics, Size size, byte alphaVal) { try { _alphaImage = new Bitmap(size.Width, size.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb565); using (Graphics gx = Graphics.FromImage(_alphaImage)) { IntPtr hdcDst = gx.GetHdc(); IntPtr hdcSrc = parentGraphics.GetHdc(); Win32.BlendFunction blendFunction = new Win32.BlendFunction(); blendFunction.BlendOp = (byte)Win32.BlendOperation.AC_SRC_OVER; // Only supported blend operation blendFunction.BlendFlags = (byte)Win32.BlendFlags.Zero; // Documentation says put 0 here blendFunction.SourceConstantAlpha = alphaVal; // Constant alpha factor blendFunction.AlphaFormat = (byte)0; // Don't look for per pixel alpha Win32.AlphaBlend(hdcDst, 0, 0, _alphaImage.Width, _alphaImage.Height, hdcSrc, 0, 0, _alphaImage.Width, _alphaImage.Height, blendFunction); gx.ReleaseHdc(hdcDst); // Required cleanup to GetHdc() parentGraphics.ReleaseHdc(hdcSrc); // Required cleanup to GetHdc() } } catch { GC.Collect(); _alphaImage = new Bitmap(1, 1); } }
public AlphaLayerControl(Graphics parentGraphics, Size size, byte alphaVal) { try { _alphaImage = new Bitmap(size.Width, size.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb565); using (Graphics gx = Graphics.FromImage(_alphaImage)) { IntPtr hdcDst = gx.GetHdc(); IntPtr hdcSrc = parentGraphics.GetHdc(); Win32.BlendFunction blendFunction = new Win32.BlendFunction(); blendFunction.BlendOp = (byte)Win32.BlendOperation.AC_SRC_OVER; // Only supported blend operation blendFunction.BlendFlags = (byte)Win32.BlendFlags.Zero; // Documentation says put 0 here blendFunction.SourceConstantAlpha = alphaVal;// Constant alpha factor blendFunction.AlphaFormat = (byte)0; // Don't look for per pixel alpha Win32.AlphaBlend(hdcDst, 0, 0, _alphaImage.Width, _alphaImage.Height, hdcSrc, 0, 0, _alphaImage.Width, _alphaImage.Height, blendFunction); gx.ReleaseHdc(hdcDst); // Required cleanup to GetHdc() parentGraphics.ReleaseHdc(hdcSrc); // Required cleanup to GetHdc() } } catch { GC.Collect(); _alphaImage = new Bitmap(1, 1); } }