Ejemplo n.º 1
0
        private void WithTransparentCanvas(Rectangle rect, byte alpha, bool copySource, Action <Win32Canvas> action)
        {
            if (rect.Width <= 0 || rect.Height <= 0 || alpha == 0)
            {
                return;
            }

            IntPtr memoryHdc = Gdi32API.CreateCompatibleDCChecked(hdc);

            try
            {
                IntPtr memoryBitmap = Gdi32API.CreateCompatibleBitmapChecked(hdc, rect.Width, rect.Height);
                try
                {
                    var originalBitmap = Gdi32API.SelectObjectChecked(memoryHdc, memoryBitmap);
                    try
                    {
                        if (copySource)
                        {
                            Gdi32API.BitBlt(memoryHdc, 0, 0, rect.Width, rect.Height, hdc, rect.X, rect.Y, GDI32RasterOperation.SRCCOPY);
                        }

                        using (Win32Canvas memoryCanvas = new Win32Canvas(memoryHdc, offset, objectCache))
                        {
                            action(memoryCanvas);
                        }

                        Gdi32API.GdiAlphaBlend
                        (
                            hdc,
                            rect.X, rect.Y, rect.Width, rect.Height,
                            memoryHdc,
                            0, 0, rect.Width, rect.Height,
                            BLENDFUNCTION.ConstantAlpha(alpha)
                        );
                    }
                    finally
                    {
                        Gdi32API.SelectObjectChecked(memoryHdc, originalBitmap);
                    }
                }
                finally
                {
                    Gdi32API.DeleteObject(memoryBitmap);
                }
            }
            finally
            {
                Gdi32API.DeleteDC(memoryHdc);
            }
        }
Ejemplo n.º 2
0
 public void CopyTo(IntPtr hdc, int x, int y)
 {
     Gdi32API.BitBlt(hdc, x, y, width, height, bitmapHdc, 0, 0, GDI32RasterOperation.SRCCOPY);
 }