0

I have a form that displays a set of graphics using a Paint event on a Panel that is docked inside a particular TabPage of a TabControl.

The problem is the following:

When the user switches to a different TabPage and then decides to go back to the TabPage where the graphics were originally displayed, those graphics are invalidated by default so the Panel appears blank.

I would like those graphics to stay unaltered and totally independent from the user's action when switching between different TabPages.

One Requirement:

Since the graphics are complex and take some time to be drawn by the computer, I don't want to repaint the graphics each time by calling the Paint event repeatedly. Instead, I only need to avoid the default invalidation of the graphics.

I have read this other question which may be helpful to solve my problem but it goes beyond my knowledge.

Community
  • 1
  • 1
codeaviator
  • 2,545
  • 17
  • 42
  • You can draw into a bitmap and set it to the panel's background image. – TaW Jun 28 '15 at 20:15
  • Woudn't work in my case, since the user can also modify the graphics by changing several parameters available in the form. – codeaviator Jun 28 '15 at 20:42
  • So? Where is the problem? Simply recreate the bitmap whenever the parameters change! To do so, simply move all drawing code from the Paint event to a draw function in which you create a Graphics object with `using (Graphics G = Graphics.FromImage(bmp)) { your drawing code...}` – TaW Jun 28 '15 at 20:45
  • There's no problem about the bitmap, but I want to keep the elegant vector layout of the graphics. – codeaviator Jun 28 '15 at 20:49
  • Huh? Whatever you draw in the paint event you can draw exactly in the same way. Where do you think it is drawn in the paint event?? On the control surface, which implictly is just another bitmap! Everything will be using the very same code and not one pixel will change.. – TaW Jun 28 '15 at 20:52
  • Understood, I didn't know that. But still, Is there any way, besides creating a bitmap, to hold the set of graphics in position? All in all those graphics are a set of data being plotted. – codeaviator Jun 28 '15 at 20:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81804/discussion-between-taw-and-cebri). – TaW Jun 28 '15 at 20:59

1 Answers1

0

If you want to cache your graphics you can draw everything into a bitmap and set it to the panel's background image.

Here is some example code, using a Control. Simply pass in your Panel:

void drawInto(Control ctl)
{
    Bitmap bmp = new Bitmap(ctl.ClientSize.Width, ctl.ClientSize.Height);

    using ( Graphics G = Graphics.FromImage(bmp))
    {
        // all your drawing code goes here..!
        G.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        G.DrawEllipse(Pens.DimGray, ctl.ClientRectangle);
        // ..
        // ..
    }
    ctl.BackgroundImage = bmp;
}

Just make sure to call the drawing function whenever necessary, as this is now your responsibility. The Resize event of the Panel is a good example of where you need to call it!

And changes in the data coming from the user are the obvious other reason to call it..

TaW
  • 53,122
  • 8
  • 69
  • 111