开发者

Graphics in C# (.NET)

开发者 https://www.devze.com 2022-12-22 02:33 出处:网络
I use this code for drawing text in a panel: Graphics g = panel1.CreateGraphics(); g.DrawString(...); So I want to know 开发者_如何转开发what size the input text will be when rendered in the panel.

I use this code for drawing text in a panel:

Graphics g = panel1.CreateGraphics();
g.DrawString(...);

So I want to know 开发者_如何转开发what size the input text will be when rendered in the panel.


Use g.MeasureString() to get the width of a string in the grapic context.

// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);

// Measure string.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont);


You can also use TextRenderer.MeasureText which is sometimes easier to use than MeasureString.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号