Ejemplo n.º 1
0
        public void DrawImage(INativeImage image, int x, int y)
        {
            x += offset.X;
            y += offset.Y;

            // todo: allow null?
            Win32Image win32Image = (Win32Image)image;

            var    bitmap = Gdi32API.CreateDIBSectionChecked(hdc, new BITMAPINFO(win32Image.Width, win32Image.Height), out IntPtr buffer);
            IntPtr hdcMem = IntPtr.Zero;

            try
            {
                Marshal.Copy(win32Image.Pixels, 0, buffer, win32Image.Pixels.Length);
                hdcMem = Gdi32API.CreateCompatibleDCChecked(hdc);
                var oldBitmap = Gdi32API.SelectObjectChecked(hdcMem, bitmap);

                Gdi32API.GdiAlphaBlend
                (
                    hdc,
                    x, y, win32Image.Width, win32Image.Height,
                    hdcMem,
                    0, 0, win32Image.Width, win32Image.Height,
                    BLENDFUNCTION.SourceAlpha()
                );

                Gdi32API.SelectObjectChecked(hdcMem, oldBitmap);
            }
            finally
            {
                Gdi32API.SafeDeleteObject(hdcMem);
                Gdi32API.DeleteObject(bitmap);
            }
        }
Ejemplo n.º 2
0
        public static Win32Bitmap Create(IntPtr hdc, int width, int height)
        {
            IntPtr bitmapPtr = IntPtr.Zero;
            IntPtr bitmapHdc = IntPtr.Zero;

            try
            {
                bitmapPtr = Gdi32API.CreateCompatibleBitmapChecked(hdc, width, height);
                bitmapHdc = Gdi32API.CreateCompatibleDCChecked(hdc);
                Gdi32API.SelectObjectChecked(bitmapHdc, bitmapPtr);

                var bitmap = new Win32Bitmap(bitmapPtr, bitmapHdc, width, height);
                bitmapPtr = IntPtr.Zero;
                bitmapHdc = IntPtr.Zero;
                return(bitmap);
            }
            finally
            {
                if (bitmapHdc != IntPtr.Zero)
                {
                    Gdi32API.DeleteDC(bitmapHdc);
                }

                if (bitmapPtr != IntPtr.Zero)
                {
                    Gdi32API.DeleteObject(bitmapPtr);
                }
            }
        }
Ejemplo n.º 3
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.º 4
0
        public void SetClipRectangle(int x, int y, int width, int height)
        {
            x += offset.X;
            y += offset.Y;

            var region = Gdi32API.CreateRectRgnChecked(x, y, x + width, y + height);

            try
            {
                Gdi32API.SelectClipRgn(hdc, region);
            }
            finally
            {
                Gdi32API.DeleteObject(region);
            }
        }
Ejemplo n.º 5
0
        private void FillOpaqueRectangleGDI(Color color, int x, int y, int width, int height)
        {
            // todo: check that width and height are exact
            var region = Gdi32API.CreateRectRgnChecked(x, y, x + width, y + height);

            try
            {
                // todo: pick FillRgn or PaintRgn
                Gdi32API.FillRgn(hdc, region, objectCache.GetSolidBrush(color));

                // SelectSolidBrush(color);
                // Gdi32API.PaintRgn(hdc, region);
            }
            finally
            {
                Gdi32API.DeleteObject(region);
            }
        }
Ejemplo n.º 6
0
 public void Dispose()
 {
     Gdi32API.DeleteDC(bitmapHdc);
     Gdi32API.DeleteObject(bitmapPtr);
 }