1

I try to remove newest drawline.

declaration

Bitmap DrawArea ;          // global variable
Bitmap Previuos_DrawArea;  // global variable

when I click button to draw a line

private void button2_Click_1(object sender, EventArgs e)
    {
        Graphics g = Graphics.FromImage(DrawArea);
        Previuos_DrawArea_img = DrawArea;
        g.(new Pen(Brushes.BlueViolet, 1.0F),0,10,10,20);
        pictureBox1.Image = DrawArea;
    }

when I click button to remove a line

private void button3_Click_1(object sender, EventArgs e)
    {
        pictureBox1.Image = Previuos_DrawArea_img;
    }

Concept :

1st step - Declare variable.

2nd step - Backup current picture.

3rd step - Draw new picture.

4th step - if undo just draw the backup picture.

Lennart
  • 9,657
  • 16
  • 68
  • 84
  • You may aslo want to consider combining drawing into the bitmap (which is what you have) and [drawing on top of a control](https://stackoverflow.com/questions/27337825/picturebox-paintevent-with-other-method/27341797?s=2|20.7580#27341797), ie your picturebox. Then you can have an apply button to draw the stuff into the bitmap but before you can have multiple undo and redos.. – TaW Aug 23 '18 at 08:39

1 Answers1

1

You aren’t creating a copy of the bitmap, you’re only storing it in two variables. They point to the same bitmap so editing one affects the other one. You’ll need to create a copy:

Previuos_DrawArea_img = new Bitmap(DrawArea);

Now it is a separate image and whatever you do to one of them doesn’t affect the other one.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74