拉绳粗体和普通文本

问题描述 投票:0回答:2

有代码:

using (Graphics g = Graphics.FromImage(pictureBox1.Image))
            {

                Font drawFont = new Font("Arial", 12);
                Font drawFontBold = new Font("Arial", 12, FontStyle.Bold);
                SolidBrush drawBrush = new SolidBrush(Color.Black);
                g.DrawString("this is normal", drawFont, drawBrush, new RectangleF(350f, 250f, 647, 200));
                g.DrawString(" and this is bold text", drawFontBold, drawBrush, new RectangleF(350f, 250f, 647, 200));
            }

我需要得到

这是正常的 这是粗体文本

但是我收到第二个文本覆盖在第一个文本上

c#
2个回答
6
投票

尝试这个代码,可能会成功。!!!

using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
    Font drawFont = new Font("Arial", 12);
    Font drawFontBold = new Font("Arial", 12, FontStyle.Bold);
    SolidBrush drawBrush = new SolidBrush(Color.Black);

    // find the width of single char using selected fonts
    float CharWidth = g.MeasureString("Y", drawFont).Width;

    // draw first part of string
    g.DrawString("this is normal", drawFont, drawBrush, new RectangleF(350f, 250f, 647, 200));

    // now get the total width of words drawn using above settings
    float widthFirst = ("this is normal").Length() * CharWidth;

    // the width of first part string to start of second part to avoid overlay
    g.DrawString(" and this is bold text", drawFontBold, drawBrush, new RectangleF(350f + widthFirst, 250f, 647, 200));
}

2
投票

我建议使用:

float widthFirst = g.MeasureString("this is normal", drawFont).Width;

© www.soinside.com 2019 - 2024. All rights reserved.