Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone,

I am drawing graphics on an image. The problem which I am facing that when data Read thread begins the graphics starts plotting on an image in a PictureBox.

I want when the data thread stops the graphics disappear and then reappear when the data Read thread resume. please tell me how I can do it??

I am sharing the block of my code.

What I have tried:

bool draw;

private void Form1_Load(object sender, EventArgs e)
    {
        pictureBox1.Image = new Bitmap("Image.jpg");
        draw = true;
        Thread DataRead = new Thread (Data_Read);
        DataRead.Start();
    }
      public void DataRead()
    {
        //code for data reading//;
         
         if (data_read !=null)
           {
             draw = true;
           }
        else 
           {
             draw = false;
           }         
      }

private void picbox_paint(object sender, PaintEventArgs e)
    {
         if (draw == true)
        {
            e.Graphics.DrawImage(new Bitmap("interface.png"), Convert.ToInt32(x), Convert.ToInt32(w), 16, 16);
            e.Graphics.DrawImage(new Bitmap("interface.png"), Convert.ToInt32(z), Convert.ToInt32(y), 16, 16);
         }
         
        else
        {
          // what should I do in this part?
        }
 e.Dispose(); pictureBox1.invalidate();
    }
Posted
Updated 9-Jan-18 22:46pm
v2

1 solution

Simple: don't draw on the image. When you draw on an image, the changes are semi-permanent - they are "real" changes to the image itself, but they aren;t reflected in an file the image came from - so the only way to "undo" them is to reload the image itself from the original source.

If you want "temporary" graphics, then either place a transparent image over the top and draw on that, or store your changes and implement them in the Paint event for the PictureBox each time so they draw onto the graphics context you are supplied with. That way, the graphics you draw over the top can change each time the data does - all you have to do is add the new values to a collection to be drawn, and invalidate the PictureBox. If the data stops, you suspend drawing from your collection, and resume when the data does.
 
Share this answer
 
Comments
SN25 10-Jan-18 8:06am    
Thank you Griff for your reply
Can you please guide me how to place a transparent image over an image on a picture box

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900