How to draw this character at the exact height?
I am drawing text using the Graphics.DrawString () method, but the height of the text is not the same as I gave.
For example:
Font F=new Font("Arial", 1f,GraphicUnit.Inch);
g.DrawString("M", F,Brushes.red,new Point(0,0));
Using the code above, I draw text 1 "high, but the text is not drawn exactly 1".
I need to draw the text at the Exact height I give. Thanks in advance.
source to share
The simplest solution would be to use GraphicsPath
. Here are the steps involved:
-
Calculate the height you want in pixels: To get a 1.0 foot inch, say 150 dpi, you need 150 pixels.
-
Then create
GraphicsPath
and add a character or line in the font and font style you want to use using the calculated height -
Now measure the resulting height using
GetBounds
. -
Then scale the height to the required number of pixels
-
Finally, clear the path and add the line again with the new height
-
Now you can use
FillPath
to display pixels.
Here's some sample code. It writes a test line to a file. If you want to write it to a printer or control using their objects Graphics
, you can do it the same way; just set / set dpi
before calculating the first height estimate.
The code below creates this file; Consolas
The 'x' is 150 pixels high, just like the second character (ox95) from the font Wingdings
. (Note that I did not center the output):
// we are using these test data:
int Dpi = 150;
float targetHeight = 1.00f;
FontFamily ff = new FontFamily("Consolas");
int fs = (int) FontStyle.Regular;
string targetString = "X";
// this would be the height without the white space
int targetPixels = (int) targetHeight * Dpi;
// we write to a Btimpap. I make it large enough..
// Instead you can write to a printer or a Control surface..
using (Bitmap bmp = new Bitmap(targetPixels * 2, targetPixels * 2))
{
// either set the resolution here
// or get and use it above from the Graphics!
bmp.SetResolution(Dpi, Dpi);
using (Graphics G = Graphics.FromImage(bmp))
{
// good quality, please!
G.SmoothingMode = SmoothingMode.AntiAlias;
G.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
// target position (in pixels)
PointF p0 = new PointF(0, 0);
GraphicsPath gp = new GraphicsPath();
// first try:
gp.AddString(targetString, ff, fs, targetPixels, p0,
StringFormat.GenericDefault);
// this is the 1st result
RectangleF gbBounds = gp.GetBounds();
// now we correct the height:
float tSize = targetPixels * targetPixels / gbBounds.Height;
// and if needed the location:
p0 = new PointF(p0.X - gbBounds.X, p0.X - gbBounds.Y);
// and retry
gp.Reset();
gp.AddString(targetString, ff, fs, tSize, p0, StringFormat.GenericDefault);
// this should be good
G.Clear(Color.White);
G.FillPath(Brushes.Black, gp);
}
//now we save the image
bmp.Save("D:\\testString.png", ImageFormat.Png);
}
You can try using a correction factor to increase the size Font
and use DrawString
in the end.
There is also a way to calculate numbers forward using FontMetrics
, but I understand this link means this approach might be font dependent.
source to share