【问题标题】:drawstring bold and normal text抽绳粗体和普通文本
【发布时间】:2016-07-28 10:43:25
【问题描述】:

有代码:

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

我需要得到

这是正常的这是粗体字

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

【问题讨论】:

  • 矩形指定位置。两个文本具有相同的坐标。
  • 在第二个 DrawString 中更改 647 和 200 以正确调用 MeasureString 方法来连接它

标签: c#


【解决方案1】:

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

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

【讨论】:

  • 解释你的答案比仅仅发布代码更有帮助。
  • 添加了cmets,上次有点着急。谢谢你的意见。
  • float widthFirst = ("this is normal").Length() * CharWidth — 你在开玩笑吗?! “Arial”不是等宽字体——即使是等宽字体也不会那样做(有变音符号«е»+«̈»=«ё»,韩文«ᄒ»+«ᅡ»+«ᇶ»= «하ᇶ»等)!只需float widthFirst = g.MeasureString("this is normal", drawFont).Width
【解决方案2】:

我建议使用:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多