简体   繁体   English

C#绘图文字

[英]C# drawing text

gfx.DrawString(
    thisTempLabel.LabelText,
    new Font("Arial", (float)thisTempLabel.fontSize),
    Brushes.Black,
    new PointF(thisTempLabel.x, thisTempLabel.y)
    );

This works fine, except I store my font size ( thisTempLabel.fontSize ) in pixels. 这工作正常,除了我以thisTempLabel.fontSize为单位存储我的字体大小( thisTempLabel.fontSize )。 I can't for the life of me work out how to convert them (probably impossible) or what to do to resolve this. 我不能为我的生活找出如何转换它们(可能是不可能的)或如何解决这个问题。

They come out sort of right, but not in the right position by a bit and a bit too big. 它们是正确的,但不是在正确的位置有点太大。

Precision is very important. 精度非常重要。

I think the issue you're having might be that the constructor you're using expects the size to be in points: 我认为你遇到的问题可能是你正在使用构造函数需要大小为点:

public Font(FontFamily family, float emSize)

emSize
Type: System.Single 类型:System.Single
The em-size, in points, of the new font. 新字体的em-size(以磅为单位)。

It looks like you can use a different overload that takes GraphicsUnit parameter, which you can set to GraphicsUnit.Pixel : 看起来您可以使用另一个带有 GraphicsUnit参数的重载 ,您可以将其设置为GraphicsUnit.Pixel

gfx.DrawString(
    thisTempLabel.LabelText,
    new Font("Arial", (float)thisTempLabel.fontSize, GraphicsUnit.Pixel),
    Brushes.Black,
    new PointF(thisTempLabel.x, thisTempLabel.y)
);

Note that you're setting the em size, which is, roughly, the height of the "M" character. 请注意,您要设置em大小,大致是“M”字符的高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM