简体   繁体   English

如何在system.drawings.font中设置字体高度?

[英]How can I set font height in system.drawings.font?

I'm using C# to write a text in a certain format. 我正在使用C#以某种格式编写文本。 My problem is that when I edit font size both width and height are changing while I just want to change the font height. 我的问题是,当我编辑字体大小时,宽度和高度都在改变,而我只想更改字体高度。

My code: 我的代码:

using (Graphics graphics = Graphics.FromImage(bitmap))
{
    using (System.Drawing.Font romanfont = new System.Drawing.Font("Times New Roman",11, FontStyle.Bold))
    //using (System.Drawing.Font romanfont = new System.Drawing.Font("Times New Roman", 11, FontStyle.Bold))
    {
        SolidBrush transBrush = new SolidBrush(Color.FromArgb(65, 79, 79));
        StringFormat format = new StringFormat(StringFormatFlags.DirectionRightToLeft);

        graphics.DrawString(firstname, romanfont, transBrush, firstnameLocation, format);
        graphics.DrawString(secondname, romanfont, transBrush, secondnameLocation, format);
        graphics.DrawString(finalfirstadd, romanfont, transBrush, firstaddresslocation, format);
        graphics.DrawString(finalsecondadd, romanfont, transBrush, secondaddresslocation, format);
    }
}

You can achieve this effect by setting a transform on the Graphics object. 您可以通过在Graphics对象上设置转换来实现此效果。

For example, if you want to make the text twice as tall but still the same width, you can do this: 例如,如果要使文本的高度加倍但宽度相同,则可以执行以下操作:

graphics.scaleTransform(1, 2);

You would put this anywhere above the place where you draw your strings. 您可以将其放在绘制线的位置上方的任何位置。 Note that this change will make everything twice as tall, so you may need to adjust your positions and sizes of your rectangles (such as firstnameLocation ; in this case you'd probably want to divide the top and height of the rectangle by 2.) 请注意,此更改将使所有内容变高两倍,因此您可能需要调整矩形的位置和大小(例如firstnameLocation ;在这种情况下,您可能希望将矩形的顶部和高度除以2。)

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

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