Disposing of Objects -AGAIN

G

Guest

I don't think I clearly stated my problem in my prior question so let me try
again. I have the following:

Private myobject as Bitmap

myobject = mybitmap (created from a part of the screen)

How do I dispose of the instance of mybitmap object yet retain myobject so I
can use it again like:

myobject = mysecondbitmap

If I do a myobject.Dispose, I can no longer assign the variable myobject to
another bitmap. If I set myobject to nothing, the instantance of mybitmap
and mysecondbitmap are still existing and have to wait for the GC to dispose
of them.
 
K

Ken Tucker [MVP]

Hi,

Take a look at Bitmap.clone.

http://msdn.microsoft.com/library/d...l/frlrfsystemdrawingbitmapclassclonetopic.asp

Ken
-----------------
I don't think I clearly stated my problem in my prior question so let me try
again. I have the following:

Private myobject as Bitmap

myobject = mybitmap (created from a part of the screen)

How do I dispose of the instance of mybitmap object yet retain myobject so I
can use it again like:

myobject = mysecondbitmap

If I do a myobject.Dispose, I can no longer assign the variable myobject to
another bitmap. If I set myobject to nothing, the instantance of mybitmap
and mysecondbitmap are still existing and have to wait for the GC to dispose
of them.
 
H

Herfried K. Wagner [MVP]

Dennis said:
I don't think I clearly stated my problem in my prior question so let me
try
again. I have the following:

Private myobject as Bitmap

myobject = mybitmap (created from a part of the screen)

How do I dispose of the instance of mybitmap object yet retain myobject so
I
can use it again like:

myobject = mysecondbitmap

Mhm... I don't understand why you want to dispose the instance if you still
need it...
 
G

Guest

I want to dispose of the first bitmap I created after I've finished using it
and set the variable myObject to reference a different bitmap so on the next
call to my functions, it will use the different bitmap. I can't pass the
variable as a parameter because I am working with MouseEvents.
 
L

Larry Serflaten

Dennis said:
I want to dispose of the first bitmap I created after I've finished using it
and set the variable myObject to reference a different bitmap so on the next
call to my functions, it will use the different bitmap. I can't pass the
variable as a parameter because I am working with MouseEvents.

Perhaps you could supply a more complete example, something small that
can be pasted into a new form, to see what it is you say isn't working.

This statement of yours is in error....

You don't assign variables to bitmaps, you assign bitmaps to variables. Using
dispose on the bitmap, after you are done with it, is the proper course of action.

LFS
 
C

Cor Ligthert

Larry,

This is done in a long thread before unluckily Dennis started a new thread
and than you get this kind of starting every discussion new again.

The problem is that Dennis has its bitmap globaly and want to keep the
bitmap, what he wants is and I don't know it that works.

\\\
dim mybitmap as bitmap
public sub whatever
if Not mybitmap Is Nothing then
mybitmap.dispose
end if
mybitmap = New bitmap(.....)
end sub
///

Cor
 
L

Larry Serflaten

Cor Ligthert said:
This is done in a long thread before unluckily Dennis started a new thread
and than you get this kind of starting every discussion new again.

Thank you Cor, as busy as I have been, I'll probably have to lurk a while
to watch for new threads, instead of jumping in the middle of finished
discussions....

:)
LFS
 
G

Guest

Thanks Cor for clarifying my problem. That's excatly what I wnat to do.
However, when executing the code, I get an error on the line mybitmap = new
bitmap(...) saying "Invalid Parameter Used" because, I think, when I use the
mybitmap.dispose, it disposes not only the bitmap but destroys the variable
mybitmap.
 
C

Cor Ligthert

Dennis,

This I could do endless
\\\
Private mybitmap As Bitmap
Private Sub Button1_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
If Not mybitmap Is Nothing Then
mybitmap.Dispose()
End If
mybitmap = New Bitmap("c:\test1\mybitmap.bmp")
End Sub
///

So what is in your parameter.
http://msdn.microsoft.com/library/d...ml/frlrfsystemdrawingbitmapclassctortopic.asp

We only saw that it was probably a function returning something


Cor
 
H

Herfried K. Wagner [MVP]

Dennis said:
However, when executing the code, I get an error on the line mybitmap =
new
bitmap(...) saying "Invalid Parameter Used" because, I think, when I use
the
mybitmap.dispose, it disposes not only the bitmap but destroys the
variable
mybitmap.

Please post the complete code calling the 'Bitmap' constructor.
 
G

Guest

Thanks Cor, you've helped solve my problem. What was happening was that I
had code similiar to yours but had a test for mybitmap is nothing before
resetting it to a different bit map. Thanks..this has been driving me nuts
for sometime now.

Private mybitmap As Bitmap
Private Sub Button1_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
If Not mybitmap Is Nothing Then
mybitmap.Dispose()
End If
If mybitmap Is Nothing then mybitmap = New Bitmap("c:\test1 _
\mybitmap.bmp")

......... Use mybitMap for something (got errorhere)
End Sub

What apparently happens is that when you do a mybitmap.dispose, it doesn't
set the variable mybitmap to Nothing so next time I try to set mybitmap to a
new bitmap based on it being nothing, it doesn't get set because mybitmap is
not nothing...not sure what it is but it's sure not nothing.
 

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