Form location being messed by VS 2019 in Windows 10

VB.net.beginner

New member
Joined
Oct 18, 2022
Messages
3
Programming Experience
10+
I have used this subroutine for screen capture of forms on Windows 7 with VS 2019. When I started using this on Windows 10, the locations are messed up. The left of the Form is a lot of pixels to the right of the image captured. Is there a good solution or workaround which does not need a lot of new code?


VB.net:
Sub SaveFromScreen(frm As Form, filn As String)

Dim img As New Bitmap(frm.ClientSize.Width, frm.ClientSize.Height)

Using gr As Graphics = Graphics.FromImage(img)
    gr.CopyFromScreen(frm.PointToScreen(frm.ClientRectangle.Location), Point.Empty, frm.ClientSize)
End Using

img.Save(filn, Imaging.ImageFormat.Png)

End Sub

Then it is called simply as
Call SaveFromScreen(Me, filN)
The screen is 1920 x 1080. Earlier ones were smaller. Could this be one of the reasons?
 
Try opening the manifest (project properties, Application tab, click View Windows Settings) and uncomment the part to make the application DPI aware.
It looks like this in a .Net Framework 4.8 project:
XML:
<application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
        <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
    </windowsSettings>
</application>
 
I uncommented this following part:

manifest:
  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
      <longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
    </windowsSettings>
  </application>

Now it messed up the sizes of the windows.
 
Now it messed up the sizes of the windows.
Try the different settings for forms AutoScaleMode property also.

The "problem" comes from having increased the system Scale factor, which is not uncommon doing when the resolution increases since it can make text small and hard to read. You may see something like this in Display settings:
1666119048350.png
 
Inspite of all this help and changes in app.manifest and app.config, I am not able to get dpiAwareness. Is it feasible to go back to .NET framework 4.2 where dpiAwareness seems to be there by default?

I have added
VB.NET:
  <System.Windows.Forms.ApplicationConfigurationSection>
    <add key="DpiAwareness" value="PerMonitorV2" />
  </System.Windows.Forms.ApplicationConfigurationSection>
to app.config
and also
uncommented for Windows 10
VB.NET:
      <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
in app.manifest, besides
VB.NET:
 <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
      <longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
    </windowsSettings>
  </application>
but it is still not working.
VB.NET:

I don't have the option of setting the screen at 100%.
 
Back
Top