开发者

How can I draw a Rectangle on a PictureBox?

开发者 https://www.devze.com 2022-12-24 01:41 出处:网络
I am trying to just draw a Rectangle on a PictureBox that is on a Form. Writing text like shown here works fine.

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));
}开发者_JAVA百科
}

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?


Well, you might have forgot to set rect.Height to something other than 0.

Did you check if the rectangle you're drawing has the correct dimensions?


Perhaps try

Rectangle rect = new Rectangle(new Point(25, 25), new Size(50, 50));

if you like it shorter.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号