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);
            }
        }