473,587 Members | 2,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to check if Graphics object is Disposed

Public Function PicCreateGraphi cs() As Graphics

'Client should dispose this

PicCreateGraphi cs = Graphics.FromIm age(mDocumentIm age)

mPicCreateGraph icsSaved = PicCreateGraphi cs '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 PicCreateGraphi cs object( that is, if he created one and hasn't
disposed it) and I want to check.

I want to raise an error if mPicCreateGraph icsSaved 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 8572
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 mPicCreateGraph icsSaved 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.co m> wrote in message
news:ec******** ******@TK2MSFTN GP11.phx.gbl... Public Function PicCreateGraphi cs() As Graphics

'Client should dispose this

PicCreateGraphi cs = Graphics.FromIm age(mDocumentIm age)

mPicCreateGraph icsSaved = PicCreateGraphi cs '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 PicCreateGraphi cs object( that is, if he created one and hasn't
disposed it) and I want to check.

I want to raise an error if mPicCreateGraph icsSaved 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 AdjustDocumentI mage 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 PicCreateGraphi cs() As Graphics
'Client should dispose this
Get
PicCreateGraphi cs = Graphics.FromIm age(mDocumentIm age)
mPicCreateGraph icsSaved = PicCreateGraphi cs 'Saved so I can later check to
see if it is still valid
End Get
End Property

Private Sub AdjustDocumentI mage()
Try
Dim zz = mPicCreateGraph icsSaved.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 PicCreateGraphi cs 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 AdjustDocumentI mage, so the
method signature becomes:

Private Sub AdjustDocumentI mage(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.co m> wrote in message
news:uh******** ******@TK2MSFTN GP12.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 AdjustDocumentI mage

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 PicCreateGraphi cs() As Graphics
'Client should dispose this
Get
PicCreateGraphi cs = Graphics.FromIm age(mDocumentIm age)
mPicCreateGraph icsSaved = PicCreateGraphi cs 'Saved so I can later check to
see if it is still valid
End Get
End Property

Private Sub AdjustDocumentI mage()
Try
Dim zz = mPicCreateGraph icsSaved.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 AdjustDocumentI mage 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******** ******@tk2msftn gp13.phx.gbl...
By implementing PicCreateGraphi cs 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 AdjustDocumentI mage, so the
method signature becomes:

Private Sub AdjustDocumentI mage(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.co m> wrote in message
news:uh******** ******@TK2MSFTN GP12.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

AdjustDocumentI mage 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 PicCreateGraphi cs() As Graphics
'Client should dispose this
Get
PicCreateGraphi cs = Graphics.FromIm age(mDocumentIm age)
mPicCreateGraph icsSaved = PicCreateGraphi cs 'Saved so I can later check to see if it is still valid
End Get
End Property

Private Sub AdjustDocumentI mage()
Try
Dim zz = mPicCreateGraph icsSaved.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
2362
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 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
1
3221
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 get the best performance for the utility class, its members, as well as the System.Drawing.Graphics object, are static:
2
2433
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 "Dispose" on Graphics too? In all cases in my code, they are local variables anyway. Won't VB.NET dispose them automatically when the function exits? Thanks for any tips,
4
3097
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 it in the form.load event and it generates a blank screen, but is supposed to display "Hello World" on the form. Then I put it in the paint event as shown below and still I get a blank form when it runs. Any ideas what is wrong? I previously...
9
5052
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 passed. I'm struggling with making the delegate/invoke thing work, as I know GUI objects aren't thread-safe. I don't quite understand the concept I'm supposed to use to modify the GUI thread-safe. Below is my form and my Circle class. Currently,...
0
7924
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7854
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8219
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8349
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2364
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 we have to send another system

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.