Image.FromStream does not contain a link to the underlying stream

I have a code that does

MemoryStream ms = new MemoryStream();
...
return Image.FromStream(ms);

This is not very eclectic, since the Image object does not contain a link to the stream, so it can be deleted if the GC hits, resulting in GDI + errors.

How can I get around this (without saving the stream to disk or changing my sigs method)?

+3
source share
2 answers

This seems unlikely to me - it will cause a problem for almost any use Image.FromStream.

It seems to me more likely that something is saving yours MemoryStream, which should not be.

, , ? - , MemoryStream , , (, , ).

+1

, . FromStream :

.

, , , . ( Reflector), FromImage , , .

, - MemoryStream ( Stream) , GCed. " " ( ), , .

+1

Source: https://habr.com/ru/post/1734513/


All Articles