How to Draw a given Character in exact height?

ぐ巨炮叔叔 提交于 2020-01-11 11:51:49

问题


I am drawing the text using Graphics.DrawString() method, But the text height drawn is not same as which i gave.

For Eg:

Font F=new Font("Arial", 1f,GraphicUnit.Inch);
g.DrawString("M", F,Brushes.red,new Point(0,0));

By using the above code, i'm drawing the text with height 1 inch, but the text drawn is not exactly in 1 inch.

I need to Draw the text in Exact height which i'm giving. Thanks in advance..


回答1:


The simplest solution will be to use a GraphicsPath. Here are the steps necessary:

  • Calculate the height you want in pixels: To get 1.0f inches at, say 150 dpi you need 150 pixels.

  • Then create a GraphicsPath and add the character or string 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 up to the necessary number of pixels

  • Finally clear the path and add the string again with the new height

  • Now you can use FillPath to output the pixels..

Here is a code example. It writes the test string to a file. If you want to write it to a printer or a control using their Graphics objects, you can do it the same way; just get/set the dpi before you calculate the first estimate of the height..

The code below creates this file; the Consolas 'x' is 150 pixels tall as is the 2nd character (ox95) from the Wingdings font. (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 may want to try using the correction factor to scale up a Font size and use DrawString after all.

There is also a way to calculate the numbers ahead using FontMetrics, but I understand the link to mean that such an approach could be font-dependent..



来源:https://stackoverflow.com/questions/28893445/how-to-draw-a-given-character-in-exact-height

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!