Graphics.DrawString() vs TextRenderer.DrawText()

时光总嘲笑我的痴心妄想 提交于 2019-12-05 05:50:42

This does not appear to have anything to do with Graphics.DrawString or TextRenderer.DrawString or Console.Writeline.

The format specifier you are providing, {0.0%}, does not simply append a percentage sign. As per the MSDN documentation here, the % custom specifier ...

causes a number to be multiplied by 100 before it is formatted.

In my tests, both Graphics.DrawString and Console.WriteLine exhibit the same behavior when passed the same value and format specifier.

Console.WriteLine test:

class Program
{
    static void Main(string[] args)
    {
        double value = .5;
        var fv = string.Format("{0:0.0%}", value);
        Console.WriteLine(fv);
        Console.ReadLine();
    }
}

Graphics.DrawString Test:

public partial class Form1 : Form
{
    private PictureBox box = new PictureBox();

    public Form1()
    {
        InitializeComponent();
        this.Load += new EventHandler(Form1_Load);
    }

    public void Form1_Load(object sender, EventArgs e)
    {
        box.Dock = DockStyle.Fill;
        box.BackColor = Color.White;

        box.Paint += new PaintEventHandler(DrawTest);
        this.Controls.Add(box);
    }

    public void DrawTest(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        double value = .5;
        var fs = string.Format("{0:0.0%}", value);
        var font = new Font("Arial", 12);
        var brush = new SolidBrush(Color.Black);
        var point = new PointF(100.0F, 100.0F);

        g.DrawString(fs, font, brush, point);
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!