开发者

ASP.net can't get font size right

开发者 https://www.devze.com 2023-02-17 06:47 出处:网络
I\'m drawing labels to an image file.It all works perfectly, except the font size. gfx.DrawString( thisTempLabel.LabelText,

I'm drawing labels to an image file. It all works perfectly, except the font size.

gfx.DrawString(
    thisTempLabel.LabelText,
    new System.Drawing.Font(
        thisTempLabel.LabelFont,
        (float)thisTempLabel.fontSize
    ),
    Brushes.Black,
    new PointF(thisTempLabel.x, thisTempLabel.y)
);

Problem is my users pick font sizes in PX, and System.Drawing.Font requires an EM siz开发者_运维百科e. I don't know how to resolve this!

Can I render the text using pixels?


Finally figured it out for if anyone has same problem:

System.Drawing.Font MyFont = new System.Drawing.Font(
    thisTempLabel.LabelFont,
    ((float)thisTempLabel.fontSize),
    FontStyle.Regular,
    GraphicsUnit.Pixel
);

GraphicsUnit.Pixel does it!


Just use 1 em = 16 pixels. So:

14px: 1 / 16 * 14 = 
13px: 1 / 16 * 13 = 
100px: 1 / 16 * 100 = 
0

精彩评论

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