Answer the question
In order to leave comments, you need to log in
How to take a screenshot of an inactive window?
Hello.
I need to take a screenshot in the window handle at the specified coordinates (the window itself can be minimized)
I have a more or less working example, but in the game I need (GTA 5) it takes a screenshot of just a black square.
Here is my current code:
public static void CaptureWindow(int posX, int posY, int wWidth, int wHeight)
{
RECT rect;
GetWindowRect(hWnd, out rect);
using (var image = new Bitmap(rect.Right - rect.Left, rect.Bottom - rect.Top))
{
using (var graphics = Graphics.FromImage(image))
{
var hdcBitmap = graphics.GetHdc();
PrintWindow(hWnd, hdcBitmap, 0);
graphics.ReleaseHdc(hdcBitmap);
}
var newImage = new Bitmap(wWidth, wHeight);
int newY = -1;
int newX = -1;
for (int y = posY; y < posY + wHeight; y++)
{
newY++;
for (int x = posX; x < posX + wWidth; x++)
{
newX++;
newImage.SetPixel(newX, newY, image.GetPixel(x, y));
if (newX == wWidth - 1)
{
newX = -1;
}
}
}
image.Save("Example1.jpg");
newImage.Save("Example.jpg");
}
}
Answer the question
In order to leave comments, you need to log in
Switch your game to borderless window or window mode to start.
In a full-fledged fullscreen, you can take a screenshot ONLY when the window is active. And for this you have to hook directx, your code will not work.
Many games that support folding stop rendering at this point. Even in windowed mode. This must also be taken into account, and there are no workarounds in this case.
When Direct3d enters full screen mode, it gets exclusive direct access to the graphics card (that is, bypassing the OS), so the standard operating system screenshot tools won't work. The OS has no idea what is drawn inside the window. To get a screenshot, you need to hook onto the renderer.
Taking the screenshot itself is relatively straightforward. Once the scene has been rendered, the Direct3D device offers a GetRenderTarget function that returns a pointer to a Direct3D surface with the current scene. Using a SurfaceToBitmap function, the RenderTarget can be converted to a Bitmap object, which can then be saved as either a JPEG or PNG to disk. Below is an example of the code involved:
Bitmap screenshot = Funcs.SurfaceToBitmap(device.GetRenderTarget(0));
screenshot.Save(Frame.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
screenshot.Dispose();
Eh guys! I thought I'd read something interesting now! The author of the question meant maybe not a window minimized to tray (in this case, yes, all rendering stops and the game freezes), but simply an inactive window in the background of the screen (the game is not in full screen, but in window screen). In this case, many games continue rendering in their own window, even though the user does not see it. I think - it's all about some sort of window switch flag (active-inactive). For the Axis, all these windows are active, processes are running in each, it’s just that this or that window is pushed to the foreground by the user
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question