We have a large WinForm C# .Net 4.6 program which from time to time needs to do obtain screen captures for debugging purposes. We currently use this code:
private static void DoScreenCapture(string filename)
{
// Determine the size of the "virtual screen", including all monitors.
int screenLeft = SystemInformation.VirtualScreen.Left;
int screenTop = SystemInformation.VirtualScreen.Top;
int screenWidth = SystemInformation.VirtualScreen.Width;
int screenHeight = SystemInformation.VirtualScreen.Height;
// Create a bitmap of the appropriate size to receive the screenshot.
using (Bitmap bmp = new Bitmap(screenWidth, screenHeight))
{
// Draw the screenshot into our bitmap.
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(screenLeft, screenTop, 0, 0, bmp.Size);
}
// Stuff the bitmap into a file
bmp.Save(filename, Imaging.ImageFormat.Png);
}
}
This code does all that we want, except when the user has scaled his monitors.
I've looked at bunches of Stack Overflow articles. Most of them provide code like we already have, but that doesn't handle the monitor scaling issue. For example:
Take screenshot of multiple desktops of all visible applications and forms
Some Stack Overflow articles indicate that making our application DPI aware would solve the problem. Yes, it would, but that's more than we can tackle today. For example:
Windows screenshot with scaling
There is also code which will do a capture for all monitors one at a time, but we much prefer to have all the monitors captured in the same image.
Can someone give me a C# code snippet which will take a screenshot of multiple monitors which have varied scaling factors?
For example, if I have three identical 1920x1080 monitors and arrange them left to right with the leftmost monitor at 175%, the center monitor at 100%, and the rightmost monitor at 150%, then this would be the screenshot that I want:
But this is the screenshot that my current code produces. Note that the rightmost monitor is missing a piece on the far right.