I have the stream of the Image and I convert it into image by the below code
Image imagefromstream = Image.FromStream(stream);
When I draw this image in graphics, it draws the image.
But, if I dispose the stream after drawing in graphics, the image is not drawn.
Can anyone help me to dispose the image stream
According to MSDN here:
You must keep the stream open for the lifetime of the Image.
You'll have to Dispose the stream when you close your application (or whenever you no longer need the Image)
In this case, you have the direct reference of memory stream and if you dispose it, image also will get disposed.
So you get the source from the stream and have it in a BitmapImage
, set that BitmapImage
as the source for the Image..
var imageSource = new BitmapImage();
imageSource.BeginInit();
imageSource.StreamSource = memoryStream;
imageSource.CacheOption= CacheOption.OnLoad;
imageSource.EndInit();
// Assign the Source property of your image
image.Source = imageSource;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With