I'm developing a WPF application that supports custom brush and image modification. If a user puts in mouse input, Bitmap is modified, converted to BitmapImage, and then displayed. It doesn't have problem at most time, but the application stops when I leave the application with no input for a amount of time(about 3~5 minutes).
This is the code block that I use for Bitmap -> BitmapImage modification
BitmapImage BitmapToImageSource(Bitmap bitmap) {
using (MemoryStream memory = new MemoryStream()) {
bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
memory.Position = 0;
BitmapImage bitmapimage = new BitmapImage();
bitmapimage.BeginInit();
bitmapimage.StreamSource = memory;
bitmapimage.DecodePixelWidth = bitmap.Width;
bitmapimage.DecodePixelHeight = bitmap.Height;
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
bitmapimage.EndInit();
return bitmapimage;
}
}
Application Stops at
bitmap.save(memory, System.Drawing.Imaging.ImageFormat.Png);
Code for displaying BitmapImage
myWindow.myImageName.Source = BitmapToImageSource(myBitmap);
//I work at different class from .xaml.cs
However, it doesn't throw an error. The Application just stops and doesn't work.
Is there any possible problem that bitmap data is not being saved in memory stream? Or is there any ways to display Bitmap to WPF Image not saving bitmap to memorystream?