如何在PictureBox上绘制矩形?

时间:2010-03-19 21:07:03

标签: c# winforms

我试图在Form上的PictureBox上绘制一个Rectangle。

编写如here所示的文字可以正常工作。

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

但是当我尝试画一个像这样的矩形时,没有任何东西出现。

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

我错过了什么?

2 个答案:

答案 0 :(得分:4)

好吧,您可能忘记将rect.Height设置为0以外的其他内容。

您是否检查过您正在绘制的矩形是否具有正确的尺寸?

答案 1 :(得分:0)

也许试试

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

如果你喜欢它更短。