Out of memory exception loading a bitmap from a stream

R

Rick

I'm trying to extract an icon from a resource in a dll. The following
sequence:

Dim MyAssemblies() As [Assembly]
..
..
ResourceNames = MyAssemblies(i).GetManifestResourceNames
IconStream = _
MyAssemblies(i).GetManifestResourceStream(ResourceNames(0))
Dim Icon As New Bitmap(IconStream)

Results in an exception:

System.OutOfMemoryException was unhandled
Message="OutOfMemoryException"
StackTrace:
at Microsoft.AGL.Common.MISC.HandleAr()
at System.Drawing.Bitmap._InitFromMemoryStream()
at System.Drawing.Bitmap..ctor()
at ...(Application stack trace)

There is plenty of both storage and program memory left on the device,
and the stream (IconStream) is small (length = 1078). Any ideas? Should
I just punt and put the icon in a file? (not as general)
 
A

Alex Feinman [MVP]

Your stream is called IconStream. Is it an icon file indeed, or just a
misnomer?
Also, avoid using resources by index. You never know which one you are
actually loading. Use the actual name
 
R

Rick Spiewak

It's an icon file as a resource.

Yes, later I'll modify the code to construct the correct name - the code I
posted is what I've been trying to get working as a test - but it needs to
be working first. Any ideas?

Alex Feinman said:
Your stream is called IconStream. Is it an icon file indeed, or just a
misnomer?
Also, avoid using resources by index. You never know which one you are
actually loading. Use the actual name

Rick said:
I'm trying to extract an icon from a resource in a dll. The following
sequence:

Dim MyAssemblies() As [Assembly]
.
.
ResourceNames = MyAssemblies(i).GetManifestResourceNames
IconStream = _
MyAssemblies(i).GetManifestResourceStream(ResourceNames(0))
Dim Icon As New Bitmap(IconStream)

Results in an exception:

System.OutOfMemoryException was unhandled
Message="OutOfMemoryException"
StackTrace:
at Microsoft.AGL.Common.MISC.HandleAr()
at System.Drawing.Bitmap._InitFromMemoryStream()
at System.Drawing.Bitmap..ctor()
at ...(Application stack trace)

There is plenty of both storage and program memory left on the device,
and the stream (IconStream) is small (length = 1078). Any ideas? Should
I just punt and put the icon in a file? (not as general)
 
A

Alex Feinman [MVP]

If it's an icon file, you can't use it with Bitmap class. It needs Icon
class

Rick Spiewak said:
It's an icon file as a resource.

Yes, later I'll modify the code to construct the correct name - the code I
posted is what I've been trying to get working as a test - but it needs to
be working first. Any ideas?

Alex Feinman said:
Your stream is called IconStream. Is it an icon file indeed, or just a
misnomer?
Also, avoid using resources by index. You never know which one you are
actually loading. Use the actual name

Rick said:
I'm trying to extract an icon from a resource in a dll. The following
sequence:

Dim MyAssemblies() As [Assembly]
.
.
ResourceNames = MyAssemblies(i).GetManifestResourceNames
IconStream = _
MyAssemblies(i).GetManifestResourceStream(ResourceNames(0))
Dim Icon As New Bitmap(IconStream)

Results in an exception:

System.OutOfMemoryException was unhandled
Message="OutOfMemoryException"
StackTrace:
at Microsoft.AGL.Common.MISC.HandleAr()
at System.Drawing.Bitmap._InitFromMemoryStream()
at System.Drawing.Bitmap..ctor()
at ...(Application stack trace)

There is plenty of both storage and program memory left on the device,
and the stream (IconStream) is small (length = 1078). Any ideas? Should
I just punt and put the icon in a file? (not as general)
 
R

Rick

Thanks! That solved the problem! However, I would still expect a
different exception than "out of memory" ...
 
A

Alex Feinman [MVP]

It is probably a bug, but consider that the bitmap constructor uses
(eventually, via GDI+) content sniffing to determine the image format. It
might accidentally misinterpret the header and end up trying to load a very
large image (non-existent)
 

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