【问题标题】:How can I draw a Rectangle on a PictureBox?如何在 PictureBox 上绘制矩形?
【发布时间】:2010-03-20 05:11:04
【问题描述】:

我正在尝试在窗体上的 PictureBox 上绘制一个矩形。

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

我错过了什么?

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

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

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

    【讨论】:

    • 哇!我怎么没注意到。谢谢。
    【解决方案2】:

    不妨试试

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

    如果你喜欢它更短。

    【讨论】:

      猜你喜欢
      • 2018-07-06
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 2018-07-26
      • 2012-08-28
      • 1970-01-01
      • 2011-12-05
      相关资源
      最近更新 更多