2014-10-09 2202 views
-1

我使用这个方法来绘制在Form1上的文字:我试着将宽度设置对的SizeF大小lline 14我如何更改Graphics DrawString的文字字体大小?

private bool DrawText(bool draw, string texttodraw) 
     { 
      Graphics g = this.CreateGraphics(); 
      SizeF size = g.MeasureString(texttodraw, SystemFonts.DefaultFont,14); 
      g.DrawString(texttodraw, Font, Brushes.Red, pictureBox1.Location.X + (pictureBox1.Width/2) - (size.Width/2), 
                    pictureBox1.Location.Y - 30); 
      return draw; 
     } 

,但它并没有改变dize和它做的唯一事情是移动有点从它的位置文本。

我该如何更改文本的字体大小,并且还要保留文本位置的透视图(如果这是正确的词)?

这是它在未使用宽度14时的所有文本位于pictureBox1上方的中心时的样子。我希望当我改变文本大小时,它将保持在现在的中心位置。

该文本是红色的,在这种情况下是在希伯来语中。

Text

回答

3

尝试使用更大的字体:

using (Font bigFont = new Font(SystemFonts.DefaultFont.FontFamily, 14, FontStyle.Regular)) { 
    SizeF size = g.MeasureString(texttodraw, bigFont, 14); 
    g.DrawString(texttodraw, bigFont, Brushes.Red, pictureBox1.Location.X + (pictureBox1.Width/2) - (size.Width/2), 
                  pictureBox1.Location.Y - 30); 
} 

请避免使用的createGraphics,这只是一个临时的图纸将被重叠的窗口或最小化的形式将被删除。它也会导致闪烁。使用绘画事件中的图形对象并使控件无效以更新绘画。

此外,请确保使用TextRenderer.DrawText和TextRenderer.MeasureText作为文本渲染。 DrawString应主要用于打印纸张。

0
 private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 
    { 
     Font drawFont = new Font("Arial Black", 9); 
     e.Graphics.DrawString(nic.Text, drawFont, Brushes.Maroon, 174, 12);