How to get Bitmap from CreateCompatibleBitmap?

mc 5,426 Reputation points
2023-11-02T09:35:14.8933333+00:00

I want to capture the screen to bitmap.

[DllImport("gdi32.dll")]
public extern static IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll")]
public extern static IntPtr CreateCompatibleBitmap(IntPtr hdc, Int32 width, Int32 height);
var hdc = CreateCompatibleDC(0);
var bitmap = CreateCompatibleBitmap(GetDC(0), 1920, 1080);
SelectObject(hdc, bitmap);
BitBlt(hdc, 0, 0, 0, 0, GetDC(0), 1920, 1080, 0);

and then how to get bitmap from bitmap?

Developer technologies | Windows Presentation Foundation
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2023-11-02T10:12:21.9166667+00:00

    Hi,@mc . Welcome to Microsoft Q&A Forum.

    To capture the screen to a bitmap in C# using the GDI functions, you can follow the steps you've outlined. However, after capturing the screen to a device context (HDC), you could copy it into a .NET Bitmap object for further manipulation.

    To display a Bitmap in a WPF application, you can use the System.Windows.Controls.Image control. You'll need to convert the Bitmap to a WPF BitmapSource first.

    
     <StackPanel Name="stackPanel">
            <Button Content="capture" Click="Button_Click"/>
        </StackPanel>
    

    Codebehind:

    
    
    
    using System;
    
    using System.Drawing;
    
    using System.Runtime.InteropServices;
    
    using System.Windows;
    
    using System.Windows.Interop;
    
    using System.Windows.Media.Imaging;
    
    using Image = System.Drawing.Image;
    
    
    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            [DllImport("gdi32.dll")]
            public extern static IntPtr CreateCompatibleDC(IntPtr hdc);
    
            [DllImport("gdi32.dll")]
            public extern static IntPtr CreateCompatibleBitmap(IntPtr hdc, int width, int height);
    
            [DllImport("gdi32.dll")]
            public extern static IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
    
            [DllImport("gdi32.dll")]
            public extern static int BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
    
            [DllImport("user32.dll")]
            public static extern IntPtr GetDC(IntPtr hWnd);
    
            [DllImport("user32.dll")]
            public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
    
            [DllImport("gdi32.dll")]
            public static extern int DeleteObject(IntPtr hObject);
    
            public static Bitmap CaptureScreen(int width, int height)
            {
                IntPtr hdc = GetDC(IntPtr.Zero);
                IntPtr hdcMem = CreateCompatibleDC(hdc);
                IntPtr hBitmap = CreateCompatibleBitmap(hdc, width, height);
                SelectObject(hdcMem, hBitmap);
                BitBlt(hdcMem, 0, 0, width, height, hdc, 0, 0, 0x00CC0020); // SRCCOPY
    
                Bitmap bitmap = Image.FromHbitmap(hBitmap);
                DeleteObject(hBitmap);
                ReleaseDC(IntPtr.Zero, hdc);
                DeleteObject(hdcMem);
    
                return bitmap;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                int screenWidth = 1920;  // Your screen width
                int screenHeight = 1080; // Your screen height
    
                Bitmap screenCapture = CaptureScreen(screenWidth, screenHeight);
    
             
                BitmapSource bitmapSource = ConvertBitmapToBitmapSource(screenCapture);
    
                System.Windows.Controls.Image imageControl = new System.Windows.Controls.Image();
                imageControl.Source = bitmapSource;
    
                stackPanel.Children.Add(imageControl);
    
            }
    
            public static BitmapSource ConvertBitmapToBitmapSource(Bitmap bitmap)
            {
                IntPtr hBitmap = bitmap.GetHbitmap();
                BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                DeleteObject(hBitmap);
                return bitmapSource;
            }
    
           
         
        }
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.