How do you make sure WPF releases large BitmapSource from Memory?

You certainly have put in a lot of work on this. I think the main problem is that BitmapCacheOption.None doesn’t prevent the underlying BitmapDecoder(s) from being cached. There are several tricky solutions to this such as doing a GC.Collect(), loading 300 small images from 300 different Uris, and calling GC.Collect() again, but the simple one … Read more

Is there a good way to convert between BitmapSource and Bitmap?

It is possible to do without using unsafe code by using Bitmap.LockBits and copy the pixels from the BitmapSource straight to the Bitmap Bitmap GetBitmap(BitmapSource source) { Bitmap bmp = new Bitmap( source.PixelWidth, source.PixelHeight, PixelFormat.Format32bppPArgb); BitmapData data = bmp.LockBits( new Rectangle(Point.Empty, bmp.Size), ImageLockMode.WriteOnly, PixelFormat.Format32bppPArgb); source.CopyPixels( Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); bmp.UnlockBits(data); return bmp; }

fast converting Bitmap to BitmapSource wpf

Here is a method that (to my experience) is at least four times faster than CreateBitmapSourceFromHBitmap. It requires that you set the correct PixelFormat of the resulting BitmapSource. public static BitmapSource Convert(System.Drawing.Bitmap bitmap) { var bitmapData = bitmap.LockBits( new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat); var bitmapSource = BitmapSource.Create( bitmapData.Width, bitmapData.Height, bitmap.HorizontalResolution, bitmap.VerticalResolution, PixelFormats.Bgr24, null, … Read more