473,513 Members | 2,339 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to check if Graphics object is Disposed

Public Function PicCreateGraphics() As Graphics

'Client should dispose this

PicCreateGraphics = Graphics.FromImage(mDocumentImage)

mPicCreateGraphicsSaved = PicCreateGraphics 'Saved so I can later check
to see if it is still valid

End Function

Probably should be a Property but the above is the way the client gets a
Graphics object.

I save a copy because there is something the client should not do if he has
an active PicCreateGraphics object( that is, if he created one and hasn't
disposed it) and I want to check.

I want to raise an error if mPicCreateGraphicsSaved is not nothing and not
Disposed

How to check for Disposed.

Controls have an IsDisposed property but Graphics objects do not.

Thanks in advance




Nov 20 '05 #1
6 8558
JD
Just from what you wrote, I don't think what you are trying to do is a good
idea. Isn't it really up to the client to decide the lifetime of the graphic
image and whether its supposed to be disposed?

Example:

'Your Code
Class MyClass
Public Property SomeGraphic as Graphic
End Class

'Client Code
Dim MC as MyClass
Dim MG as Graphic = MC.SomeGraphic
MC = Nothing
'Client goes on to do more stuff with the MG Object

How can your object correctly say that the above example is an error or not?

Matter of fact take the graphic object right out of the picture, say MyClass
implemented IDisposable itself, and MyClass wants to alert the client of an
error when the dispose method is not called. Guess what, you cannot do it
because MyClass cannot determine when there are 0 references to it unless it
overrides Finalize method, but even by that point there is no client to
alert and the code is now operating on the Finalize thread.
I want to raise an error if mPicCreateGraphicsSaved is not nothing and not Disposed
How and when can you enforce this rule? Unless you force the user to call a
method (Dispose or Close) in your class, but then how is your class going to
know the client did not call your class's Dispose or Close method before
setting its own reference to your object to Nothing?

Maybe I'm missing something?
" SamSpade" <st**************@REMOVEaol.com> wrote in message
news:ec**************@TK2MSFTNGP11.phx.gbl... Public Function PicCreateGraphics() As Graphics

'Client should dispose this

PicCreateGraphics = Graphics.FromImage(mDocumentImage)

mPicCreateGraphicsSaved = PicCreateGraphics 'Saved so I can later check to see if it is still valid

End Function

Probably should be a Property but the above is the way the client gets a
Graphics object.

I save a copy because there is something the client should not do if he has an active PicCreateGraphics object( that is, if he created one and hasn't
disposed it) and I want to check.

I want to raise an error if mPicCreateGraphicsSaved is not nothing and not Disposed

How to check for Disposed.

Controls have an IsDisposed property but Graphics objects do not.

Thanks in advance





Nov 20 '05 #2
Just from what you wrote, I don't think what you are trying to do is a good idea. Isn't it really up to the client to decide the lifetime of the graphic image and whether its supposed to be disposed?
Yes but see below
How and when can you enforce this rule? Unless you force the user to call a method (Dispose or Close) in your class, but then how is your class going to know the client did not call your class's Dispose or Close method before
setting its own reference to your object to Nothing?

Maybe I'm missing something?


I doubt it!

But he should never program anything that would cause AdjustDocumentImage to
execute because it deletes the bitmap the graphic object points to and
creates a new one.

If he does and there is an active reference to the Graphic object it will
be pointing to the deleted Bitmap

How about the following just to catch a programming error?

Public ReadOnly Property PicCreateGraphics() As Graphics
'Client should dispose this
Get
PicCreateGraphics = Graphics.FromImage(mDocumentImage)
mPicCreateGraphicsSaved = PicCreateGraphics 'Saved so I can later check to
see if it is still valid
End Get
End Property

Private Sub AdjustDocumentImage()
Try
Dim zz = mPicCreateGraphicsSaved.DpiX 'GO TO CATCH IF GRAPHIC REFERENCE IS
NG
MessageBox.Show("Can not adjust image if Graphics object is active",
"Error", Message-snip
Catch
'THIS WHERE TH GOOD CODE GOES
DELETE mDocumentImage BITMAP
Why am I deleteing the Bitmap?? Because I want to change its size (at the
client request - he should make sure he does not have an active graphics
object at this time) while preserving the contents. The only way I know how
to do that is to make a new one and get rid of the old one.

Boy if I just change the size of the current one that would be great - but
I've no idea how to do that.

Thanks for your reply
Nov 20 '05 #3
JD
By implementing PicCreateGraphics your object has become a factory for the
graphic object. Since it is a factory, you should force the user to pass the
graphic object back into the method that calls AdjustDocumentImage, so the
method signature becomes:

Private Sub AdjustDocumentImage(ByRef InGraphic as Graphic)

Now you can dispose the graphic, allocate the new one, and repoint the
user's reference to the new one.
" SamSpade" <st**************@REMOVEaol.com> wrote in message
news:uh**************@TK2MSFTNGP12.phx.gbl...
Just from what you wrote, I don't think what you are trying to do is a good
idea. Isn't it really up to the client to decide the lifetime of the

graphic
image and whether its supposed to be disposed?


Yes but see below
How and when can you enforce this rule? Unless you force the user to

call a
method (Dispose or Close) in your class, but then how is your class
going to
know the client did not call your class's Dispose or Close method before
setting its own reference to your object to Nothing?

Maybe I'm missing something?
I doubt it!

But he should never program anything that would cause AdjustDocumentImage

to execute because it deletes the bitmap the graphic object points to and
creates a new one.

If he does and there is an active reference to the Graphic object it will
be pointing to the deleted Bitmap

How about the following just to catch a programming error?

Public ReadOnly Property PicCreateGraphics() As Graphics
'Client should dispose this
Get
PicCreateGraphics = Graphics.FromImage(mDocumentImage)
mPicCreateGraphicsSaved = PicCreateGraphics 'Saved so I can later check to
see if it is still valid
End Get
End Property

Private Sub AdjustDocumentImage()
Try
Dim zz = mPicCreateGraphicsSaved.DpiX 'GO TO CATCH IF GRAPHIC REFERENCE IS NG
MessageBox.Show("Can not adjust image if Graphics object is active",
"Error", Message-snip
Catch
'THIS WHERE TH GOOD CODE GOES
DELETE mDocumentImage BITMAP
Why am I deleteing the Bitmap?? Because I want to change its size (at the
client request - he should make sure he does not have an active graphics
object at this time) while preserving the contents. The only way I know how to do that is to make a new one and get rid of the old one.

Boy if I just change the size of the current one that would be great - but
I've no idea how to do that.

Thanks for your reply

Nov 20 '05 #4
Great idea, I don't know how to implement it now because the routines that
call AdjustDocumentImage are things like the controls Resize event.
However, I'm going to keep this in mind.

The best I can think of so far is to raise an event saving that the Graphic
object has been disposed and the client should reCreate his. I didn't do
this yet - still thinking.

The best solution is to change its size of the bitmap
while preserving the contents.
Is there any chance I could do that??

"JD" <no@email.org> wrote in message
news:u7**************@tk2msftngp13.phx.gbl...
By implementing PicCreateGraphics your object has become a factory for the
graphic object. Since it is a factory, you should force the user to pass the graphic object back into the method that calls AdjustDocumentImage, so the
method signature becomes:

Private Sub AdjustDocumentImage(ByRef InGraphic as Graphic)

Now you can dispose the graphic, allocate the new one, and repoint the
user's reference to the new one.
" SamSpade" <st**************@REMOVEaol.com> wrote in message
news:uh**************@TK2MSFTNGP12.phx.gbl...
Just from what you wrote, I don't think what you are trying to do is a good
idea. Isn't it really up to the client to decide the lifetime of the

graphic
image and whether its supposed to be disposed?


Yes but see below
How and when can you enforce this rule? Unless you force the user to call
a
method (Dispose or Close) in your class, but then how is your class

going
to
know the client did not call your class's Dispose or Close method before setting its own reference to your object to Nothing?

Maybe I'm missing something?


I doubt it!

But he should never program anything that would cause

AdjustDocumentImage to
execute because it deletes the bitmap the graphic object points to and
creates a new one.

If he does and there is an active reference to the Graphic object it

will be pointing to the deleted Bitmap

How about the following just to catch a programming error?

Public ReadOnly Property PicCreateGraphics() As Graphics
'Client should dispose this
Get
PicCreateGraphics = Graphics.FromImage(mDocumentImage)
mPicCreateGraphicsSaved = PicCreateGraphics 'Saved so I can later check to see if it is still valid
End Get
End Property

Private Sub AdjustDocumentImage()
Try
Dim zz = mPicCreateGraphicsSaved.DpiX 'GO TO CATCH IF GRAPHIC REFERENCE

IS
NG
MessageBox.Show("Can not adjust image if Graphics object is active",
"Error", Message-snip
Catch
'THIS WHERE TH GOOD CODE GOES
DELETE mDocumentImage BITMAP
Why am I deleteing the Bitmap?? Because I want to change its size (at the client request - he should make sure he does not have an active graphics
object at this time) while preserving the contents. The only way I know

how
to do that is to make a new one and get rid of the old one.

Boy if I just change the size of the current one that would be great - but I've no idea how to do that.

Thanks for your reply


Nov 20 '05 #5
Hi Sam,

Hi Sam,

Seldom that I can help you because you become the expert in the Rich Text
Box in this newsgroup.

However this is a link for your question about the resizing to effect the
qualitiy of an image.
I thought that all that documentation is in C# however it is not that
difficult to translate to VB.net

http://msdn.microsoft.com/library/de...alityTopic.asp

When you take this link too, than I think that you are half on the rout to
realize your goals.
http://msdn.microsoft.com/library/de...ingimaging.asp

I hope it helps something?

Cor
Nov 20 '05 #6
Thanks for the lead - I'll check it out right now.
I hope it helps something?

Cor

Nov 20 '05 #7

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

Similar topics

12
2340
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...
1
3213
by: Hadar | last post by:
Hi, I'm getting "object is currently in use elsewhere" when I use System.Drawing.Graphics.MesureString. This is what I do: My controls use a utility class the helps it to mesure strings. To...
2
2422
by: Robin Tucker | last post by:
Hi there, Do I need to call "Dispose" on System.Drawing.Brush and System.Drawing.Pen if I've created new ones? What if I'm using stock brushes and pens? Also, do I need to explicitly call...
4
3096
by: billsahiker | last post by:
There is a code example in the MS Training Kit for the Framework 2.0 test that does not seem to work. The example has just three lines of code, but does not indicate where to put the code, so I put...
9
5046
by: koschwitz | last post by:
Hi, I hope you guys can help me make this simple application work. I'm trying to create a form displaying 3 circles, which independently change colors 3 times after a random time period has...
0
7267
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7391
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7553
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7120
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3235
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1609
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
466
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.