Reputation: 131
I want to create a bmp file (in C#) with text written on it. This bmp is really small (120*70) and the required font size is 5. Once I create this bmp the text is not readable. Is there a way to solve this?
I have already used
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
But no luck
Upvotes: 0
Views: 1548
Reputation: 54433
The key to success is mainly the chosen Font. This seems to go to the limit:
It uses the smallest-pixel-7 font and writes to the bitmap like this:
string testText = "The quick brown fox <_>°|^ 123456789-0!";
using (Graphics G = Graphics.FromImage(bmp0))
using (Font font = new Font("Smallest Pixel-7", 10f))
{
G.TextContrast = 0;
G.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
G.DrawString(testText, font, Brushes.Black, new Point(20, 20));
}
Most characters fit in a 5 pixel height, some use the 6th pixel line, like the 'Q' and I guess the 7th line is for leading..
Note the it is all caps and watch their license! (There ought to be a freeware font as well..like maybe here)
In my tests AntiAliasGridFit
was the best option, much better than AntiAlias
:
Each character is drawn using its antialiased glyph bitmap with hinting. Much better quality due to antialiasing, but at a higher performance cost.
Note 2: The bitmap's dpi resolution is directly related to the necessary font size. In the 1st example 72 dpi were used. If you increase the resolution you can reduce the font size.
bmp0.SetResolution(150, 150);
The example below used 72dpi and a fontsize of 9.6pt.
Here is a selection of various other fonts; if you can't read the font family you don't want to use it ;-)
(I have added a line every 5 pixels so you can compare the actual heights.)
Upvotes: 3