Answer the question
In order to leave comments, you need to log in
How to take a screenshot of a hidden part of the screen?
I have a program running through selenium webdriver firefox and just launching chrome with my extension.
So that all this does not bother me, I hide the windows of these programs, for this I turn to the methods:
class Viewer
{
public const int SW_HIDE = 0;
public const int SW_SHOW = 5;
public const int SW_Min = 2;
public const int SW_Max = 3;
public const int SW_Norm = 4;
public const int WM_CLOSE = 0x10;
[DllImport("kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
public static extern int FindWindow(string ClassName, string WindowsName);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern IntPtr SendMessage(IntPtr handle, UInt32 message, IntPtr w, IntPtr l);
public static void CloseWindow(IntPtr handle)
{
SendMessage(handle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
}
driver = new FirefoxDriver(ffds, opt);
wname = Viewer.FindWindow("MozillaWindowClass", "Mozilla Firefox");
IntPtr hh = new IntPtr(wname);
Viewer.ShowWindow(hh, Viewer.SW_HIDE);
Screenshot screen = driver.GetScreenshot();
screen.SaveAsFile(@"D:\logs\screen.jpg", ScreenshotImageFormat.Jpeg)
Process proc = new Process();
proc.StartInfo.FileName ="chrome.exe";
proc.Start();
int wname = Viewer.FindWindow("Chrome_WidgetWin_1", "Новая вкладка - Google Chrome");
IntPtr name = new IntPtr(wname)
Viewer.ShowWindow(name, Viewer.SW_SHOW);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr handle, ref Rectangle rect);
public static void CaptureWindow(IntPtr handle)
{
// Get the size of the window to capture
Rectangle rect = new Rectangle();
GetWindowRect(handle, ref rect);
// GetWindowRect returns Top/Left and Bottom/Right, so fix it
rect.Width = rect.Width - rect.X;
rect.Height = rect.Height - rect.Y;
// Create a bitmap to draw the capture into
using (Bitmap bitmap = new Bitmap(rect.Width, rect.Height))
{
// Use PrintWindow to draw the window into our bitmap
using (Graphics g = Graphics.FromImage(bitmap))
{
IntPtr hdc = g.GetHdc();
if (!PrintWindow(handle, hdc, 0))
{
int error = Marshal.GetLastWin32Error();
var exception = new System.ComponentModel.Win32Exception(error);
Debug.WriteLine("ERROR: " + error + ": " + exception.Message);
// TODO: Throw the exception?
}
g.ReleaseHdc(hdc);
}
// Save it as a .png just to demo this
bitmap.Save(@"D:\logs\screen.png");
}
}
private void button8_Click(object sender, EventArgs e)
{
Thread tr10 = new Thread(() =>
{
IntPtr hwnd = new IntPtr(Wiq.wname);
Viewer.CaptureWindow(hwnd);
});
tr10.Start();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question