If you have just opened one of your previous applications in your brand new computer with a very high-dpi monitor, perhaps you will find out that your interface that previously worked perfectly is now utterly broken and/or blurry.
This might be the case, for example, if you just tried to open an old Windows.Forms application in your brand new Surface Pro computer.


How to fix it
- Go the the Forms designer, then select your Form (by clicking at its title bar)
- Press F4 to open the Properties window, then locate the AutoScaleMode property
- Change it from Font (default) to Dpi.
Now, go to Program.cs (or the file where your Main method is located) and change it to look like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace Classification.BoW { static class Program { [STAThread] static void Main() { if (Environment.OSVersion.Version.Major >= 6) SetProcessDPIAware(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern bool SetProcessDPIAware(); } } |
Save and compile. Now your form should look crispy again.
I encountered this problem while opening and editing Accord.NET sample applications in Visual Studio in a Surface 3 Pro.
Related resources
- http://stackoverflow.com/questions/13228185/winforms-high-dpi-blurry-fonts
- http://stackoverflow.com/questions/27933868/creating-dpi-aware-c-sharp-clickonce-application-with-winforms
- http://blogs.telerik.com/winformsteam/posts/14-02-11/winforms-scaling-at-large-dpi-settings-is-it-even-possible-
- http://stackoverflow.com/questions/27933868/creating-dpi-aware-c-sharp-clickonce-application-with-winforms
Thank you very much, good sir!
Thanks a lot!! Great article!!!
Thanks much… everything looks so much better now!
This was a problem on my windows 8.1 box, and when I upgraded to 10, it was still an issue – blurry, and my controls were getting clipped/shrunken with lines of text cut in half and other weird artifacts that all looked fine in the designer. I checked the settings on my machine and yup, 150%. So I added the interop and changed the form AutoScaleMode to DPI as you recommended above, and >poof< like magic all the weirdness went away. Far far away!
Thank you so much, This worked in Visual Studio 2015 on Windows 10. I was having blurry fonts in all my forms!
For anyone interested in getting rid of blurry forms in VB.net:
– Find Application.Designer.vb (instead of Program.cs)
– Insert:
‘Inside Public Sub New(), AFTER the line MyBase.New(…
If (Environment.OSVersion.Version.Major >= 6) Then
SetProcessDPIAware()
End If
‘Outside of Public Sub New()
Private Shared Function SetProcessDPIAware() As Boolean
End Function
Cheers 🙂
This did not work for me.
I got it. This works if you follow the 1 am comment by JOAOPAULO.
Shit, it got rid of my line with System.Runtime.InteropServices.DllImport(“user32.dll”)
You need this inside pointy brackets or inequality signs () instead of quare brackets above the definition of SetProcessDPIAware() function
Thank you YumzLab, it actually worked awesomely!
Team,
I am having exactly the same problem as the link below and I have finally come to the conclusion that I had to implement the
SetProcessDPIAware() function on one of the windows forms encompassing the report viewer. I tried doing the changes to the application manifest file but it is not compatible with the Click Once Deployment technique.
https://social.msdn.microsoft.com/Forums/en-US/51ce2679-5671-41c3-91d9-b4636d16b94e/rdlc-print-layout-size-varies-with-resolution-on-surface-pro-win-81-winforms?forum=sqlreportingservices.
After implementation, i found that there the font sizes have been minimized and there are some misalignment in other forms too.
Also I just want the report print layout to scaled to the width which is just one form. Is there any way I can just apply to this form and leave others untouched ?
Thanks a lot, and more than a lot …
it works on my Windows 10. See you on CP …
Cheers,
Thank you, works like a charm.
Fantastic! Thank you very, very much…
Thank you very much bro . You are best
Thank you so much ! So long i was searching for this !
Wonderful article, very useful and well explanation. Your post is extremely incredible. I will refer this to my candidates
You are a life saver!! Thanks a lot for this solution. Worked like a charm.
Thanks for this!! Solved the issue perfectly.
Great! It fixed my problem on Visual Studio 2015 and Windows 10. Thanks.
Thank you for explaining this solution! Works great with my C# Winform apps!
Most excellent!
very good :-0
Thanks !!!!!!!!
Blessed!! Many thanks.
It works !
Thanks,
Thank you!
That was rapidly driving me nuts!!
Thank you for sharing best post.
Now in 2019, very thanks to you.
Thank you Very Much. Please Consider Fixing the code text >=6 to >= 6 be helpfull.
If someone gets an error in >=6 write it like Environment.OSVersion.Version.Major >= 6
Since I was getting an error in & gt
Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..
I think this is an informative post and knowledgeable. Thank you for sharing this wonderful post! I’m glad that I came across your article.
Thanks for sharing the informative concept. Your data visualization contents are really good. You will explain the concept is a very clear and detailed explanation.
Hello, thank you for the tutorial, very useful. I used these tips for fix de blurry Windows forms for my Visual Basic VB.net applications.
For fix in VB.net follow the steps:
–
1. In Program.vb insert the library:
Imports System.Runtime.InteropServices
–
2.Inside the subroutine Sub New, after the line MyBase.New, insert the conditional code:
If (Environment.OSVersion.Version.Major >= 6) Then
SetProcessDPIAware()
End If
–
3.Outside Public Sub New, on the next line insert the code:
Private Shared Function SetProcessDPIAware() As Boolean
End Function
Congratulations, you VB.net application is great for all DPI monitors 🙂
Special tanks for CEZARSOUZA and YumzLab
Hello, thank you for the tutorial, very useful. I used these tips for fix de blurry Windows forms for my Visual Basic VB.net applications.
For fix in VB.net follow the steps:
–
1. In Program.vb insert the library:
Imports System.Runtime.InteropServices
–
2.Inside the subroutine Sub New, after the line MyBase.New, insert the conditional code:
If (Environment.OSVersion.Version.Major >= 6) Then
SetProcessDPIAware()
End If
–
3.Outside Public Sub New, on the next line insert the code:
DllImport(“user32.dll”, SetLastError:=True, CharSet:=CharSet.Auto) //put this expression between diamond symbols (Minor / Greater than)
Private Shared Function SetProcessDPIAware() As Boolean
End Function
Congratulations, you VB.net application is great for all DPI monitors
Special tanks for CEZARSOUZA and YumzLab
Thankyou !!!!!!
Amazing !!!!!!! Worked !
Thank you! Worked beautifully on a C#/VS 2019 form.
Good Post! , it was so good to read and useful to improve my knowledge as an updated one, keep blogging. After seeing your article I want to say that also a well-written article with some very good information which is very useful for the readers….thanks for sharing it and do share more posts like this.
Thanks For Sharing The Information The Information Shared Is Very Valuable Please Keep Updating Us
Thanks for providing good content.
Thanks for sharing good content.
i am c++ guy 😥…………………………………………………………….
How about vb.net winform code ?