如何绘制精确高度的给定角色? [英] How to Draw a given Character in exact height?

查看:79
本文介绍了如何绘制精确高度的给定角色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Graphics.DrawString()方法绘制文本,但是绘制的文本高度与我给出的高度不同.

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

例如:

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

通过使用上面的代码,我正在绘制高度为1英寸的文本,但是绘制的文本并非恰好在1英寸内.

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..

推荐答案

最简单的解决方案是使用GraphicsPath.这是必要的步骤:

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

  • 计算所需的高度(以像素为单位):要获得1.0f英寸(例如150 dpi)的分辨率,您需要150像素.

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

然后创建一个GraphicsPath,并使用计算出的高度以您要使用的字体和字体样式添加字符或字符串

Then create a GraphicsPath and add the character or string in the font and font style you want to use, using the calculated height

现在使用GetBounds测量结果高度.

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

现在您可以使用FillPath输出像素.

Now you can use FillPath to output the pixels..

这是一个代码示例.它将测试字符串写入文件.如果要使用它们的Graphics对象将其写入打印机或控件,则可以使用相同的方法进行操作.在计算高度的第一个估计值之前,只需获取/设置dpi.

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..

下面的代码创建此文件; Consolas'x'的高度为150像素,是Wingdings字体的第二个字符(ox95)的高度. (请注意,我没有将输出居中):

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);
}

您可能想尝试使用校正因子来放大Font的大小,并最终使用DrawString.

You may want to try using the correction factor to scale up a Font size and use DrawString after all.

还有一种方法可以使用 FontMetrics ,但我知道链接的意思是这种方法可能与字体有关.

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..

这篇关于如何绘制精确高度的给定角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆