[英]How can i change in Graphics DrawString the text font size?
我正在使用此方法在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;
}
我試圖在SizeF size lline上將Width設置為14,但是它並沒有改變昏暗感,唯一要做的就是將文本從其位置移開一點。
如何更改文本的字體大小,並保持文本位置的透視圖(如果這是正確的詞)?
這就是當根本不使用寬度14時,文本位於pictureBox1上方的中間的樣子。 我希望當我更改文字大小時,它會像現在一樣保持在中間。
文本為紅色,在這種情況下為希伯來語。
嘗試使用更大的字體:
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,它只是一個臨時圖形,會因重疊窗口或最小化表單而被擦除。 也會引起閃爍。 使用paint事件中的圖形對象並使控件無效以更新繪畫。
此外,請務必在文本渲染中使用TextRenderer.DrawText和TextRenderer.MeasureText。 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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.