简体   繁体   English

如何在Graphics DrawString中更改文本字体大小?

[英]How can i change in Graphics DrawString the text font size?

I'm using this method to draw the text on form1: 我正在使用此方法在form1上绘制文本:

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

I tried to set Width to 14 on the SizeF size lline but it didn't change the dize and the only thing it did is moving a bit the text from it's location . 我试图在SizeF size lline上将Width设置为14,但是它并没有改变昏暗感,唯一要做的就是将文本从其位置移开一点。

How can i change the font size of the text, and also to keep the perspective(if this is the right word to use) of the text location ? 如何更改文本的字体大小,并保持文本位置的透视图(如果这是正确的词)?

This is how it look like when not using the Width 14 at all the text is in the center above the pictureBox1. 这就是当根本不使用宽度14时,文本位于pictureBox1上方的中间的样子。 I want that when i change the text size it will be kept to be in the center like it is now. 我希望当我更改文字大小时,它会像现在一样保持在中间。

The text is in Red and it's in hebrew in this case. 文本为红色,在这种情况下为希伯来语。

文本

Try using a bigger font: 尝试使用更大的字体:

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

Do avoid using CreateGraphics, it's only a temporary drawing that will get erased by overlapping windows or minimizing the form. 请避免使用CreateGraphics,它只是一个临时图形,会因重叠窗口或最小化表单而被擦除。 It will also cause flicker. 也会引起闪烁。 Use the graphics object from the paint event and invalidate the control to update the painting. 使用paint事件中的图形对象并使控件无效以更新绘画。

Also, do favor using TextRenderer.DrawText and TextRenderer.MeasureText for your text renderings. 此外,请务必在文本渲染中使用TextRenderer.DrawText和TextRenderer.MeasureText。 DrawString should primarily be used for printing to paper. DrawString应该主要用于打印到纸张上。

I think the best way is to use StringFormat object to center-align the text either horizontally or vertically or both using the 5th overload of Graphics.DrawString() funciton: 我认为最好的方法是使用Graphics.DrawString()函数的第5次重载使用StringFormat对象将文本水平或垂直居中对齐或同时对齐:

在此处输入图片说明

You need to provide a Rectangle objet and alignment is done with respect to this object. 您需要提供一个Rectangle对象,并已针对该对象进行对齐。

StringFormat sf=new StringFormat();
sf.LineAlignment = StringAlignment.Center;//center-align vertically
sf.Alignment = StringAlignment.Center; //center-align horizontally
     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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM