SVG: measure the bounding box of some text from C#

2019-08-19 03:52发布

I am writing a program that is going to dump an SVG file, in C#. I was wondering if there is any way of measuring how big a piece of text is going to be from the C# program. I can assume that I know the font and fontsize. But the "average size of letter"*number_of_letters is very inaccurate.

I am looking for some option like this: 1) Just figure out, from font size and font, how big the text is. 2) I could dump the SVG once, and get the measure from there (by rendering it somehow? and then reading that information from it, somehow?)

Thanks!

标签: c# svg
1条回答
劳资没心,怎么记你
2楼-- · 2019-08-19 04:15

Figured it out:

Font stringFont = new Font("Verdana", 8, GraphicsUnit.Pixel);
Image newImage = new Bitmap(10, 10);
Graphics g = Graphics.FromImage(newImage);
SizeF stringSize = new SizeF();
g.PageUnit = GraphicsUnit.Pixel;
stringSize = g.MeasureString("Hello, this is \n a string and stuff", stringFont);
查看更多
登录 后发表回答