How to kept a graphic drawing in a picturebox on a

2019-02-27 17:06发布

问题:

I have a Tab Control with two (2) Tabs. The Tab 1 does a drawing in a picture box (the picture box is optional, I can draw directly to the tab) using Graphics Addline. The second tab opens a web browser. Everything is working fine. I can make the drawing in the first tab but when I switch to the second tab and return to the first tab, the drawing disappear and if I return to tab 2 I can see what I was watching in the web browser. I need to kept the drawing in the tab 1 so when I return to it I can see it. Here is the code I'm using to draw in the tab 1:

private void DataLoaded(ref string strFileName)  //strFileName has the data 
need for the drawing.
{

 Graphics g = this.pictureBox1.CreateGraphics();

 Pen black = new Pen(Color.Black, 5); 

 Pen green = new Pen(Color.Green, 5); 

 List<double> xpoints = new List<double>(); 

 List<double> ypoints = new List<double>();

 g.TranslateTransform(350, 350);

 g.DrawLine(green, new Point(Convert.ToInt32(X1), Convert.ToInt32(Y1)), new  
 Point(Convert.ToInt32(X2), Convert.ToInt32(Y2))); 

 for (int i = 2; i < xpoints.Count(); i++){

            g.DrawLine(black, new Point(Convert.ToInt32(X1), 
            Convert.ToInt32(Y1)), new Point(Convert.ToInt32(X2), 
            Convert.ToInt32(Y2))); 

            X1 = X2;                                            
            Y1 = Y2;                                            

            X2 = xpoints[i];                                    
            Y2 = ypoints[i];                                   

        }// end of for 

}

I even tried to do the drawing using the painteventarg but its not working at all. It helped me a bit because when I change back to the tab 1 and move the mouse over the tab it draws again the lines. Can anyone help me with this?? I even tried using this.picturebox1.Invalidate() but nothing. Like I said, what I need is: Preserve the drawing in tab 1 after switching to tab 2 so when I returned to tab 1 the lines are there. Thanks in advance for the help!!!.

回答1:

Its done, I just used a Bitmap to draw to it and the set the picturebox image with the bitmap.

The code I used is as follows:

Bitmap image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(image);

// In between all the code required for extracting the data and do the draw.

pictureBox1.Image = image;

Thanks anyway to whoever saw my question and try to answer it.