Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take screenshot in process window

Tags:

c#

I am trying to make a screenshot to the window of a process in Windows 7 64 bit, the problem is that I always get error in the following line:

var bmp = new Bitmap (width, height, PixelFormat.Format32bppArgb);

Saying "invalid parameters", I made a throw to see the errors and width and height are always 0.

Before in 32 bits it worked well, but now in 64 bits it does not work anymore.

The code :

public void CaptureApplication()
{
    string procName = "firefox";

    var proc = Process.GetProcessesByName(procName)[0];
    var rect = new User32.Rect();
    User32.GetWindowRect(proc.MainWindowHandle, ref rect);

    int width = rect.right - rect.left;
    int height = rect.bottom - rect.top;

    var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
    Graphics graphics = Graphics.FromImage(bmp);
    graphics.CopyFromScreen(rect.left, rect.top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);

    bmp.Save("c:\\tmp\\test.png", ImageFormat.Png);
}

private class User32
{
    [StructLayout(LayoutKind.Sequential)]
    public struct Rect
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    }

    [DllImport("user32.dll")]
    public static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
}

How do I fix this error?

like image 437
Chris Avatar asked Oct 26 '17 14:10

Chris


People also ask

How do I take a screenshot of just the active window?

Alt + Print Screen To take a quick screenshot of the active window, use the keyboard shortcut Alt + PrtScn. This will snap your currently active window and copy the screenshot to the clipboard. You'll need to open the shot in an image editor to save it.

How do I take a screenshot of a specific window in Windows 10?

Press “Windows + Shift + S”. Your screen will appear grayed out and your mouse cursor will change. Click and drag on your screen to select the part of your screen you want to capture. A screenshot of the screen region you selected will be copied to your clipboard.

How do I take a screenshot on the command window?

First off all open cmd in full screen mode then click on print screen button after that open paint brush and press ctrl + v (past) you can save it in any where, where ever you want (file type should be . png).

What is the shortcut key to take a screenshot in Windows 10?

Depending on your hardware, you may use the Windows Logo Key + PrtScn button as a shortcut for print screen. If your device does not have the PrtScn button, you may use Fn + Windows logo key + Space Bar to take a screenshot, which can then be printed.


1 Answers

Getting the process of firefox returns an array of processes and you are looking for the one with an rectangle with realistic sizes which is the main process.

Process[] procs = Process.GetProcessesByName(procName);
var rect = new User32.Rect();
int width = 0;
int height = 0:
foreach (Process proc in procs)
{
    User32.GetWindowRect(proc.MainWindowHandle, ref rect);
    width = rect.right - rect.left;
    height = rect.bottom - rect.top;
    // break foreach if an realistic rectangle found => main process found
    if (width != 0 && height != 0)
    {
        break;
    }
}
like image 105
M. Schena Avatar answered Oct 24 '22 19:10

M. Schena