简体   繁体   中英

Keep things drawn on panel on refresh/invalidate

The question is fairly simple. Is there a way to keep the things drawn on a panel with Graphics everytime the Paint event fires?

Demonstrated on a simple program: Turn on a random pixel on the panel every 100 miliseconds until the panel is all colored.

Short example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM