这个问题相当简单。有没有办法在每次触发 Paint 事件时将绘制的内容保留在带有图形的面板上?
在一个简单的程序上演示:每 100 毫秒打开一次面板上的随机像素,直到面板全部着色。
简短的例子:
public partial class Form1 : Form
{
private Bitmap bitmap;
private Random random = new Random();
public Form1()
{
InitializeComponent();
bitmap = new Bitmap(panel1.Width,panel1.Height);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(bitmap,new Point(0,0));
}
private void button1_Click(object sender, EventArgs e)
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.DrawLine(
new Pen(new SolidBrush(Color.Black),1),
new Point(random.Next(0, bitmap.Width), random.Next(0, bitmap.Width)),
new Point(random.Next(0, bitmap.Width), random.Next(0, bitmap.Width)));
}
panel1.Invalidate(); // redraw
}
}