Graphics query, C#, .NET CF

N

Neville Lang

Hi all,

On a Pocket PC app, I have a TabControl with a number of TabPages. When a
user taps on one of the TabPages, I use Graphics to draw an image comprising
DrawLine, DrawEllipse and DrawString. I have found that each time you tap on
another TabPage and then come back the TabPage that has my image on it, the
OnPaint redraws it all again. While this is what should normally occur with
OnPaint, is there any way of saving that image in memory after it is drawn
and so on further OnPaints, instead of redrawing it again, the program just
shifts the saved copy back onto the surface?

In a related query, is there are way that I can create the above drawing in
the "background" before presenting it to the user on screen? At the moment,
the user can see each element being drawn due to slow graphics draws.

Regards,
Neville Lang
 
C

Chris Tacke, eMVP

Yes, use a cached bitmap. Darw to it, and then in OnPaint, just DrawImage
that bitmap to the Graphics.

-Chris
 
N

Neville Lang

Chris (or anyone else),

You mentioned to create a cached Bitmap (object) and draw to it. At the
moment, I have a method that contains something like the lines below:

Font font = new Font(this.Font.Name, this.MyfontSize, this.Font.Style);

pen.Color = cc.LineColor;

SolidBrush brush = new SolidBrush(clr);



grfx.DrawEllipse(pen, cc.Left, cc.Top, cc.Width, cc.Height);

grfx.DrawLine(pen, hl.Left, hl.Top, hl.Right, hl.Bottom);

grfx.DrawString(ci.String, font, brush, (float) tmpPt.X, (float) tmpPt.Y);

....
....

The question is: How do I change this code to draw to a BitMap image in
order to keep it aside and use it for DrawImage during OnPaint()?


Regards,
Neville Lang
 
G

Graham McKechnie

Neville,

Why can't you do it in a similar way to what I showed in those controls the
other night in the onPaint

// draw to memory bitmap
Graphics g = Graphics.FromImage(bmp);
DrawBackground(g);
DrawSatelliteStrength(g);
// blit memory bitmap to screen
e.Graphics.DrawImage(bmp, 0, 0);


Graham
 
N

Neville Lang

Graham,

Thanks for your reply. From your code, it seems that you are creating a
Graphics object from a bitmap image or file. I do not have a bitmap image to
begin with. Instead, I have a number of arrays with calculated coordinates
for each item to be drawn. What I am doing each time is drawing Ellipses,
Lines and Strings onto a surface (a TabPage) from these arrays of
coordinates.

My main question is how to generate the Ellipses, Lines and Strings in the
"background" to create a Bitmap image in order to display that image during
the OnPaint(). I can see from your code that once it is a Bitmap image, how
it can be drawn but not how to create the Bitmap in the first place.

Regards,
Neville
 
C

Chris Tacke, eMVP

Create a Bitmap variable in the class, in the ctor get a Graphics object
from it, then in your paint routinges, paint to that graphics and blit it
over.

-Chris
 
N

Neville Lang

Chris,

Thank you for the info. I have now worked it out.

Regards,
Neville Lang
 
N

Neville Lang

Chris and Graham,

Following both of your replies, I now have a better understanding of the
graphics part of C# and, of course, I have got what I wanted working
perfectly.

As an interesting side-effect, a previous post I issued on this NG (no reply
to it) where I was getting gaps in circles only on the device itself (iPAQ
3970) and not on the Emulator, has now been cleared up using this Bitmap
technique. It seems that when Graphics.DrawEllipse() is done directly on a
surface, like my TabPage when OnPaint() runs, then I get the gaps in the
circle on the device. Now that the circles are drawn in the "background"
well before the image is required to be displayed, the circles are complete,
as they should have been. I do not know why this was a problem but it seems
this is the workaround. Maybe it has something to do with
Graphics.DrawImage() doing a better job in rendering a circle. Thanks again.

Regards,
Neville Lang
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top