How to check if Graphics object is Disposed

S

SamSpade

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
 
J

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?
 
S

SamSpade

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
 
J

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.
 
S

SamSpade

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??
 
C

Cor Ligthert

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/d...temDrawingImagingEncoderClassQualityTopic.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/d...n-us/cpref/html/frlrfsystemdrawingimaging.asp

I hope it helps something?

Cor
 

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