在PictureBox控件上進行圖形繪制,可以通過以下步驟實現:
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
// 在這里編寫繪制圖形的代碼,例如繪制一個矩形
Pen pen = new Pen(Color.Red);
Rectangle rect = new Rectangle(10, 10, 100, 100);
e.Graphics.DrawRectangle(pen, rect);
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
// 繪制一個矩形
Pen pen = new Pen(Color.Red);
Rectangle rect = new Rectangle(10, 10, 100, 100);
g.DrawRectangle(pen, rect);
// 繪制一個橢圓
pen = new Pen(Color.Blue);
rect = new Rectangle(50, 50, 100, 50);
g.DrawEllipse(pen, rect);
}
通過以上兩種方法,在PictureBox控件上進行圖形繪制。可以根據需要繪制各種圖形,調整顏色、大小等屬性,實現自定義的圖形效果。