I have several images that I upload to a ListBox in my WPF application. I originally used GDI to resize images (originals take up too much memory). This was fine, except that they took about 400 ms per image. Not so good. Therefore, in search of another solution, I found a method that uses TransformedBitmap (which inherits from BitmapSource). βIt's great,β I thought, βI can use it. Besides the fact that I'm now losing memory leaks ...
I load images asynchronously using BackgroundWorker, for example:
BitmapSource bs = ImageUtils.ResizeBitmapSource(ImageUtils.GetImageSource(photo.FullName)); //BitmapSource bs = ImageUtils.GetImageSource(photo.FullName); bs.Freeze(); this.dispatcher.Invoke(new Action(() => { photo.Source = bs; }));
GetImageSource simply gets the bitmap from the path and then converts it to BitmapSource.
Here is the code snippet for ResizeBitmapSource:
const int thumbnailSize = 200; int width; int height; if (bs.Width > bs.Height) { width = thumbnailSize; height = (int)(bs.Height * thumbnailSize / bs.Width); } else { height = thumbnailSize; width = (int)(bs.Width * thumbnailSize / bs.Height); } BitmapSource tbBitmap = new TransformedBitmap(bs, new ScaleTransform(width / bs.Width, height / bs.Height, 0, 0)); return tbBitmap;
This code is essentially code from: http://rongchaua.net/blog/c-wpf-fast-image-resize/
Any ideas what could cause the leak?
edit: Here is the code for GetImageSource requested
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read)) { using (var bmp = Image.FromStream(stream, false, false)) {