Is it possible to determine the exact font size when drawing text using GDI +?

I use the following code to draw text in a bitmap:

using (Font font = new Font("Arial", 10.0f, FontStyle.Bold, GraphicsUnit.Point)) { //draw the text graphics.DrawString("Some text", font, Brushes.White, rect, stringFormat); } 

It works well. The text is displayed here:

enter image description here

I want to make the text a little bigger. If I set 11 as the font size, here is what I get:

enter image description here

He is too big for what I want. I tried 10.25, 10.5 and such, but it gives the same result as 10.

I am also trying to set GraphicsUnit on a Pixel , but it behaves the same (there is no way to set my own font size).

Here is my question:

When drawing text using GDI + (C #), is it possible to "fine-tune" the size of the displayed text?


EDIT: a more complete code snippet (on request):

 using (Bitmap bitmap = new Bitmap(width, height)) using (Graphics graphics = Graphics.FromImage(bitmap)) using (Font font = new Font("Arial", 10.0f, FontStyle.Bold, GraphicsUnit.Point)) { graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; Rectangle rect = new Rectangle(0, 0, width, height); //method 1 StringFormat stringFormat = new StringFormat(); stringFormat.Alignment = StringAlignment.Center; stringFormat.LineAlignment = StringAlignment.Center; graphics.DrawString("Some text", font, Brushes.White, rect, stringFormat); //method 2 TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak; TextRenderer.DrawText(graphics, "Some text", font, rect, Color.White, flags); bitmap.Save(stream, ImageFormat.Png); } 
+6
source share
2 answers

I don’t know C # or .Net libraries, but looking at this page , it seems that choosing AntiAlias for TextRenderingHint should turn off the hint and enable anti-aliasing. This will give you more space between sizes, but possibly more complex text.

The problem is the choice of fonts. When the font details, such as the stroke width or serif size, are in the same order as the resolution of the target device, simply scaling the design units on the devices can cause one-sided and uneven text, especially if there is no smoothing of any kind.

To avoid this, most fonts have β€œhints” that give special rules for rounding when permission is limited. The effect is that you see β€œsteps” in size.

If you want to avoid steps, you need to disable the "hint". This will cause the rasterizer to ignore special rounding rules and simply scale the design units directly on the devices. This allows for truly linearly scalable text. The trade-off is that the text may not look so good with small sizes. Smoothing can improve symmetry, but for many people, text may look fuzzy.

+3
source

Graphics.DrawString () has serious and insoluble problems with accuracy, suitable only for high-resolution devices such as printers.

Use the .NET 2.0 TextRenderer.DrawText () method instead.

+4
source

Source: https://habr.com/ru/post/917085/


All Articles