Embedded Resource Question

  • Thread starter Thread starter wackyphill
  • Start date Start date
W

wackyphill

If I Repeat the following code several times (Because I want to reuse
the img throught my program) will I get multiple copies of it in memory
or only one?

Also why do I get exceptions when I close the stream after opening it?
I guess you are not supposed to close it I just dont understand why?

Stream stream = asmType.Assembly.GetManifestResourceStream( qualImgName
);
img = Image.FromStream(stream);

Thanks.
 
If I Repeat the following code several times (Because I want to reuse
the img throught my program) will I get multiple copies of it in memory
or only one?

My **expectation** would be that you'd get multiple, independent
streams. Try calling GetManifestResourceStream and see if the streams
are ReferenceEqual().
Also why do I get exceptions when I close the stream after opening it?
I guess you are not supposed to close it I just dont understand why?

Stream stream = asmType.Assembly.GetManifestResourceStream( qualImgName
);
img = Image.FromStream(stream);

Dox for Image.FromStream() clearly state that "You must keep the
stream open for the lifetime of the Image object" in both 1.1 and 2.0.
Apparently certain drawing ops go back to the streamed data. Try

using (Stream stream =
asmType.Assembly.GetManifestResourceStream(qualImgName))
using (Image StreamImage = Image.FromStream(stream))
img = new Bitmap(StreamImage);

// Copy the FromStream image, then Dispose of both
// the image and the stream.
 
Back
Top