• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4485
  • Last Modified:

C# Panel Graphics disappear

Hi,

 I am new to the Panel in the VS.Net Environment.  I am using a Panel to draw a Map as follows (the map is made up of Lightblue points):

.
.
Graphics g = panel1.CreateGraphics();
//Looping through an ArrayList of WayPoints (an object I created)
foreach(WayPoint wp in arrListWayPoints)
{
   g.DrawEllipse(new Pen(Color.LightBlue),wp.x,wp.y,2,2);
}
.
.

Then I added a scrollbar to view the points outside of the boundaries of the panel. I had to add a label to the panel (outside the viewable area) so that it will scroll to that point as follows:

.
.
Label l = new Label();
l.Text = "";
l.Location = new System.Drawing.Point(784, 448); //784,448 is a point outside of the viewable area
panel1.Controls.Add(l);
panel1.AutoScroll = true;
.
.

Now the Problem is that when I move the scrollbars to view an area of the panel, the graphics disappear. I tried using the panel1.Refresh property but it did not help.  

any help is welcomed,
Mr. R. Sammut.
0
rsammut
Asked:
rsammut
  • 3
  • 3
1 Solution
 
SimesACommented:
Where do you draw the map? It needs to be in the Paint event handler.
0
 
SimesACommented:
A better solution would be to draw your map to an off-screen bitmap, then draw that to the panel in the Paint event. (Taking care to offset the image to reflect the position of the scroll bars.)

The map need only be drawn once, and it would prevent any flicker as the image was redrawn to the screen.
0
 
rsammutAuthor Commented:
Hi, Simes

No I'm not drawing the map in the paint event handler.  I need to draw the map when the user asks to and not every time the paint event is called.

Do you know of a good tutorial which explains how to save the panel to bitmap and loading it to the paint event?

thanks
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
rsammutAuthor Commented:
Hi, Simes

No I'm not drawing the map in the paint event handler.  I need to draw the map when the user asks to and not every time the paint event is called.

Do you know of a good tutorial which explains how to save the panel to bitmap and loading it to the paint event?

thanks
0
 
SimesACommented:
This might get you started:

private System.Drawing.Bitmap TheBitmap;

private void Form1_Load(object sender, System.EventArgs e) {
     Label l = new Label();
     l.Text = "";
     l.Location = new System.Drawing.Point(784, 448); //784,448 is a point outside of the viewable area
     panel1.Controls.Add(l);
     TheBitmap = new System.Drawing.Bitmap(784,448);
     DrawMap();
}

private void DrawMap() {
     Graphics g = Graphics.FromImage(TheBitmap);
     g.SmoothingMode = SmoothingMode.AntiAlias;
     g.FillRectangle(new SolidBrush(Color.White), 0,0,TheBitmap.Size.Width,TheBitmap.Size.Height);

     // draw the "map"
     Pen pen = new Pen(Color.Blue);
     g.DrawEllipse(pen,100,123,2,2);
     g.DrawEllipse(pen,89,34,2,2);
     g.DrawEllipse(pen,10,22,2,2);
     g.DrawEllipse(pen,45,55,2,2);
     g.DrawEllipse(pen,12,55,2,2);
     g.DrawEllipse(pen,66,192,2,2);
}

private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {
     Graphics g = e.Graphics;
     Point pt = new Point(panel1.AutoScrollPosition.X, panel1.AutoScrollPosition.Y);
     g.DrawImage(TheBitmap,pt);
}
0
 
rsammutAuthor Commented:
That worked great!!


thanks a lot,
Mr. R . Sammut
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now