473,323 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Should I store Graphics object ?

hi,

We are currently porting our project from VB6 to VB .NET.

Earlier we used to make scale transformations on objects like pictureBox ,
forms etc.Now Such transformations are made on the graphics object of the
form/pictureBox.

Should It be better if make a graphics object from my pictureBox in load
event handler of the form and store it as member variable of the form , make
transformations as and when required(we really require such transformations
from mils to pixels.). And finally I can dispose it when the form is
disposed.

But I have read somewhere that Graphics object should never be stored as
member varibale of a class.And so I am really confused as to what should I
do ?

Plz help me regarding this..

sanjay
Jul 21 '05 #1
12 2318
You should definately not store the graphics object. All the code using
Graphics should ideally be encapsulated within the Paint/OnPaint procedures.

"Sanjay" <sa***********@gmail.com> wrote in message
news:eS**************@TK2MSFTNGP15.phx.gbl...
hi,

We are currently porting our project from VB6 to VB .NET.

Earlier we used to make scale transformations on objects like pictureBox ,
forms etc.Now Such transformations are made on the graphics object of the
form/pictureBox.

Should It be better if make a graphics object from my pictureBox in load
event handler of the form and store it as member variable of the form ,
make
transformations as and when required(we really require such
transformations
from mils to pixels.). And finally I can dispose it when the form is
disposed.

But I have read somewhere that Graphics object should never be stored as
member varibale of a class.And so I am really confused as to what should I
do ?

Plz help me regarding this..

sanjay

Jul 21 '05 #2
No. Never.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Sanjay" <sa***********@gmail.com> wrote in message
news:eS**************@TK2MSFTNGP15.phx.gbl...
hi,

We are currently porting our project from VB6 to VB .NET.

Earlier we used to make scale transformations on objects like pictureBox ,
forms etc.Now Such transformations are made on the graphics object of the
form/pictureBox.

Should It be better if make a graphics object from my pictureBox in load
event handler of the form and store it as member variable of the form ,
make
transformations as and when required(we really require such
transformations
from mils to pixels.). And finally I can dispose it when the form is
disposed.

But I have read somewhere that Graphics object should never be stored as
member varibale of a class.And so I am really confused as to what should I
do ?

Plz help me regarding this..

sanjay

Jul 21 '05 #3
Hi Hederman,
thanks for your reply.
But our's is a different requirement.We do all our drawing in units of
mils(0.001 inch).And so we are doing a lot of transformations
to/from supported units to/from mils here and there.If I put all the
transformations in the paint,It will really be messy.Anyway it is not
possible with the current implementations.

Also could u plz make me clear as to why r u against making in a data
member.

I will be thankful to u for the same

thanking u
sanjay

----- Original Message -----
From: "Sean Hederman" <us***@blogentry.com>
Newsgroups:
microsoft.public.dotnet.framework,microsoft.public .dotnet.framework.drawing,
microsoft.public.dotnet.framework.windowsforms,mic rosoft.public.dotnet.gener
al,microsoft.public.dotnet.languages.vb
Sent: Friday, March 11, 2005 6:10 PM
Subject: Re: Should I store Graphics object ?

You should definately not store the graphics object. All the code using
Graphics should ideally be encapsulated within the Paint/OnPaint procedures.
"Sanjay" <sa***********@gmail.com> wrote in message
news:eS**************@TK2MSFTNGP15.phx.gbl...
hi,

We are currently porting our project from VB6 to VB .NET.

Earlier we used to make scale transformations on objects like pictureBox , forms etc.Now Such transformations are made on the graphics object of the form/pictureBox.

Should It be better if make a graphics object from my pictureBox in load
event handler of the form and store it as member variable of the form ,
make
transformations as and when required(we really require such
transformations
from mils to pixels.). And finally I can dispose it when the form is
disposed.

But I have read somewhere that Graphics object should never be stored as
member varibale of a class.And so I am really confused as to what should I do ?

Plz help me regarding this..

sanjay


Jul 21 '05 #4
If you need to store the effects of transformations, what you can do is have
Matrix member variable that you store your transformations in. Apply the
changes to the Matrix in your various procedures, and then apply the Matrix
to the Graphics object in the Paint.

As for why you shouldn't store the Graphics object, there's no guarantee
that the Graphics object has any meaning outside the Paint event. For
example, when double bufferring is enabled, then the Graphics object being
passed to your Paint relates to a GraphicsBuffer object rather than the
actual drawing surface. More importantly, the next time Paint is invoked,
you're given a different GraphicsBuffer object. Thus, painting on your
stored Graphics object will not result in the painting being on the drawing
surface. In addition, since there's no guarantee, future versions of .NET
may break your code even if you don't use double buffering.

"Sanjay" <sa***********@gmail.com> wrote in message
news:ed**************@TK2MSFTNGP15.phx.gbl...
Hi Hederman,
thanks for your reply.
But our's is a different requirement.We do all our drawing in units of
mils(0.001 inch).And so we are doing a lot of transformations
to/from supported units to/from mils here and there.If I put all the
transformations in the paint,It will really be messy.Anyway it is not
possible with the current implementations.

Also could u plz make me clear as to why r u against making in a data
member.

I will be thankful to u for the same

thanking u
sanjay

----- Original Message -----
From: "Sean Hederman" <us***@blogentry.com>
Newsgroups:
microsoft.public.dotnet.framework,microsoft.public .dotnet.framework.drawing,
microsoft.public.dotnet.framework.windowsforms,mic rosoft.public.dotnet.gener
al,microsoft.public.dotnet.languages.vb
Sent: Friday, March 11, 2005 6:10 PM
Subject: Re: Should I store Graphics object ?

You should definately not store the graphics object. All the code using
Graphics should ideally be encapsulated within the Paint/OnPaint

procedures.

"Sanjay" <sa***********@gmail.com> wrote in message
news:eS**************@TK2MSFTNGP15.phx.gbl...
> hi,
>
> We are currently porting our project from VB6 to VB .NET.
>
> Earlier we used to make scale transformations on objects like
> pictureBox , > forms etc.Now Such transformations are made on the graphics object of the > form/pictureBox.
>
> Should It be better if make a graphics object from my pictureBox in
> load
> event handler of the form and store it as member variable of the form ,
> make
> transformations as and when required(we really require such
> transformations
> from mils to pixels.). And finally I can dispose it when the form is
> disposed.
>
> But I have read somewhere that Graphics object should never be stored
> as
> member varibale of a class.And so I am really confused as to what
> should I > do ?
>
> Plz help me regarding this..
>
> sanjay



Jul 21 '05 #5
Why don't you apply your transform and store it in a Bitmap object?

"Sanjay" wrote:
Hi Hederman,
thanks for your reply.
But our's is a different requirement.We do all our drawing in units of
mils(0.001 inch).And so we are doing a lot of transformations
to/from supported units to/from mils here and there.If I put all the
transformations in the paint,It will really be messy.Anyway it is not
possible with the current implementations.

Also could u plz make me clear as to why r u against making in a data
member.

I will be thankful to u for the same

thanking u
sanjay

----- Original Message -----
From: "Sean Hederman" <us***@blogentry.com>
Newsgroups:
microsoft.public.dotnet.framework,microsoft.public .dotnet.framework.drawing,
microsoft.public.dotnet.framework.windowsforms,mic rosoft.public.dotnet.gener
al,microsoft.public.dotnet.languages.vb
Sent: Friday, March 11, 2005 6:10 PM
Subject: Re: Should I store Graphics object ?

You should definately not store the graphics object. All the code using
Graphics should ideally be encapsulated within the Paint/OnPaint

procedures.

"Sanjay" <sa***********@gmail.com> wrote in message
news:eS**************@TK2MSFTNGP15.phx.gbl...
hi,

We are currently porting our project from VB6 to VB .NET.

Earlier we used to make scale transformations on objects like pictureBox , forms etc.Now Such transformations are made on the graphics object of the form/pictureBox.

Should It be better if make a graphics object from my pictureBox in load
event handler of the form and store it as member variable of the form ,
make
transformations as and when required(we really require such
transformations
from mils to pixels.). And finally I can dispose it when the form is
disposed.

But I have read somewhere that Graphics object should never be stored as
member varibale of a class.And so I am really confused as to what should I do ?

Plz help me regarding this..

sanjay



Jul 21 '05 #6
I'm curious as to where M'soft documents that future versions may use double
buffering as the default? Can you cite a link or reference? It's very
convientent to use a class graphics variable to update only parts of the
display without recalculating and re-displaying the entire control.

"Sean Hederman" wrote:
If you need to store the effects of transformations, what you can do is have
Matrix member variable that you store your transformations in. Apply the
changes to the Matrix in your various procedures, and then apply the Matrix
to the Graphics object in the Paint.

As for why you shouldn't store the Graphics object, there's no guarantee
that the Graphics object has any meaning outside the Paint event. For
example, when double bufferring is enabled, then the Graphics object being
passed to your Paint relates to a GraphicsBuffer object rather than the
actual drawing surface. More importantly, the next time Paint is invoked,
you're given a different GraphicsBuffer object. Thus, painting on your
stored Graphics object will not result in the painting being on the drawing
surface. In addition, since there's no guarantee, future versions of .NET
may break your code even if you don't use double buffering.

"Sanjay" <sa***********@gmail.com> wrote in message
news:ed**************@TK2MSFTNGP15.phx.gbl...
Hi Hederman,
thanks for your reply.
But our's is a different requirement.We do all our drawing in units of
mils(0.001 inch).And so we are doing a lot of transformations
to/from supported units to/from mils here and there.If I put all the
transformations in the paint,It will really be messy.Anyway it is not
possible with the current implementations.

Also could u plz make me clear as to why r u against making in a data
member.

I will be thankful to u for the same

thanking u
sanjay

----- Original Message -----
From: "Sean Hederman" <us***@blogentry.com>
Newsgroups:
microsoft.public.dotnet.framework,microsoft.public .dotnet.framework.drawing,
microsoft.public.dotnet.framework.windowsforms,mic rosoft.public.dotnet.gener
al,microsoft.public.dotnet.languages.vb
Sent: Friday, March 11, 2005 6:10 PM
Subject: Re: Should I store Graphics object ?

You should definately not store the graphics object. All the code using
Graphics should ideally be encapsulated within the Paint/OnPaint

procedures.

"Sanjay" <sa***********@gmail.com> wrote in message
news:eS**************@TK2MSFTNGP15.phx.gbl...
> hi,
>
> We are currently porting our project from VB6 to VB .NET.
>
> Earlier we used to make scale transformations on objects like
> pictureBox

,
> forms etc.Now Such transformations are made on the graphics object of

the
> form/pictureBox.
>
> Should It be better if make a graphics object from my pictureBox in
> load
> event handler of the form and store it as member variable of the form ,
> make
> transformations as and when required(we really require such
> transformations
> from mils to pixels.). And finally I can dispose it when the form is
> disposed.
>
> But I have read somewhere that Graphics object should never be stored
> as
> member varibale of a class.And so I am really confused as to what
> should

I
> do ?
>
> Plz help me regarding this..
>
> sanjay



Jul 21 '05 #7
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
I'm curious as to where M'soft documents that future versions may use
double
buffering as the default? Can you cite a link or reference? It's very
convientent to use a class graphics variable to update only parts of the
display without recalculating and re-displaying the entire control.
They don't. There are two issues here. One is that if you ever decide to
switch on double bufferring, then all your code outside of Paint could be
rendered useless. The other is that there are no guarantees about the
Graphics object outside Paint. Since there are no guarantees, using the
Graphics object elsewhere *could* be invalidated by future versions of .NET.
Using Graphics outside Paint, knowing that it wraps an HDC is breaking
encapsulation.

As to updating only parts of your display, there are two main approaches you
could follow here:
1. Use the Invalidate() overrides to only invalidate the parts of your
control you're interested in, and only paint those items within the
Graphics.ClipRectangle.
2. Roll your own double-bufferring system. Create a Bitmap object with the
same characteristics as your control, generate a Graphics object from that,
and manipulate that where you see fit. In your Paint, just blit the Bitmap
onto your Control.
"Sean Hederman" wrote:
If you need to store the effects of transformations, what you can do is
have
Matrix member variable that you store your transformations in. Apply the
changes to the Matrix in your various procedures, and then apply the
Matrix
to the Graphics object in the Paint.

As for why you shouldn't store the Graphics object, there's no guarantee
that the Graphics object has any meaning outside the Paint event. For
example, when double bufferring is enabled, then the Graphics object
being
passed to your Paint relates to a GraphicsBuffer object rather than the
actual drawing surface. More importantly, the next time Paint is invoked,
you're given a different GraphicsBuffer object. Thus, painting on your
stored Graphics object will not result in the painting being on the
drawing
surface. In addition, since there's no guarantee, future versions of .NET
may break your code even if you don't use double buffering.

"Sanjay" <sa***********@gmail.com> wrote in message
news:ed**************@TK2MSFTNGP15.phx.gbl...
> Hi Hederman,
> thanks for your reply.
> But our's is a different requirement.We do all our drawing in units of
> mils(0.001 inch).And so we are doing a lot of transformations
> to/from supported units to/from mils here and there.If I put all the
> transformations in the paint,It will really be messy.Anyway it is not
> possible with the current implementations.
>
> Also could u plz make me clear as to why r u against making in a data
> member.
>
> I will be thankful to u for the same
>
> thanking u
> sanjay
>
> ----- Original Message -----
> From: "Sean Hederman" <us***@blogentry.com>
> Newsgroups:
> microsoft.public.dotnet.framework,microsoft.public .dotnet.framework.drawing,
> microsoft.public.dotnet.framework.windowsforms,mic rosoft.public.dotnet.gener
> al,microsoft.public.dotnet.languages.vb
> Sent: Friday, March 11, 2005 6:10 PM
> Subject: Re: Should I store Graphics object ?
>
>
>> You should definately not store the graphics object. All the code
>> using
>> Graphics should ideally be encapsulated within the Paint/OnPaint
> procedures.
>>
>> "Sanjay" <sa***********@gmail.com> wrote in message
>> news:eS**************@TK2MSFTNGP15.phx.gbl...
>> > hi,
>> >
>> > We are currently porting our project from VB6 to VB .NET.
>> >
>> > Earlier we used to make scale transformations on objects like
>> > pictureBox
> ,
>> > forms etc.Now Such transformations are made on the graphics object
>> > of
> the
>> > form/pictureBox.
>> >
>> > Should It be better if make a graphics object from my pictureBox in
>> > load
>> > event handler of the form and store it as member variable of the
>> > form ,
>> > make
>> > transformations as and when required(we really require such
>> > transformations
>> > from mils to pixels.). And finally I can dispose it when the form is
>> > disposed.
>> >
>> > But I have read somewhere that Graphics object should never be
>> > stored
>> > as
>> > member varibale of a class.And so I am really confused as to what
>> > should
> I
>> > do ?
>> >
>> > Plz help me regarding this..
>> >
>> > sanjay
>>
>>
>
>


Jul 21 '05 #8
Thanks. I like the double buffering idea and would chose that if I decide to
change my program as this is the least amount of re-programming. I would
assume the bltbit transfer of the bitmap is almost as quick as the double
buffering. In any event, there are only two guarantees in life, i.e., you
will pay taxes and you will die!

"Sean Hederman" wrote:
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
I'm curious as to where M'soft documents that future versions may use
double
buffering as the default? Can you cite a link or reference? It's very
convientent to use a class graphics variable to update only parts of the
display without recalculating and re-displaying the entire control.


They don't. There are two issues here. One is that if you ever decide to
switch on double bufferring, then all your code outside of Paint could be
rendered useless. The other is that there are no guarantees about the
Graphics object outside Paint. Since there are no guarantees, using the
Graphics object elsewhere *could* be invalidated by future versions of .NET.
Using Graphics outside Paint, knowing that it wraps an HDC is breaking
encapsulation.

As to updating only parts of your display, there are two main approaches you
could follow here:
1. Use the Invalidate() overrides to only invalidate the parts of your
control you're interested in, and only paint those items within the
Graphics.ClipRectangle.
2. Roll your own double-bufferring system. Create a Bitmap object with the
same characteristics as your control, generate a Graphics object from that,
and manipulate that where you see fit. In your Paint, just blit the Bitmap
onto your Control.
"Sean Hederman" wrote:
If you need to store the effects of transformations, what you can do is
have
Matrix member variable that you store your transformations in. Apply the
changes to the Matrix in your various procedures, and then apply the
Matrix
to the Graphics object in the Paint.

As for why you shouldn't store the Graphics object, there's no guarantee
that the Graphics object has any meaning outside the Paint event. For
example, when double bufferring is enabled, then the Graphics object
being
passed to your Paint relates to a GraphicsBuffer object rather than the
actual drawing surface. More importantly, the next time Paint is invoked,
you're given a different GraphicsBuffer object. Thus, painting on your
stored Graphics object will not result in the painting being on the
drawing
surface. In addition, since there's no guarantee, future versions of .NET
may break your code even if you don't use double buffering.

"Sanjay" <sa***********@gmail.com> wrote in message
news:ed**************@TK2MSFTNGP15.phx.gbl...
> Hi Hederman,
> thanks for your reply.
> But our's is a different requirement.We do all our drawing in units of
> mils(0.001 inch).And so we are doing a lot of transformations
> to/from supported units to/from mils here and there.If I put all the
> transformations in the paint,It will really be messy.Anyway it is not
> possible with the current implementations.
>
> Also could u plz make me clear as to why r u against making in a data
> member.
>
> I will be thankful to u for the same
>
> thanking u
> sanjay
>
> ----- Original Message -----
> From: "Sean Hederman" <us***@blogentry.com>
> Newsgroups:
> microsoft.public.dotnet.framework,microsoft.public .dotnet.framework.drawing,
> microsoft.public.dotnet.framework.windowsforms,mic rosoft.public.dotnet.gener
> al,microsoft.public.dotnet.languages.vb
> Sent: Friday, March 11, 2005 6:10 PM
> Subject: Re: Should I store Graphics object ?
>
>
>> You should definately not store the graphics object. All the code
>> using
>> Graphics should ideally be encapsulated within the Paint/OnPaint
> procedures.
>>
>> "Sanjay" <sa***********@gmail.com> wrote in message
>> news:eS**************@TK2MSFTNGP15.phx.gbl...
>> > hi,
>> >
>> > We are currently porting our project from VB6 to VB .NET.
>> >
>> > Earlier we used to make scale transformations on objects like
>> > pictureBox
> ,
>> > forms etc.Now Such transformations are made on the graphics object
>> > of
> the
>> > form/pictureBox.
>> >
>> > Should It be better if make a graphics object from my pictureBox in
>> > load
>> > event handler of the form and store it as member variable of the
>> > form ,
>> > make
>> > transformations as and when required(we really require such
>> > transformations
>> > from mils to pixels.). And finally I can dispose it when the form is
>> > disposed.
>> >
>> > But I have read somewhere that Graphics object should never be
>> > stored
>> > as
>> > member varibale of a class.And so I am really confused as to what
>> > should
> I
>> > do ?
>> >
>> > Plz help me regarding this..
>> >
>> > sanjay
>>
>>
>
>


Jul 21 '05 #9
Let me ask you another question if you don't mind. I have a User Control
Class that inheiits from the Panel Class. When my control is instaniated, I
create a graphics object (myGraphics as Graphics = new Me.CreateGrahics) I
maintain this graphics object and draw on it to repaint parts of the control
or all of the control which ever is required depending on what the user does.
If the control is resized, I recreate myGraphics object in the sizechanged
event.

If I understand you correctly, this will not work under future versions of
..net..is that correct.

"Sean Hederman" wrote:
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
I'm curious as to where M'soft documents that future versions may use
double
buffering as the default? Can you cite a link or reference? It's very
convientent to use a class graphics variable to update only parts of the
display without recalculating and re-displaying the entire control.


They don't. There are two issues here. One is that if you ever decide to
switch on double bufferring, then all your code outside of Paint could be
rendered useless. The other is that there are no guarantees about the
Graphics object outside Paint. Since there are no guarantees, using the
Graphics object elsewhere *could* be invalidated by future versions of .NET.
Using Graphics outside Paint, knowing that it wraps an HDC is breaking
encapsulation.

As to updating only parts of your display, there are two main approaches you
could follow here:
1. Use the Invalidate() overrides to only invalidate the parts of your
control you're interested in, and only paint those items within the
Graphics.ClipRectangle.
2. Roll your own double-bufferring system. Create a Bitmap object with the
same characteristics as your control, generate a Graphics object from that,
and manipulate that where you see fit. In your Paint, just blit the Bitmap
onto your Control.
"Sean Hederman" wrote:
If you need to store the effects of transformations, what you can do is
have
Matrix member variable that you store your transformations in. Apply the
changes to the Matrix in your various procedures, and then apply the
Matrix
to the Graphics object in the Paint.

As for why you shouldn't store the Graphics object, there's no guarantee
that the Graphics object has any meaning outside the Paint event. For
example, when double bufferring is enabled, then the Graphics object
being
passed to your Paint relates to a GraphicsBuffer object rather than the
actual drawing surface. More importantly, the next time Paint is invoked,
you're given a different GraphicsBuffer object. Thus, painting on your
stored Graphics object will not result in the painting being on the
drawing
surface. In addition, since there's no guarantee, future versions of .NET
may break your code even if you don't use double buffering.

"Sanjay" <sa***********@gmail.com> wrote in message
news:ed**************@TK2MSFTNGP15.phx.gbl...
> Hi Hederman,
> thanks for your reply.
> But our's is a different requirement.We do all our drawing in units of
> mils(0.001 inch).And so we are doing a lot of transformations
> to/from supported units to/from mils here and there.If I put all the
> transformations in the paint,It will really be messy.Anyway it is not
> possible with the current implementations.
>
> Also could u plz make me clear as to why r u against making in a data
> member.
>
> I will be thankful to u for the same
>
> thanking u
> sanjay
>
> ----- Original Message -----
> From: "Sean Hederman" <us***@blogentry.com>
> Newsgroups:
> microsoft.public.dotnet.framework,microsoft.public .dotnet.framework.drawing,
> microsoft.public.dotnet.framework.windowsforms,mic rosoft.public.dotnet.gener
> al,microsoft.public.dotnet.languages.vb
> Sent: Friday, March 11, 2005 6:10 PM
> Subject: Re: Should I store Graphics object ?
>
>
>> You should definately not store the graphics object. All the code
>> using
>> Graphics should ideally be encapsulated within the Paint/OnPaint
> procedures.
>>
>> "Sanjay" <sa***********@gmail.com> wrote in message
>> news:eS**************@TK2MSFTNGP15.phx.gbl...
>> > hi,
>> >
>> > We are currently porting our project from VB6 to VB .NET.
>> >
>> > Earlier we used to make scale transformations on objects like
>> > pictureBox
> ,
>> > forms etc.Now Such transformations are made on the graphics object
>> > of
> the
>> > form/pictureBox.
>> >
>> > Should It be better if make a graphics object from my pictureBox in
>> > load
>> > event handler of the form and store it as member variable of the
>> > form ,
>> > make
>> > transformations as and when required(we really require such
>> > transformations
>> > from mils to pixels.). And finally I can dispose it when the form is
>> > disposed.
>> >
>> > But I have read somewhere that Graphics object should never be
>> > stored
>> > as
>> > member varibale of a class.And so I am really confused as to what
>> > should
> I
>> > do ?
>> >
>> > Plz help me regarding this..
>> >
>> > sanjay
>>
>>
>
>


Jul 21 '05 #10
*May* not work. No guarantees one way or another. As for using
CreateGraphics, it should only be used to get information about the graphics
surface and not to paint on. Have a look at
http://www.bobpowell.net/creategraphics.htm for more details.

Basically if you replace all your CreateGraphics with a bitmap Graphics
creation, and your Paint to bitblt the Bitmap, you should be fine, and be
able to keep your code pretty much as it is.

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:51**********************************@microsof t.com...
Let me ask you another question if you don't mind. I have a User Control
Class that inheiits from the Panel Class. When my control is instaniated,
I
create a graphics object (myGraphics as Graphics = new Me.CreateGrahics)
I
maintain this graphics object and draw on it to repaint parts of the
control
or all of the control which ever is required depending on what the user
does.
If the control is resized, I recreate myGraphics object in the sizechanged
event.

If I understand you correctly, this will not work under future versions of
.net..is that correct.

"Sean Hederman" wrote:
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
> I'm curious as to where M'soft documents that future versions may use
> double
> buffering as the default? Can you cite a link or reference? It's very
> convientent to use a class graphics variable to update only parts of
> the
> display without recalculating and re-displaying the entire control.


They don't. There are two issues here. One is that if you ever decide to
switch on double bufferring, then all your code outside of Paint could be
rendered useless. The other is that there are no guarantees about the
Graphics object outside Paint. Since there are no guarantees, using the
Graphics object elsewhere *could* be invalidated by future versions of
.NET.
Using Graphics outside Paint, knowing that it wraps an HDC is breaking
encapsulation.

As to updating only parts of your display, there are two main approaches
you
could follow here:
1. Use the Invalidate() overrides to only invalidate the parts of your
control you're interested in, and only paint those items within the
Graphics.ClipRectangle.
2. Roll your own double-bufferring system. Create a Bitmap object with
the
same characteristics as your control, generate a Graphics object from
that,
and manipulate that where you see fit. In your Paint, just blit the
Bitmap
onto your Control.
> "Sean Hederman" wrote:
>
>> If you need to store the effects of transformations, what you can do
>> is
>> have
>> Matrix member variable that you store your transformations in. Apply
>> the
>> changes to the Matrix in your various procedures, and then apply the
>> Matrix
>> to the Graphics object in the Paint.
>>
>> As for why you shouldn't store the Graphics object, there's no
>> guarantee
>> that the Graphics object has any meaning outside the Paint event. For
>> example, when double bufferring is enabled, then the Graphics object
>> being
>> passed to your Paint relates to a GraphicsBuffer object rather than
>> the
>> actual drawing surface. More importantly, the next time Paint is
>> invoked,
>> you're given a different GraphicsBuffer object. Thus, painting on your
>> stored Graphics object will not result in the painting being on the
>> drawing
>> surface. In addition, since there's no guarantee, future versions of
>> .NET
>> may break your code even if you don't use double buffering.
>>
>> "Sanjay" <sa***********@gmail.com> wrote in message
>> news:ed**************@TK2MSFTNGP15.phx.gbl...
>> > Hi Hederman,
>> > thanks for your reply.
>> > But our's is a different requirement.We do all our drawing in units
>> > of
>> > mils(0.001 inch).And so we are doing a lot of transformations
>> > to/from supported units to/from mils here and there.If I put all the
>> > transformations in the paint,It will really be messy.Anyway it is
>> > not
>> > possible with the current implementations.
>> >
>> > Also could u plz make me clear as to why r u against making in a
>> > data
>> > member.
>> >
>> > I will be thankful to u for the same
>> >
>> > thanking u
>> > sanjay
>> >
>> > ----- Original Message -----
>> > From: "Sean Hederman" <us***@blogentry.com>
>> > Newsgroups:
>> > microsoft.public.dotnet.framework,microsoft.public .dotnet.framework.drawing,
>> > microsoft.public.dotnet.framework.windowsforms,mic rosoft.public.dotnet.gener
>> > al,microsoft.public.dotnet.languages.vb
>> > Sent: Friday, March 11, 2005 6:10 PM
>> > Subject: Re: Should I store Graphics object ?
>> >
>> >
>> >> You should definately not store the graphics object. All the code
>> >> using
>> >> Graphics should ideally be encapsulated within the Paint/OnPaint
>> > procedures.
>> >>
>> >> "Sanjay" <sa***********@gmail.com> wrote in message
>> >> news:eS**************@TK2MSFTNGP15.phx.gbl...
>> >> > hi,
>> >> >
>> >> > We are currently porting our project from VB6 to VB .NET.
>> >> >
>> >> > Earlier we used to make scale transformations on objects like
>> >> > pictureBox
>> > ,
>> >> > forms etc.Now Such transformations are made on the graphics
>> >> > object
>> >> > of
>> > the
>> >> > form/pictureBox.
>> >> >
>> >> > Should It be better if make a graphics object from my pictureBox
>> >> > in
>> >> > load
>> >> > event handler of the form and store it as member variable of the
>> >> > form ,
>> >> > make
>> >> > transformations as and when required(we really require such
>> >> > transformations
>> >> > from mils to pixels.). And finally I can dispose it when the form
>> >> > is
>> >> > disposed.
>> >> >
>> >> > But I have read somewhere that Graphics object should never be
>> >> > stored
>> >> > as
>> >> > member varibale of a class.And so I am really confused as to what
>> >> > should
>> > I
>> >> > do ?
>> >> >
>> >> > Plz help me regarding this..
>> >> >
>> >> > sanjay
>> >>
>> >>
>> >
>> >
>>
>>
>>


Jul 21 '05 #11
Thanks. It just so happens that I have a utility bitblt routine that copies
a bitmap to a Graphics object so can easily use this

"Sean Hederman" wrote:
*May* not work. No guarantees one way or another. As for using
CreateGraphics, it should only be used to get information about the graphics
surface and not to paint on. Have a look at
http://www.bobpowell.net/creategraphics.htm for more details.

Basically if you replace all your CreateGraphics with a bitmap Graphics
creation, and your Paint to bitblt the Bitmap, you should be fine, and be
able to keep your code pretty much as it is.

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:51**********************************@microsof t.com...
Let me ask you another question if you don't mind. I have a User Control
Class that inheiits from the Panel Class. When my control is instaniated,
I
create a graphics object (myGraphics as Graphics = new Me.CreateGrahics)
I
maintain this graphics object and draw on it to repaint parts of the
control
or all of the control which ever is required depending on what the user
does.
If the control is resized, I recreate myGraphics object in the sizechanged
event.

If I understand you correctly, this will not work under future versions of
.net..is that correct.

"Sean Hederman" wrote:
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
> I'm curious as to where M'soft documents that future versions may use
> double
> buffering as the default? Can you cite a link or reference? It's very
> convientent to use a class graphics variable to update only parts of
> the
> display without recalculating and re-displaying the entire control.

They don't. There are two issues here. One is that if you ever decide to
switch on double bufferring, then all your code outside of Paint could be
rendered useless. The other is that there are no guarantees about the
Graphics object outside Paint. Since there are no guarantees, using the
Graphics object elsewhere *could* be invalidated by future versions of
.NET.
Using Graphics outside Paint, knowing that it wraps an HDC is breaking
encapsulation.

As to updating only parts of your display, there are two main approaches
you
could follow here:
1. Use the Invalidate() overrides to only invalidate the parts of your
control you're interested in, and only paint those items within the
Graphics.ClipRectangle.
2. Roll your own double-bufferring system. Create a Bitmap object with
the
same characteristics as your control, generate a Graphics object from
that,
and manipulate that where you see fit. In your Paint, just blit the
Bitmap
onto your Control.

> "Sean Hederman" wrote:
>
>> If you need to store the effects of transformations, what you can do
>> is
>> have
>> Matrix member variable that you store your transformations in. Apply
>> the
>> changes to the Matrix in your various procedures, and then apply the
>> Matrix
>> to the Graphics object in the Paint.
>>
>> As for why you shouldn't store the Graphics object, there's no
>> guarantee
>> that the Graphics object has any meaning outside the Paint event. For
>> example, when double bufferring is enabled, then the Graphics object
>> being
>> passed to your Paint relates to a GraphicsBuffer object rather than
>> the
>> actual drawing surface. More importantly, the next time Paint is
>> invoked,
>> you're given a different GraphicsBuffer object. Thus, painting on your
>> stored Graphics object will not result in the painting being on the
>> drawing
>> surface. In addition, since there's no guarantee, future versions of
>> .NET
>> may break your code even if you don't use double buffering.
>>
>> "Sanjay" <sa***********@gmail.com> wrote in message
>> news:ed**************@TK2MSFTNGP15.phx.gbl...
>> > Hi Hederman,
>> > thanks for your reply.
>> > But our's is a different requirement.We do all our drawing in units
>> > of
>> > mils(0.001 inch).And so we are doing a lot of transformations
>> > to/from supported units to/from mils here and there.If I put all the
>> > transformations in the paint,It will really be messy.Anyway it is
>> > not
>> > possible with the current implementations.
>> >
>> > Also could u plz make me clear as to why r u against making in a
>> > data
>> > member.
>> >
>> > I will be thankful to u for the same
>> >
>> > thanking u
>> > sanjay
>> >
>> > ----- Original Message -----
>> > From: "Sean Hederman" <us***@blogentry.com>
>> > Newsgroups:
>> > microsoft.public.dotnet.framework,microsoft.public .dotnet.framework.drawing,
>> > microsoft.public.dotnet.framework.windowsforms,mic rosoft.public.dotnet.gener
>> > al,microsoft.public.dotnet.languages.vb
>> > Sent: Friday, March 11, 2005 6:10 PM
>> > Subject: Re: Should I store Graphics object ?
>> >
>> >
>> >> You should definately not store the graphics object. All the code
>> >> using
>> >> Graphics should ideally be encapsulated within the Paint/OnPaint
>> > procedures.
>> >>
>> >> "Sanjay" <sa***********@gmail.com> wrote in message
>> >> news:eS**************@TK2MSFTNGP15.phx.gbl...
>> >> > hi,
>> >> >
>> >> > We are currently porting our project from VB6 to VB .NET.
>> >> >
>> >> > Earlier we used to make scale transformations on objects like
>> >> > pictureBox
>> > ,
>> >> > forms etc.Now Such transformations are made on the graphics
>> >> > object
>> >> > of
>> > the
>> >> > form/pictureBox.
>> >> >
>> >> > Should It be better if make a graphics object from my pictureBox
>> >> > in
>> >> > load
>> >> > event handler of the form and store it as member variable of the
>> >> > form ,
>> >> > make
>> >> > transformations as and when required(we really require such
>> >> > transformations
>> >> > from mils to pixels.). And finally I can dispose it when the form
>> >> > is
>> >> > disposed.
>> >> >
>> >> > But I have read somewhere that Graphics object should never be
>> >> > stored
>> >> > as
>> >> > member varibale of a class.And so I am really confused as to what
>> >> > should
>> > I
>> >> > do ?
>> >> >
>> >> > Plz help me regarding this..
>> >> >
>> >> > sanjay
>> >>
>> >>
>> >
>> >
>>
>>
>>


Jul 21 '05 #12
I am trying to implement drawing on the bitmap and using bitblt to transfer
it to the control graphics object in the paint event. It seems to draw on
the bitmap ok but doesn't get transferred to the control graphics object in
the paint event. Any help would be appreciated. Here is my code:

public class as mycontrol

Private Declare Auto Function BitBlt Lib "GDI32.DLL" (ByVal hdcDest As
IntPtr, ByVal nxDest As Integer, ByVal nyDest As Integer, ByVal nWidth As
Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc As
Integer, ByVal nYSrc As Integer, ByVal dwRop As Int32) As Boolean

Private v_BackImage As Bitmap = New Bitmap(Me.Width, Me.Height,
Me.CreateGraphics)
Private gph As Graphics = Graphics.FromImage(v_BackImage)

'do some drawing on gph

Private Sub Panel_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
'Get Device contexts for Source and Memory Graphics Objects
Dim hdcSrc As IntPtr = gph.GetHdc
Dim hdcDest As IntPtr = e.Graphics.GetHdc
BitBlt(hdcDest, 0, 0, Me.Width, Me.Height, hdcSrc, Me.Width, Me.Height,
13369376)
'Clean Up
gph.ReleaseHdc(hdcSrc)
e.Graphics.ReleaseHdc(hdcDest)
end sub

end class
"Sean Hederman" wrote:
*May* not work. No guarantees one way or another. As for using
CreateGraphics, it should only be used to get information about the graphics
surface and not to paint on. Have a look at
http://www.bobpowell.net/creategraphics.htm for more details.

Basically if you replace all your CreateGraphics with a bitmap Graphics
creation, and your Paint to bitblt the Bitmap, you should be fine, and be
able to keep your code pretty much as it is.

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:51**********************************@microsof t.com...
Let me ask you another question if you don't mind. I have a User Control
Class that inheiits from the Panel Class. When my control is instaniated,
I
create a graphics object (myGraphics as Graphics = new Me.CreateGrahics)
I
maintain this graphics object and draw on it to repaint parts of the
control
or all of the control which ever is required depending on what the user
does.
If the control is resized, I recreate myGraphics object in the sizechanged
event.

If I understand you correctly, this will not work under future versions of
.net..is that correct.

"Sean Hederman" wrote:
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
> I'm curious as to where M'soft documents that future versions may use
> double
> buffering as the default? Can you cite a link or reference? It's very
> convientent to use a class graphics variable to update only parts of
> the
> display without recalculating and re-displaying the entire control.

They don't. There are two issues here. One is that if you ever decide to
switch on double bufferring, then all your code outside of Paint could be
rendered useless. The other is that there are no guarantees about the
Graphics object outside Paint. Since there are no guarantees, using the
Graphics object elsewhere *could* be invalidated by future versions of
.NET.
Using Graphics outside Paint, knowing that it wraps an HDC is breaking
encapsulation.

As to updating only parts of your display, there are two main approaches
you
could follow here:
1. Use the Invalidate() overrides to only invalidate the parts of your
control you're interested in, and only paint those items within the
Graphics.ClipRectangle.
2. Roll your own double-bufferring system. Create a Bitmap object with
the
same characteristics as your control, generate a Graphics object from
that,
and manipulate that where you see fit. In your Paint, just blit the
Bitmap
onto your Control.

> "Sean Hederman" wrote:
>
>> If you need to store the effects of transformations, what you can do
>> is
>> have
>> Matrix member variable that you store your transformations in. Apply
>> the
>> changes to the Matrix in your various procedures, and then apply the
>> Matrix
>> to the Graphics object in the Paint.
>>
>> As for why you shouldn't store the Graphics object, there's no
>> guarantee
>> that the Graphics object has any meaning outside the Paint event. For
>> example, when double bufferring is enabled, then the Graphics object
>> being
>> passed to your Paint relates to a GraphicsBuffer object rather than
>> the
>> actual drawing surface. More importantly, the next time Paint is
>> invoked,
>> you're given a different GraphicsBuffer object. Thus, painting on your
>> stored Graphics object will not result in the painting being on the
>> drawing
>> surface. In addition, since there's no guarantee, future versions of
>> .NET
>> may break your code even if you don't use double buffering.
>>
>> "Sanjay" <sa***********@gmail.com> wrote in message
>> news:ed**************@TK2MSFTNGP15.phx.gbl...
>> > Hi Hederman,
>> > thanks for your reply.
>> > But our's is a different requirement.We do all our drawing in units
>> > of
>> > mils(0.001 inch).And so we are doing a lot of transformations
>> > to/from supported units to/from mils here and there.If I put all the
>> > transformations in the paint,It will really be messy.Anyway it is
>> > not
>> > possible with the current implementations.
>> >
>> > Also could u plz make me clear as to why r u against making in a
>> > data
>> > member.
>> >
>> > I will be thankful to u for the same
>> >
>> > thanking u
>> > sanjay
>> >
>> > ----- Original Message -----
>> > From: "Sean Hederman" <us***@blogentry.com>
>> > Newsgroups:
>> > microsoft.public.dotnet.framework,microsoft.public .dotnet.framework.drawing,
>> > microsoft.public.dotnet.framework.windowsforms,mic rosoft.public.dotnet.gener
>> > al,microsoft.public.dotnet.languages.vb
>> > Sent: Friday, March 11, 2005 6:10 PM
>> > Subject: Re: Should I store Graphics object ?
>> >
>> >
>> >> You should definately not store the graphics object. All the code
>> >> using
>> >> Graphics should ideally be encapsulated within the Paint/OnPaint
>> > procedures.
>> >>
>> >> "Sanjay" <sa***********@gmail.com> wrote in message
>> >> news:eS**************@TK2MSFTNGP15.phx.gbl...
>> >> > hi,
>> >> >
>> >> > We are currently porting our project from VB6 to VB .NET.
>> >> >
>> >> > Earlier we used to make scale transformations on objects like
>> >> > pictureBox
>> > ,
>> >> > forms etc.Now Such transformations are made on the graphics
>> >> > object
>> >> > of
>> > the
>> >> > form/pictureBox.
>> >> >
>> >> > Should It be better if make a graphics object from my pictureBox
>> >> > in
>> >> > load
>> >> > event handler of the form and store it as member variable of the
>> >> > form ,
>> >> > make
>> >> > transformations as and when required(we really require such
>> >> > transformations
>> >> > from mils to pixels.). And finally I can dispose it when the form
>> >> > is
>> >> > disposed.
>> >> >
>> >> > But I have read somewhere that Graphics object should never be
>> >> > stored
>> >> > as
>> >> > member varibale of a class.And so I am really confused as to what
>> >> > should
>> > I
>> >> > do ?
>> >> >
>> >> > Plz help me regarding this..
>> >> >
>> >> > sanjay
>> >>
>> >>
>> >
>> >
>>
>>
>>


Jul 21 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Sanjay | last post by:
hi, We are currently porting our project from VB6 to VB .NET. Earlier we used to make scale transformations on objects like pictureBox , forms etc.Now Such transformations are made on the...
15
by: matko | last post by:
Hi! In one of the two examples for the PaintEventArgs.Graphics-property (in the VS 2005 documentation), the Graphics-object is "saved" to a local variable, before being used. In the other...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.