1

I have a C# application Winforms .Net 3.5 that takes a screenshot of itself. When running under Windows 7, it works fine, but under Windows 10, the screenshot is offset from the window. With 100% magnification, the screenshot is just a little larger than the window, showing a few pixels of the background on the sides and bottom. But for larger magnifications, it shows a frame that is offset to the upper left by an amount that increases with the magnification level. Since this application needs to be run on laptops where the magnification is necessary (particularly for those of us with old eyes), this is a significant issue.

The code taking the screenshot is

Rectangle bounds = this.Bounds;
using (Bitmap ScreenShot = new Bitmap(bounds.Width, bounds.Height)) 
{
    // code hiding a few controls on the form (control.Visible = false)
    this.Update();
    using (Graphics G = Graphics.FromImage(ScreenShot)) 
    { 
        G.CopyFromScreen(bounds.Left, bounds.Top, 0, 0, bounds.Size);
    }
    // code unhiding the same elements
    SaveScreenshot(Screenshot);
}

The application was originally compiled under Windows 7 using VS2008, but recompiling it under Windows 10 with VS2017 hasn't solved the issue.

Any ideas what would cause this to go wrong under Windows 10, but not Windows 7?

I would prefer not to have to upgrade to a later version of .Net if possible.

Here is a sample of the screenshots it is providing at 125% magnification. Badly offset screenshot

Paul Sinclair
  • 223
  • 1
  • 12
  • Might the extra pixels be the (terrible) drop shadow w10 add to our (desktop) windows? To test: disable and compare the bounds. If so reduce the bounds you use accordingly.. – TaW Mar 11 '19 at 23:47
  • @TaW - unfortunately no. I wasn't seeing any shadow, but I hunted down and disabled the shadow option anyway. It didn't have any affect. – Paul Sinclair Mar 12 '19 at 13:38
  • I wonder if that has something to do with [DPI awareness and differences between `PrimaryScreen.Bounds` and `PrimaryScreen.WorkingArea`](https://social.msdn.microsoft.com/Forums/windows/en-US/44c1fb89-3577-4f99-bcf6-d15d0efb7bb5/windows-form-screenprimaryscreenbounds-incorrect-for-1920x1080-screen-resolution?forum=winforms). – GSerg Mar 12 '19 at 13:57
  • @GSerg - Unfortunately, adding DPI awareness has resulted in the entire form getting messed up for magnifications > 100%. – Paul Sinclair Mar 12 '19 at 14:20
  • @PaulSinclair Is https://stackoverflow.com/q/27661545/11683 of any help? – GSerg Mar 12 '19 at 15:46
  • @GSerg - it may be helpful to make the form actually DPI aware, but that may take a lot of work. If it is what I need to do, I'll do it, but I'm hoping there is something simpler. – Paul Sinclair Mar 12 '19 at 15:55
  • @GSerg - I've updated the form to better handle DPI changes (there was some dynamic resizing that was messing it up) and added DPI awareness to the manifest. Now whichever DPI the user logs in with will have screenshots that have 4 extra pixels from the background on each side and on the bottom. However a change of DPI will still result in huge offsets. – Paul Sinclair Mar 13 '19 at 12:49
  • You discovered that the borders of a window on Win10 are transparent. Another way to see it is hovering the mouse near a border, note that you get the arrow cursor well before the mouse is on the border. – Hans Passant May 15 '23 at 00:03

0 Answers0