?
Solved

C# Panel Graphics disappear

Posted on 2003-03-12
6
Medium Priority
?
4,306 Views
Last Modified: 2008-02-26
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
Comment
Question by:rsammut
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 1

Expert Comment

by:SimesA
ID: 8118330
Where do you draw the map? It needs to be in the Paint event handler.
0
 
LVL 1

Expert Comment

by:SimesA
ID: 8118363
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
 

Author Comment

by:rsammut
ID: 8118390
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rsammut
ID: 8118414
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
 
LVL 1

Accepted Solution

by:
SimesA earned 600 total points
ID: 8118601
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
 

Author Comment

by:rsammut
ID: 8118657
That worked great!!


thanks a lot,
Mr. R . Sammut
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Simple Linear Regression

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question