Home All Groups Group Topic Archive Search About

simple graphics question

Author
22 Jun 2006 2:33 PM
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

Author
22 Jun 2006 3:20 PM
Mythran
Show quote Hide quote
"Tim" <Citizen10Be***@gmail.com> wrote in message
news:1150986810.181813.13110@i40g2000cwc.googlegroups.com...
> 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
Author
22 Jun 2006 4:55 PM
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?



Mythran wrote:
Show quoteHide quote
> "Tim" <Citizen10Be***@gmail.com> wrote in message
> news:1150986810.181813.13110@i40g2000cwc.googlegroups.com...
> > 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
Author
22 Jun 2006 5:17 PM
Mythran
Show quote Hide quote
"Tim" <Citizen10Be***@gmail.com> wrote in message
news:1150995326.446477.165960@b68g2000cwa.googlegroups.com...
> 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
Author
22 Jun 2006 6:22 PM
Mythran
Show quote Hide quote
"Mythran" <kip_potter@hotmail.comREMOVETRAIL> wrote in message
news:OLFcu%23hlGHA.2304@TK2MSFTNGP02.phx.gbl...
>
> "Tim" <Citizen10Be***@gmail.com> wrote in message
> news:1150995326.446477.165960@b68g2000cwa.googlegroups.com...
>> 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
>

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
Author
22 Jun 2006 11:17 PM
Dennis
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.
--
Dennis in Houston


Show quoteHide quote
"Tim" wrote:

> 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?
>
>
>
> Mythran wrote:
> > "Tim" <Citizen10Be***@gmail.com> wrote in message
> > news:1150986810.181813.13110@i40g2000cwc.googlegroups.com...
> > > 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
>
>