How to draw text on picturebox?

后端 未结 1 1649
旧时难觅i
旧时难觅i 2021-02-05 11:59

I googled for \"Drawing text on picturebox C#\" ,but I couldnt find anything useful.Then I googled for \"Drawing text on form C#\" and I found some code,but it doesnt work the w

相关标签:
1条回答
  • 2021-02-05 12:34

    You don't want that call to Clear() - that's why it's turning the background white, and it will cover up your picture.

    You want to use the Paint event in the PictureBox. You get the graphics reference from e.Graphics, and then use the DrawString() that you have in your sample.

    Here's a sample. Just add a picture box to your form, and add an event handler for the Paint event:

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        using (Font myFont = new Font("Arial", 14))
        {
            e.Graphics.DrawString("Hello .NET Guide!", myFont, Brushes.Green, new Point(2, 2));
        }
    }
    

    (Note that you won't see the text at design time - you'll have to run the program for it to paint).

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