2010-03-19 106 views
0

我想在窗體上的PictureBox上畫一個矩形。如何在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); 
     } 
    } 

我在想什麼?

回答

4

那麼,您可能忘記將rect.Height設置爲0以外的值。

您是否檢查您繪製的矩形是否具有正確的尺寸?

+0

哇!我怎麼沒有注意到這一點。謝謝。 – etoisarobot 2010-03-19 21:16:01

0

也許嘗試

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

如果你喜歡它短。