title: Prevent BitmapDecoder use from causing SystemOutOfMemoryException
body: <p>I am developing a scientific image-capture application where successive webcam frames are saved as individual images (as opposed to video encoding).</p>
  
  <p>Files are saved to a zip archive, and when the capture is loaded for analysis, each time the frame at a given position is queried, a <code>BitmapFrame</code> is generated by the method <code>LoadImage()</code> in the following class:</p>
  
  <pre><code>public class ImagemCinemetria
  {
      public int Index { get; private set; }
  
      public byte[] ImageBytes { get; private set; }
  
      public BitmapSource Imagem
      {
          get
          {
              if (_imagem == null)
                  _imagem = LoadImage();
              return _imagem;
          }
      }
      BitmapSource _imagem;
  
  
      // CONSTRUTOR
      public ImagemCinemetria(int index, byte[] jpegBytes)
      {
          Index = index;
          ImageBytes = jpegBytes;
      }
  
  
  
      private BitmapSource LoadImage()
      {
          using (var stream = new MemoryStream(ImageBytes))
          {
              var decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
              return decoder.Frames.First();
          }
      }
  }
  </code></pre>
  
  <p>The problem is, as soon as I start to navigate the capture (by moving a slider cursor similar to a video player) and the <code>BitmapFrames</code> start to be loaded, there is a HUGE memory consumption and quite soon I get a <code>SystemOutOfMemoryException</code>.</p>
  
  <p>I know vaguely that there are some cache options for WPF bitmaps, but I am not sure what to do.</p>