Example #1
0
        private Gdi32.HBITMAP GetCompatibleBitmap(Bitmap bm)
        {
            using var screenDC = User32.GetDcScope.ScreenDC;

            // GDI+ returns a DIBSECTION based HBITMAP. The clipboard deals well
            // only with bitmaps created using CreateCompatibleBitmap(). So, we
            // convert the DIBSECTION into a compatible bitmap.
            Gdi32.HBITMAP hBitmap = bm.GetHBITMAP();

            // Create a compatible DC to render the source bitmap.
            using var sourceDC = new Gdi32.CreateDcScope(screenDC);
            using var sourceBitmapSelection = new Gdi32.SelectObjectScope(sourceDC, hBitmap);

            // Create a compatible DC and a new compatible bitmap.
            using var destinationDC = new Gdi32.CreateDcScope(screenDC);
            Gdi32.HBITMAP bitmap = Gdi32.CreateCompatibleBitmap(screenDC, bm.Size.Width, bm.Size.Height);

            // Select the new bitmap into a compatible DC and render the blt the original bitmap.
            using var destinationBitmapSelection = new Gdi32.SelectObjectScope(destinationDC, bitmap);
            Gdi32.BitBlt(
                destinationDC,
                0,
                0,
                bm.Size.Width,
                bm.Size.Height,
                sourceDC,
                0,
                0,
                Gdi32.ROP.SRCCOPY);

            return(bitmap);
        }
 internal static Graphics CreateGraphics(this Gdi32.CreateDcScope hdc) => Graphics.FromHdcInternal(hdc.HDC.Handle);
 internal static Color GetNearestColor(this Gdi32.CreateDcScope hdc, Color color)
 => GetNearestColor(hdc.HDC, color);
 internal static void DrawLine(this Gdi32.CreateDcScope hdc, Gdi32.HPEN pen, int x1, int y1, int x2, int y2)
 => DrawLine(hdc.HDC, pen, x1, y1, x2, y2);
 internal static void FillRectangle(this Gdi32.CreateDcScope hdc, Rectangle rectangle, Gdi32.HBRUSH hbrush)
 => FillRectangle(hdc.HDC, rectangle, hbrush);
 internal static void DrawRectangle(this Gdi32.CreateDcScope hdc, Rectangle rectangle, Gdi32.HPEN hpen)
 => DrawRectangle(hdc.HDC, rectangle, hpen);