simple graphics question

T

Tim

hi

I used to do this

Dim gfx As System.Drawing.Graphics = pic1.CreateGraphics
gfx.FillEllipse blah blah blah

to draw straight onto a form.
but this is frowned up (slow).

so what is the alternative?

does this line make the new graphics in memory?

Dim gfx As System.Drawing.Graphics = CreateGraphics()
gfx.FillEllipse blah blah blah

if so, how do I get the final image onto my form or picturebox?

many thanks
 
M

Mythran

Tim said:
hi

I used to do this

Dim gfx As System.Drawing.Graphics = pic1.CreateGraphics
gfx.FillEllipse blah blah blah

to draw straight onto a form.
but this is frowned up (slow).

so what is the alternative?

does this line make the new graphics in memory?

Dim gfx As System.Drawing.Graphics = CreateGraphics()
gfx.FillEllipse blah blah blah

if so, how do I get the final image onto my form or picturebox?

many thanks

Calling CreateGraphics is pretty slow (or at least slower than using the
existing Graphics object that is already there, in the OnPaint method). So,
override the OnPaint method and use the e.Graphics property :)

Mythran
 
T

Tim

interesting.

how do I limit when the onpaint gets called?
say I want to click a button to draw something.

also, I am drawing hundreds of circles on the form. I still think it
would be better to draw them to something in memory, and then dump the
finished thing to the form.

any tips?
 
M

Mythran

Tim said:
interesting.

how do I limit when the onpaint gets called?
say I want to click a button to draw something.

also, I am drawing hundreds of circles on the form. I still think it
would be better to draw them to something in memory, and then dump the
finished thing to the form.

any tips?

Ok, you really don't want to limit when onpaint gets called:

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e) ' This line will cause the form/control to paint
' itself. This also raised the Paint event for
' the form/control.

' Perform you custom paint functions here.
End Sub

What you can do, then, is to create a bitmap object and draw on that object
(of course, you'll need to create your own Graphics class to do this) and
then draw the bitmap onto the form/control using the Graphics object
provided by the OnPaint method. I'm not sure if it is faster though, since
you are creating another instance of the Graphics object on every
call...maybe, instead, you can somehow prevent the actual rendering of the
drawing until you have completed drawing the circles.

You can force the OnPaint method to be called by invalidating the client
area of the form (or just the region you want to repaint) by calling one of
the Invalidate method overloads for the form/control.

I'll check into it and get back to ya via ng.

HTH,
Mythran
 
M

Mythran

Mythran said:
Ok, you really don't want to limit when onpaint gets called:

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e) ' This line will cause the form/control to paint
' itself. This also raised the Paint event for
' the form/control.

' Perform you custom paint functions here.
End Sub

What you can do, then, is to create a bitmap object and draw on that
object (of course, you'll need to create your own Graphics class to do
this) and then draw the bitmap onto the form/control using the Graphics
object provided by the OnPaint method. I'm not sure if it is faster
though, since you are creating another instance of the Graphics object on
every call...maybe, instead, you can somehow prevent the actual rendering
of the drawing until you have completed drawing the circles.

You can force the OnPaint method to be called by invalidating the client
area of the form (or just the region you want to repaint) by calling one
of the Invalidate method overloads for the form/control.

I'll check into it and get back to ya via ng.

HTH,
Mythran

After checking, I believe what you want to do to prevent that actual
rendering of the control is to call SuspendLayout and then ResumeLayout when
you want to render.

HTH,
Mythran
 
G

Guest

I maintain a bitmap the same size as my control in memory then draw what I
want on the bitmap and when OnPaint is called, copy the bitmap to the
graphics object using bitblt. This can be lightning fast if programmed
correctly.
 

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

Similar Threads


Top