简体   繁体   中英

How can I set font height in system.drawings.font?

I'm using C# to write a text in a certain format. 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.

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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