我编写了一个事件处理程序方法并将其附加到Paint
a 的事件Form
(只是主窗口)。此事件发送一个PaintEventArgs
包含一个名为 的属性Graphics
,它是 的一个实例System.Drawing.Graphics
。
这是我正在使用的代码:
private void Form1_Paint(object sender, PaintEventArgs e) {
Bitmap bm = new Bitmap("fruit-dealer-full.jpg");
Graphics g1 = this.CreateGraphics();
Graphics g2 = e.Graphics;
// g1.DrawImage(bm, 0, 0, this.Width, this.Height);
// g1.DrawRectangle(
// Pens.White, 10.0f, 10.0f, this.Width - 200, this.Height - 200);
g2.DrawImage(bm, 0, 0, this.Width, this.Height);
g2.DrawRectangle(
Pens.White, 10.0f, 10.0f, this.Width - 200, this.Height - 200);
}
最终,我只是想更好地了解这里发生的事情,但具体来说,我有以下三个问题:
- 为什么
g1
在整个窗口中重绘图像,而g2
只绘制新部分,即使我g2.Clear()
在绘制之前调用? - 为什么对于任
Graphics
一对象,图像仅在窗口增大时才重绘,而不是在窗口变小时时重绘? - 如果
PaintEventArgs.Graphics
可以(或不应该)用于绘图,它的用途是什么?我想Graphics
如果不需要重绘表单,它只会阻止您创建新实例;还有更多我想念的吗?