2

I have this code snippet below:

 int screenLeft = (int)SystemParameters.VirtualScreenLeft;
 int screenTop = (int)SystemParameters.VirtualScreenTop;
 int screenWidth = (int)SystemParameters.VirtualScreenWidth;
 int screenHeight = (int)SystemParameters.VirtualScreenHeight;
 
 Bitmap bitmap_Screen = new Bitmap(screenWidth, screenHeight);
 Graphics g = Graphics.FromImage(bitmap_Screen);
 g.CopyFromScreen(screenLeft, screenTop, 0, 0, bitmap_Screen.Size);
 
 if (!Directory.Exists(screenshotDir))
 {
     Directory.CreateDirectory(screenshotDir);
 }
 bitmap_Screen.Save(fileLoc);
 

I'm trying to take a snapshot of the entire desktop. It's just that VirtualScreenWidth and VirtualScreenHeight returns 1536 and 800 respectively, when my desktop size is 1920 x 1080. So the snapshot is just capturing a portion of the screen and not an entire screencap

I already found the exact same issue here Screen Resolution Problem In WPF? but being new to WPF and C# in general, I don't understand the selected answer.

1 Answers1

1

The answer to your problem is actually in the answer to the question you linked. The dimension you're getting is not pixels, but the WPF "units" that are 1/96". I'm guessing much like in the answer there points out, your screen is at 125% scaling right now, so the formula is exactly the same. 1536 / 96 * 120 = 1920 pixels.

The issue is that you're taking this WPF unit measurement and passing it into a method that needs actual pixels, because it's working not with a WPF element, but the generic Windows screen space. If you also get your current DPI and do the proper math on the values you get from the VirtualScreenWidth and other methods you should get a proper screenshot.

Logarr
  • 2,120
  • 1
  • 17
  • 29