Bitmap/Image data type

M

Michael C

It seems that no matter how I load a bitmap into memory it remains attached
to whatever I used to create it. If I obtained the bitmap from a file the
file stays locked or if it came from a stream I can't close the stream. I
don't see why it is doing this when it loads the full image into memory
anyway. I could understand it doing this if the bitmap discards it's data
when not needed but it doesn't seem to do this. The only way I've found to
fully detach a bitmap from it's source is to create a new bitmap from the
previous one but this slows things down considerably when loading a lot of
images and doubles the memory use while loading large images. Is there any
other way?

Thanks,
Michael
 
W

Willy Denoyette [MVP]

| It seems that no matter how I load a bitmap into memory it remains
attached
| to whatever I used to create it. If I obtained the bitmap from a file the
| file stays locked or if it came from a stream I can't close the stream. I
| don't see why it is doing this when it loads the full image into memory
| anyway. I could understand it doing this if the bitmap discards it's data
| when not needed but it doesn't seem to do this. The only way I've found to
| fully detach a bitmap from it's source is to create a new bitmap from the
| previous one but this slows things down considerably when loading a lot of
| images and doubles the memory use while loading large images. Is there any
| other way?
|
| Thanks,
| Michael
|
|

Make sure you dispose the Image after having done with it. One way to
automate this is by applying the using idiom like this:

using (Image imageFile = Image.FromFile(.....)) {
// draw to screen or whatever...
...

} // close underlying stream

Willy.
 
M

Michael C

Willy Denoyette said:
Make sure you dispose the Image after having done with it. One way to
automate this is by applying the using idiom like this:

using (Image imageFile = Image.FromFile(.....)) {
// draw to screen or whatever...
...

} // close underlying stream

That's ok but the problem in my case is the image can get passed around all
over the place. If the image is attached to a stream then I need to pass the
stream around with the image everywhere it goes. Usually I'll have a method
which gets and image from a various places (eg scanner, camera, database or
file) and I don't want to have to return the stream with the image. All I
want is an image that is totally detatched from the stream but this doesn't
seem possible without completely duplicating the image.

Michael
 
G

Guest

Yes, when you open a bitmap from a Stream, the Stream must remain open. When
you open it from a file, the file remains locked.

I'm not sure exactly what you are doing with the image when you pass it
around, or where exactly you are passing it around. What about reading in
the image as an array of bytes and passing that around?
 
M

Michael C

rmacias said:
Yes, when you open a bitmap from a Stream, the Stream must remain open.
When
you open it from a file, the file remains locked.

Do you know the reason behind this? It doesn't seem necessary to me if
everything is copied into memory anyway.
I'm not sure exactly what you are doing with the image when you pass it
around, or where exactly you are passing it around. What about reading in
the image as an array of bytes and passing that around?

Typically an image will be grabbed from a source (could be database, file,
directshow, twain, wia, clipboard or even custom hardware). The image will
be passed to a control that displays a small version of it. If the user
double clicks it it gets passed to another form to display full screen. If
the user decides to keep the image it is then passed to be saved into the
database and passed to another form which displays all images. From there it
can be passed around to all sorts of places to display, edit, print, delete,
email it etc. It's also stored in an image cache to speed up loading if
users are scrolling between images.

To keep the stream open or the file locked during all of this is impracticle
(eg if a user imports a file into my app the file should then be able to be
deleted (or if the user so specifies then my app will delete it)). Currently
I just create a copy of the bitmap which does work but is quite inefficient.
I've ordered a book about dot net imagine so hopefully that will shed some
light on what is actually happening. The help is pretty poor really and
usually only explains what obvious anyway. :)

Michael
 

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