How can I draw a Rectangle on a PictureBox?

Posted by TooFat on Stack Overflow See other posts from Stack Overflow or by TooFat
Published on 2010-03-19T21:07:03Z Indexed on 2010/03/19 21:11 UTC
Read the original article Hit count: 264

Filed under:
|

I am trying to just draw a Rectangle on a PictureBox that is on a Form.

Writing text like shown here works fine.

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));
}
}

but when I try to draw a rectangle like so nothing shows up.

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        Rectangle rect = new Rectangle();
        rect.Location = new Point(25, 25);
        rect.Width = 50;

        using (Pen pen = new Pen(Color.Red, 2))
        {
            e.Graphics.DrawRectangle(pen, rect);
        }
    }

What am I missing?

© Stack Overflow or respective owner

Related posts about c#

Related posts about winforms