By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,837 Members | 1,613 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,837 IT Pros & Developers. It's quick & easy.

When to dispose

P: n/a
I've been disposing whenever I create a bitmap or graphics object.

Recently I developed a bug which turned out to be as follows:

bmp=new Bitmap(...
picBox.Image = bmp
..
..
bmp.dispose
exit sub

Now I can see that Image is pointing to a disposed bitmap.
But if I don't will picturebox dispose the bitmap assigned to Image?

Do I have to assign to Image or can I just draw on it.

What happens if I never dispose of my Graphic or Bitmap objects?

Thanks in advance for any answers

Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"David Williams" <Da***********@discussions.microsoft.com> wrote in message
news:56**********************************@microsof t.com...
1) You should dispose of the Image (in this case) not the Bitmap. Now that's confusing. Doesn't the PictureBox need to have the Image as long
as it is displaying?
Or do I dispose the image when the Picturebox is closing?

2) Not sure what you mean by Assign to Image or Just Draw to it.
Is it better to
Dim zz As Graphics = PictureControl1.PicCreateGraphics
zz.DrawImage(Img, 0, 0)

or is it better to

picBox.Image = Img

You have to have the object (an Image) in memory before you can update it
(by drawing to on it.)

Meaning I can't do the first above before I do the second - right?
3) If you do not dispose of your Graphics or Bitmaps, most likely you will be fine, but if you are creating huge numbers of them you may run out of
memory or other resources. The GC "should" take care of them, but I have
found that it is much bertter to dispose of them once you are done with
them. As is always the case, your application design will dictate.

There's no association between these disposes and deleting handles in
windows API (which as I remember MUST be done)?
Thanks a lot for all the info!


HTH
--
David Williams, VB.NET MVP
"Just Me" wrote:
I've been disposing whenever I create a bitmap or graphics object.

Recently I developed a bug which turned out to be as follows:

bmp=new Bitmap(...
picBox.Image = bmp
..
..
bmp.dispose
exit sub

Now I can see that Image is pointing to a disposed bitmap.
But if I don't will picturebox dispose the bitmap assigned to Image?

Do I have to assign to Image or can I just draw on it.

What happens if I never dispose of my Graphic or Bitmap objects?

Thanks in advance for any answers

Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.