如何在精确高度绘制给定角色?

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

对于Eg:

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

通过使用上面的代码,我正在绘制高度为1英寸的文本,但绘制的文本并不完全是1英寸。

我需要在精确的高度绘制文本,我正在给予。 提前致谢..

最简单的解决方案是使用GraphicsPath 。 以下是必要的步骤:

  • 计算你想要的高度(以像素为单位):要达到1.0英寸,比如150 dpi,你需要150像素。

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

  • 现在使用GetBounds测量结果高度。

  • 然后将高度缩放到必要的像素数

  • 最后清除路径并使用新高度再次添加字符串

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

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

下面的代码创建了这个文件; Consolas ‘的高度为150像素,而Wingdings字体的第二个字符(ox95)也是如此。 (注意我没有输出中心):

一英寸X.在此处输入图像描述

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

还有一种方法可以使用FontMetrics计算前面的数字,但我理解链接意味着这种方法可能与字体有关。