load bitmap from file

J

Jeffrey Hall

I have code that loads a bitmap from a file like this:

Dim bmp as Bitmap = New Bitmap (imagePath)

It works for smaller files, but throws an out of memory exception when I use
larger files. I believe this is more related to the pixel width and height
than the actual file size because it seems to crash on files that are larger
than 2000 pixels in both dimensions, regardless of the size. I have a
number of GUI elements on the same form, but I tried loading images in an
empty project with no GUI elements with similar results.

My Ipaq has an image viewer that loads 5 of these images at a time (one of
these images alone crashes me) onto the screen and lets you rotate, zoom,
etc, so someone has figured out a way around this. My problem is that I
don't know how to manipulate the image if I can't get the file into a bitmap
to start with. I am new to the .Net Compact Framework so I apologize if
this was answered already, but I haven't had any luck searching forums.

Thanks,

Jeff
 
D

Daniel Moth

There is a known issue with CF 1.0 described here:
http://blogs.msdn.com/scottholden/archive/2004/12/28/339733.aspx

The workaround (copy/paste from the above link) is this (please read the
entire entry):
<quote>
try {
// Bitmap constructor throw an exception if OOM
bitmap = Bitmap(stream);
} catch {
// Generally, you should try to catch an explicit
// exception, but I know that this can throw 2 different
// types of exceptions on an OOM.
GC.Collect();

// An exception will be thrown if still OOM
bitmap = Bitmap(stream);
}
</quote>

Let us know if it works

Cheers
Daniel
 
I

Ilya Tumanov [MS]

You are correct, file size is not important in this case.
What matters is how big the uncompressed image is (that's how bitmap is
stored in memory).
2000x2000x32bit would result in 16 Megs of uncompressed size.
Even one image of that size can cause your application to run out of
virtual memory on some less powerful devices.
Latest devices might be able to handle one or even two.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
 
J

Jeffrey Hall

Thanks to both of you for the feedback. I tried the suggestion from the
garbage collector article like this:

Try
Dim bmp as Bitmap = New Bitmap (imagePath)

Catch ex as OutOfMemoryException
GC.Collect
Dim bmp as Bitmap = New Bitmap (imagePath)

End Try


It threw another "out of memory exception" on the second to last line. The
image I used was 2300 x 2500 pixels, 151 KB. If I missed something, please
let me know. Otherwise, I think I'll be telling users to downsize their
images. Thanks again for the help.

Jeff
 
I

Ilya Tumanov [MS]

The file was 151K; the bitmap was 2300x2500x32 = 23MB.

You'll be able to load only one such bitmap on most devices. Before you do,
dispose of the one you already have:

Dim bmp As Bitmap = Nothing ' Member variable to keep bitmap
we're working with
...

If Not bmp Is Nothing Then ' See if we have bitmap already
bmp.Dispose() ' Dispose of old bitmap before loading a new one
End If

Dim bmp as Bitmap = New Bitmap (imagePath)
' Go ahead and load new bitmap.

Note reference to the bitmap is kept in a member variable.
Should local variable be used, the reference will be lost and memory won't
be freed immediately.
It will be only freed after GC runs finalizes which is too late in your
case.

If you need to load several bitmaps, consider storing references to loaded
bitmaps in, say, HashTable.
Manually dispose of them all in case you run out of memory on attempt to
load another bitmap.

By the way, it's not just about bitmaps. Any object which allocates native
resources should be disposed of.
If object implements IDisposable and has Dispose method, it should be
disposed of after use.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
 

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