TextRenderer doesn't draw a long string

2019-02-26 09:45发布

Look at this sample:

public partial class Form1 : Form
{
    private static string myString = null;

    private const int MAX_TEXT = 5460;

    public Form1()
    {
        InitializeComponent();

        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < MAX_TEXT; i++)
        {
            builder.Append('a');
        }

        myString = builder.ToString();

        this.Paint += Form1_Paint;
    }

    void Form1_Paint(object sender, PaintEventArgs e)
    {
        TextRenderer.DrawText(
            e.Graphics,
            myString,
            this.Font,
            new Point(10, 30),
            Color.Black);
    }
}

When I set the MAX_TEXT to 5461, the string is not drawn. Do you know if the native mechanism has a limit to draw text, if/or I can setup the options to get it working?

1条回答
相关推荐>>
2楼-- · 2019-02-26 10:13

I think you hit the limitation of the TextRenderer class, which I think is calling the DrawTextEx API function under the hood. If you try to put your builder.ToString() results into a TextBox, it won't show up either.

If for some reason you need to print a string that long, you would have to revert back to the DrawString method:

e.Graphics.DrawString(myString, this.Font, Brushes.Black, new Point(10, 30));
查看更多
登录 后发表回答