Created
July 20, 2023 09:09
-
-
Save DefaultO/321c4c965d9409d03c95ddec088aa390 to your computer and use it in GitHub Desktop.
Take Screenshot of Fullscreen Game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.Runtime.InteropServices; | |
using System.Threading; | |
using static System.Net.Mime.MediaTypeNames; | |
class Program | |
{ | |
private const int SM_XVIRTUALSCREEN = 76; | |
private const int SM_YVIRTUALSCREEN = 77; | |
private const int SM_CXVIRTUALSCREEN = 78; | |
private const int SM_CYVIRTUALSCREEN = 79; | |
private const int SRCCOPY = 0x00CC0020; | |
private const int CAPTUREBLT = 0x40000000; | |
[DllImport("user32.dll")] | |
private static extern int GetSystemMetrics(int nIndex); | |
[DllImport("user32.dll")] | |
private static extern IntPtr GetDC(int hWnd); | |
[DllImport("gdi32.dll")] | |
private static extern IntPtr CreateCompatibleDC(IntPtr hdc); | |
[DllImport("gdi32.dll")] | |
private static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight); | |
[DllImport("gdi32.dll")] | |
private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj); | |
[DllImport("gdi32.dll")] | |
private static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSrc, int xSrc, int ySrc, int dwRop); | |
[DllImport("gdi32.dll")] | |
private static extern bool DeleteObject(IntPtr hObject); | |
[DllImport("user32.dll")] | |
private static extern int ReleaseDC(IntPtr hWnd, IntPtr hdc); | |
static void Main() | |
{ | |
Console.ReadLine(); | |
Thread.Sleep(5000); | |
int x = GetSystemMetrics(SM_XVIRTUALSCREEN); // left (e.g. -1024) | |
int y = GetSystemMetrics(SM_YVIRTUALSCREEN); // top (e.g. -34) | |
int cx = GetSystemMetrics(SM_CXVIRTUALSCREEN); // entire width (e.g. 2704) | |
int cy = GetSystemMetrics(SM_CYVIRTUALSCREEN); // entire height (e.g. 1050) | |
IntPtr dcScreen = GetDC((int)IntPtr.Zero); | |
IntPtr dcTarget = CreateCompatibleDC(dcScreen); | |
IntPtr bmpTarget = CreateCompatibleBitmap(dcScreen, cx, cy); | |
IntPtr oldBmp = SelectObject(dcTarget, bmpTarget); | |
BitBlt(dcTarget, 0, 0, cx, cy, dcScreen, x, y, SRCCOPY | CAPTUREBLT); | |
SelectObject(dcTarget, oldBmp); | |
// Save the screenshot as an image file | |
using (Bitmap screenshot = System.Drawing.Image.FromHbitmap(bmpTarget)) | |
{ | |
screenshot.Save("D:/screenshot.png", System.Drawing.Imaging.ImageFormat.Png); | |
} | |
// Cleanup | |
DeleteObject(bmpTarget); | |
DeleteObject(oldBmp); | |
DeleteObject(dcTarget); | |
ReleaseDC(IntPtr.Zero, dcScreen); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment