Unable to make app DPI aware - this is not a duplicate

100 views Asked by At

I cannot make my application DPI-aware. In app.manifest I uncommented:

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

In App.config I added:

<appSettings>
    <add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />
</appSettings>

I am following the questions and responses in Make vb.net application DPI aware and https://www.telerik.com/blogs/winforms-scaling-at-large-dpi-settings-is-it-even-possible-

My application has a single form with a single user control. On each I tried running the app with the AutoScaleMode to each of the various settings: None, Dpi, Font, Inherit (they default to Font). I am using a single monitor which is factory original on my laptop.

In every case, e.graphics.dpix and e.graphics.dpiy (where e is PaintEventArgs) is 96.0. It should be 128.0 = 1920 pixels / 15 inches and 128.0 = 1080 / 8.4375 inches.

What am I missing?

1

There are 1 answers

0
RJPisscat On

Here's a partial solution.

For painting to the screen, set Graphics.PageUnit = GraphicsUnit.Point (the default is GraphicsUnit.Display).

(I haven't figured out how to AutoSize the UserControl that I'm painting without kludging DPI.)

For printing, use Graphics.PageUnit = GraphicsUnit.Pixel.

' printing
dim gs as Drawing2D.GraphicsState = e.Graphics.Save
Try
    e.Graphics.PageUnit = GraphicsUnit.Pixel
    dim DpiX as Single = e.Graphics.DpiX
    dim DpiY as Single = e.Graphics.DpiY
    DoPrinting(e.Graphics, DpiX, DpiY)  ' this is where you implement the code to draw your page
Catch ex as Exception
Finally
    e.Graphics.Restore(gs)
End Try

' painting to screen
dim gs as Drawing2D.GraphicsState = e.Graphics.Point
Try
    e.Graphics.PageUnit = GraphicsUnit.Pixel
    dim DpiX as Single = 128.0!    ' your value may vary; to find out, divide
    dim DpiY as Single = 128.0!    ' physical size of screen by screen resolution
    DoPrinting(e.Graphics, DpiX, DpiY)  ' this is where you implement the code to paint to screen
Catch ex as Exception
Finally
    e.Graphics.Restore(gs)
End Try