Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Forms Graphic Issue on Windows 10 OS

When I run any Windows Forms application in Windows 10, the graphics inside the window appear to be distorted:

Bad Image

At design time this does not happen:

Good Image

Has anyone ever experienced this?

(Please,open the images to see better.)

like image 591
Vinicius Gonçalves Avatar asked Nov 07 '15 21:11

Vinicius Gonçalves


People also ask

Why is my graphics card not working Windows 10?

Driver issues including corrupt, outdated or incompatible drivers could be the cause of Graphics card issues. Thus, fixing the driver issue will restore the device to its working condition. #1: Updating Graphics Card Drivers Right click the Windows Start button or press “Win + X” on the keyboard.

What is a graphics card in Windows 10?

Graphics card is one of the most important components of your computer hardware that enables the rendering of every pixel you see on the display. It allows quality display when you navigate through the computer, running applications and playing games. In this article, we will explain how to fix various graphics card issues in Windows 10 computer.

How do I adjust the graphics settings in Windows 10?

You can try to adjust the graphics settings especially if you experience display problems when running a specific application or game. Press the “Win + I” keys to access the settings window and then select “System” settings. Scroll down the options under “Display” and click the “Graphic settings” link at the bottom.

How to update graphics card drivers on Windows 10?

Right click the specific graphics card drivers and click on “Properties”. Go to the “Driver” tab and click the “Update Driver” button on the pop-up window. Select the option to search for updated drivers automatically or select the manual update option if you have the drivers downloaded and saved on your computer storage.


1 Answers

To solve the problem, you can make your application DPI-Aware using either of these options:

  • Application Manifest
  • API calls to SetProcessDPIAware, SetProcessDpiAwareness or SetProcessDpiAwarenessContext

Important Note: It is recommended that you set the process-default DPI awareness via application manifest, not an API call.

Using Application Manifest File

To make the application DPI-Aware, you can add an Application Manifest File to your project. Then in the app.manifest file, uncomment the part that is related to DPI-Awareness:

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

Then in your app.config file, add EnableWindowsFormsHighDpiAutoResizing setting its value to true:

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

For more information take a look at the following topic in Microsoft docs:

  • High DPI Desktop Application Development on Windows

SetProcessDPIAware API call Example

You can use SetProcessDPIAware() method before showing your main form to set your application dpi aware and prevent windows from scaling the application. Also you should check the windows version to be greater than or equals to vista:

static class Program
{
    [DllImport("user32.dll", SetLastError = true)]
    static extern bool SetProcessDPIAware();

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        if (Environment.OSVersion.Version.Major >= 6)
            SetProcessDPIAware();

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(true);
        Application.Run(new Form1());
    }
}

Notes

  1. As it's already mentioned above, it is recommended that you set the process-default DPI awareness via application manifest, not an API call.

  2. Before using API calls, read the documentations to know about supported OS and also possible race condition if a DLL caches dpi settings during initialization. Also keep in mind, DLLs should accept the dpi setting of the host process rather than API call themselves.

  3. You may find this DpiHelper class implemented in WinForms for .NET Core 3.0 useful.
like image 63
Reza Aghaei Avatar answered Oct 15 '22 12:10

Reza Aghaei