Search code examples
c#winformsmouseeventdpi

Force Winform program to run at 96dpi?


I currently have a winform application that relies relatively heavily on reading mouse click points. It's a video application that allows the user to click on incoming video feed from a webcam and draw lines on it and do other things. The important part for this question is the drawing lines- a simple example is that if they click somewhere on the video feed, it instantly draws a horizontal line across the entire picture at the point of the mouse.

This works flawlessly on 96dpi, but when windows scales to 125% zoom (Control Panel\All Control Panel Items\Display -> Medium - 125%), which scales it to 120 dpi, it all goes to pot. Suddenly the application reads the mouse as being clicked much farther down the picture than it actually is.

All I'm doing is pulling the exact location of the mouse upon mouseclick. It is literally as simple as

                using (Graphics g = Graphics.FromImage(tmpImage))
            {
                g.DrawLine(new Pen(lineColor, lineWidth),
                           new Point(0, e.Location.X), new Point(tmpImage.Width,
                           e.Location.Y));
            }

In 96dpi, it draws exactly at the mouse. At 120dpi, it draws quite a bit south of the mouse. This is a huge problem for me.

Is there any way that I can either make my application constantly run at 96dpi, or some other solution to cause my application to work appropriately under the circumstances of 120+dpi?

Thank you!


Solution

  • Take a look at AutoScaleMode property. this is what you need.

    By default AutoScaleMode is set to AutoScaleMode.Font typically in *.designer.cs Change that to AutoScaleMode.None for no scaling or you can use AutoScaleMode.Dpi with AutoScaleDimensions

    Hope this helps