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.