Drawing to usercontrols and bitmaps

S

steve bull

I have a program in which I draw various shapes to a form but I would like to move it off the form into a user control.
The painting on the form takes a significant amount of time. I was thinking of making a bitmap map to the control so
that when it does a refresh it doesn't need to recalculate everything. It is easy using the form because I have clipping
and all the graphics draw routines(line, polygon etc). Is there a way to draw to a bitmap and use that to do refreshes.
From what I see of bitmap it does not give any facility for drawing shapes.

Any ideas would be welcome.
Thanks, Steve
 
T

Tim Wilson

You could enable double buffering using the protected SetStyle method, from
within the UserControl, to see if that helps.
http://msdn.microsoft.com/library/d...stemwindowsformscontrolclasssetstyletopic.asp

Or, if you still want to draw into your own Bitmap then here's one way to do
that, from within the OnPaint override.
Bitmap b = new Bitmap(this.ClientRectangle.Width,
this.ClientRectangle.Height);
Graphics g = Graphics.FromImage(b);
// g.DrawRectangle(...);
// g.DrawEllipse(...);
e.Graphics.DrawImage(b, 0, 0);

In addition, whenever you're going to Invalidate the control, try to use one
of the Invalidate overload that takes a specific area to invalidate.

--
Tim Wilson
..Net Compact Framework MVP

steve bull said:
I have a program in which I draw various shapes to a form but I would like
to move it off the form into a user control.
The painting on the form takes a significant amount of time. I was
thinking of making a bitmap map to the control so
that when it does a refresh it doesn't need to recalculate everything. It
is easy using the form because I have clipping
and all the graphics draw routines(line, polygon etc). Is there a way to
draw to a bitmap and use that to do refreshes.
 
S

steve bull

the 1st case sounds like it should work but the one thing I am not clear about is when the control is invalidated and it
gets another OnPaint message does it get repainted from the double buffer automatically or do I have to do something?

thanks.
 
T

Tim Wilson

Double buffering is essentially the same as drawing into a Bitmap and then
painting the Bitmap as an image to the screen. So the first case is like the
second case only you don't need to create a Bitmap and Graphics object
yourself. Just enable double buffering and draw normally in the OnPaint
override, you don't need to do anything special.

--
Tim Wilson
..Net Compact Framework MVP

steve bull said:
the 1st case sounds like it should work but the one thing I am not clear
about is when the control is invalidated and it
gets another OnPaint message does it get repainted from the double buffer
automatically or do I have to do something?
 

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