抽绳粗体和普通文字

时间:2014-06-14 12:00:23

标签: c#

有代码:

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));
            }

我需要

  

这是正常的,这是粗体文字

但是我第一次收到第二个文字的叠加

2 个答案:

答案 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);

    // 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));
}

答案 1 :(得分:2)

我建议使用:

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