Graphics.DrawString() vs TextRenderer.DrawText()
Asked Answered
K

1

7

I am confused about these two methods.

My understanding is that Graphics.DrawString() uses GDI+ and is a graphics-based implementation, while TextRenderer.DrawString() uses GDI and allows a large range of fonts and supports unicode.

My issue is when I attempt to print decimal-based numbers as percentages to a printer. My research leads me to believe that TextRenderer is a better way to go.

However, MSDN advises, "The DrawText methods of TextRenderer are not supported for printing. You should always use the DrawString methods of the Graphics class."

My code to print using Graphics.DrawString is:

if (value != 0)
    e.Graphics.DrawString(String.Format("{0:0.0%}", value), GetFont("Arial", 12, "Regular"), GetBrush("Black"), HorizontalOffset + X, VerticleOffset + Y);

This prints "100%" for number between 0 and 1 and "-100% for numbers below zero.

When I place,

Console.WriteLine(String.Format("{0:0.0%}", value));

inside my print method, the value prints in the correct format (eg: 75.0%), so I am pretty sure the problem lies within Graphics.DrawString().

Kabuki answered 27/4, 2011 at 5:13 Comment(2)
Seems a bit odd. DrawString shouldn't be aware of any sort of formatting since String.Format simply returns a string. You might want to try a couple things: - In visual studio, break on the e.Graphics line, highlight the String.Format() parameter and hit Shift-F9 to see what is returned. Or put a literal in place of the format temporarily (i.e. "100.0%") to see if DrawString treats it differently.Ay
This behavior sounds like value is being treated as an integer. I'd suggest breaking out your String.Format into a separate string variable so that you can set a breakpoint on the Graphics.DrawString and examine the contents of value and the resulting string during debugging. I seriously doubt this is Graphics.DrawString doing this.Cerotype
J
2

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);
    }
}
Johns answered 6/2, 2012 at 17:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.