C# 如何更改图形抽绳中的文本字体大小?
我使用此方法在表格1上绘制文本:C# 如何更改图形抽绳中的文本字体大小?,c#,.net,winforms,C#,.net,Winforms,我使用此方法在表格1上绘制文本: private bool DrawText(bool draw, string texttodraw) { Graphics g = this.CreateGraphics(); SizeF size = g.MeasureString(texttodraw, SystemFonts.DefaultFont,14); g.DrawString(texttodraw, Font
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;
}
我试图将SizeF size lline的宽度设置为14,但它没有改变dize,它所做的唯一一件事就是将文本从它的位置移动一点
如何更改文本的字体大小,以及如何保持透视效果,如果这是正确使用文本位置的单词
这是完全不使用宽度14时的情况,文本位于图片Box1上方的中心。我想,当我改变文本大小时,它将保持在中心,就像现在一样
文本为红色,在本例中为希伯来语
尝试使用更大的字体:
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主要用于打印到纸上。我认为最好的方法是使用StringFormat对象来水平或垂直居中对齐文本,或者使用第五个图形重载。DrawString函数:
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);
您需要提供一个矩形对象,并针对该对象进行对齐
StringFormat sf=new StringFormat();
sf.LineAlignment = StringAlignment.Center;//center-align vertically
sf.Alignment = StringAlignment.Center; //center-align horizontally