Using FormattedText to create a Bitmap

后端 未结 1 1621
旧时难觅i
旧时难觅i 2021-01-25 19:40

In a C# forms project, I can write the following code to get something like what I want, but it seems that there are two different \"worlds\" that I am trying to fuse.



        
相关标签:
1条回答
  • 2021-01-25 20:27

    Yes, that's WPF code, an entirely different world. The System.Drawing version ought to resemble something like this:

    var bmp = new Bitmap(480, 480);
    using (var gr = Graphics.FromImage(bmp)) {
        gr.Clear(Color.White);
        TextRenderer.DrawText(gr, textBox1.Text, this.Font, 
            new Rectangle(0, 0, bmp.Width, bmp.Height), 
            Color.Black, Color.White,
            TextFormatFlags.WordBreak | TextFormatFlags.Left);
    }
    if (pictureBox1.Image != null) pictureBox1.Image.Dispose();
    pictureBox1.Image = bmp;
    

    I guessed at a picture box on the form.

    0 讨论(0)
提交回复
热议问题