0
    private void Side_pictureBox_Paint(object sender, PaintEventArgs e)
        {

            if (doubleclicked == true)
            {


                Side_pictureBox.Refresh();
                for (int numbering_for_digram = 1; numbering_for_digram <= No_of_circle; numbering_for_digram++)
                {
                    //MessageBox.Show(numbering_for_digram.ToString());
                    String drawString = numbering_for_digram.ToString();
                    // Create font and brush.


                    Font drawFont = new Font("Calibri (Body)", 20);
                    SolidBrush drawBrush = new SolidBrush(Color.Blue);

                    // Create point for upper-left corner of drawing.
                    //float x = 0;
                    //float y = 0;
                    doubleclicked = false;
                    // Draw string to screen.

                    e.Graphics.DrawString(drawString, drawFont, drawBrush, lastPoint);




                    Side_pictureBox.Update();


                    //MessageBox.Show("passed graphics draw");


                }

            }
        }


        }

private void Side_pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown == true && Edit_Variables.add_remark_now==false)//check to see if the mouse button is down

            {

                if (lastPoint != null)//if our last point is not null, which in this case we have assigned above

                {

                    if (Side_pictureBox.Image == null)//if no available bitmap exists on the picturebox to draw on

                    {
                        //create a new bitmap
                        Bitmap bmp = new Bitmap(Side_pictureBox.Width, Side_pictureBox.Height);

                        Side_pictureBox.Image = bmp; //assign the picturebox.Image property to the bitmap created

                    }

                    using (Graphics g = Graphics.FromImage(Side_pictureBox.Image))

                    {//we need to create a Graphics object to draw on the picture box, its our main tool

                        //when making a Pen object, you can just give it color only or give it color and pen size


                        g.DrawLine(new Pen(Color.DarkRed, 2), lastPoint, e.Location);
                        g.SmoothingMode = SmoothingMode.AntiAlias;
                        //this is to give the drawing a more smoother, less sharper look


                    }

                    Side_pictureBox.Invalidate();//refreshes the picturebox

                    lastPoint = e.Location;//keep assigning the lastPoint to the current mouse position


                }

            }

This is how the whole process works, firstly I will draw using a "Pen" on an image. This drawing process starts with mousedown and ends with mouseup event. Once mouseup is detected, I am to required to click on a point within the image and it suppose to drawstring(draws "1" on the image) on the image itself. For the first drawstring event it turns out fine. However, once I trigger the mousedown event, it will remove the previous drawstring("1"). Can someone help how can i prevent the "previous" drawstring to be remove? Thank you all alot!

  • 1
    Keep __all__ data needed (maybe in List collections) and draw __all__ elements on __each__ Paint call! - Expandable example [DrawAction](https://stackoverflow.com/questions/28714411/update-a-drawing-without-deleting-the-previous-one/28716887?s=3|28.8492#28716887) – TaW Jan 12 '19 at 11:12
  • 1
    You're setting a new Image to the PicturBox.Image property every time you move the mouse (and you **never dispose of either the old one** or the Pen). You need to perform all your painting in the Paint event of the Control. If you need to add and remove different shapes, use a container of some sort. A List, for example. Add the new shape to it in the `MouseDown` event, where you create a new object. You will then finalize the object in the `MouseUp` event. If you need to preserve the painting, saving them in a Bitmap file, draw all the shapes in a Bitmap in one go, then save it to disc. – Jimi Jan 12 '19 at 11:19
  • Better, by using specialized classes as TaW is showning in the linked example. – Jimi Jan 12 '19 at 11:21
  • sorry for late reply! thx everyone for all your suggestion, I will give them I try and update my outcome! – user10748500 Jan 14 '19 at 00:53
  • @Jimi what is the purpose of dispose? Weird thing is I am able to draw multiple lines and non will dissapear, while only drawstring dissappears. – user10748500 Jan 14 '19 at 03:13
  • `Dispose` removes all references to an object an makes it eligible for Garbage Collection (so the memory it used can be re-assigned). You're drawing on a Bitmap (you shouldn't, see the previous comments). The string is instead drawn on the PictureBox surface. When you change the string, the new string is painted: nothing is there to redraw the old one. You should add all of you objects (Shapes, String, anything else than can be drawn) to a collection (a `List` is often used). Or use specialized classes that reference specific objects. All drawings need to be performed in the Paint event. – Jimi Jan 14 '19 at 03:42
  • sorry im still having trouble figuring out, can i have a example how to use List to store drawstring? – user10748500 Jan 14 '19 at 06:57
  • Sorry I'm new to programming. How do i store these variables (drawstring,drawFont,drawBrush,lastPoint) from e.Graphics.DrawString? i tried this "actions.Add(numbering_for_digram, drawFont,drawBrush, lastPoint);" what am i doing wrong? – user10748500 Jan 14 '19 at 07:39

1 Answers1

-1

Don't draw directly in the paint box. Draw in the Image of the PaintBox instead. And do this at the end of the your MouseMove event, before calling Side_pictureBox.Invalidate();

Nick
  • 4,787
  • 2
  • 18
  • 24