Basically, I want to measure the text size in a TextBox, and I found out that TextRenderer gives me the correct values, while Graphics would give me wrong values. So that seems like TextBox should be using GDI to draw text, right?
However, wherever I read, I see that it's supposed to use GDI+. For example:
- Here http://microsoft.public.dotnet.framework.windowsforms.controls.narkive.com/yoHKjPut/text-rendering-accuracy-problem-in-textbox-richtextbox it says that TextBox and Buttons use GDI+.
- Here Why is Graphics.MeasureString() returning a higher than expected number? it says that default is "compatibility mode" that draws in GDI+
So why is it that I'm getting consistent results with TextRenderer and not with Graphics when it's supposed to be other way around?
The .NET Framework uses GDI+ in most places. The Graphics
class is a wrapper around GDI+.
However, WinForms also wraps native Win32 controls, which do not use GDI+. They use GDI. TextBox and RichTextBox are examples of this. They draw their text using GDI.
To measure GDI+ text, you will use Graphics.MeasureString
(note the Graphics
class there). To measure GDI text, you will use TextRenderer.MeasureText
(note how it is not provided by the Graphics
class).
Things do get slightly more complicated: some controls do use GDI+ for drawing purposes. The Button control is an example of this. Unless you have its FlatStyle set to System, it is owner-drawn by the .NET Framework, and that owner drawing is done using GDI+ (the graphics subsystem used by .NET). However, if you set the FlatStyle to System, the rendering is delegated to the native Win32 Button control, which uses GDI (like all native Win32 controls).
My suggestion is to use GDI (TextRenderer
) whenever possible to draw text. If you use a control that implements the FlatStyle property, set it to System. Text drawn with GDI+ just looks lousy by comparison. As a bonus, you'll get controls that actually look native and blend in with the system. FlatStyle.System is the only way to get a button control that actually pulses and glows. The WinForms renderer doesn't implement this.
All right, some more research have shown that there is a confusion of what is "Default".
When these sources talk about "Default", they talk about "Default(1)" as in "If no extra code is executed", not "Default(2)" as in "If no extra action is taken".
Because by "Default(2)", "Application.SetCompatibleTextRenderingDefault(false)" is added to the Program.cs file of a new proiject, which makes TextBox class use GDI for rendering, which is not the "Default(1)" for it, but in fact this is what happens if you, as a programmer, take no additional action.
This is basically what got me.
PS: I'm basing the assumtion that it's true by "Default(1)" on the accepted answer from this question UseCompatibleTextRendering property not created by designer when it is set to false
GDI+ error comes usually when we giving path for storing something in folder and that path is incorrect, meaning folder can not access or not able to find that path. So, just correct the path or allow folder security accessible.