user3072062
user3072062

Reputation: 93

How do i reset/clear all the drawings i did on pictureBox1?

I have a pictureBox1 with image inside and when i click on it its drawing points. Now i added a reset button i called it when i click on it its should clear all the drawings i did on the pictureBox and leavethe image inside without the drawings on it.

I did:

private void button4_Click(object sender, EventArgs e)
        {
            Graphics graphics;
            graphics = pictureBox1.CreateGraphics();
            graphics.DrawImage(pictureBox1.Image, 0, 0);
        }

So i draw a lot of points on pictureBox1 then click the button and all points are gone but then once i click on the picturebox1 again i see also the new points but also the old points i did before the clearing.

How can i clear the old drawings so it wont show up on the next clicks ?

This is the paint event: Moved the paint event to a new class:

public static void Paint(List<PointF> pb1points, GraphicsPath pb1gp, Point movingPoint, PictureBox pictureBox1, Graphics e)
        {
            e.Clear(Color.White);
            e.DrawImage(pictureBox1.Image, movingPoint);
            Pen p;
            p = new Pen(Brushes.Green);
            foreach (PointF pt in pb1points)
            {
                e.FillEllipse(Brushes.Red, pt.X, pt.Y, 3f, 3f);
            }
            using (Pen pp = new Pen(Color.Green, 2f))
            {
                pp.StartCap = pp.EndCap = LineCap.Round;
                pp.LineJoin = LineJoin.Round;
                e.DrawPath(pp, pb1gp);
            }
        }

Upvotes: 0

Views: 2761

Answers (3)

user3072062
user3072062

Reputation: 93

This is working:

private void button4_Click(object sender, EventArgs e)
        {
            Graphics graphics;
            graphics = pictureBox1.CreateGraphics();
            graphics.DrawImage(pictureBox1.Image, 0, 0);
            pb1points = new List<PointF>();
        }

Upvotes: 0

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28403

setting the Image property to null should work.

picBox.Image = null;

Ii it's not worked ,you might be used the InitialImage property to display your image.

pictBox.InitialImage = null;

Please refer the link: Clear image on picturebox

Upvotes: 0

Chun Lin
Chun Lin

Reputation: 544

You can try using Graphics.Clear().

Reference: http://msdn.microsoft.com/en-us/library/system.drawing.graphics.clear(v=vs.110).aspx

Upvotes: 1

Related Questions