title: Which codec for saving bitmaps to memory fast
body: <p>I'm saving a large amount of bitmaps (screenshots) to memory. Nothing special with the code, it's trivial:</p>
  
  <pre><code>var memory = new MemoryStream();
  bitmap.Save(memory, ImageFormat.Png);
  </code></pre>
  
  <p>Since my PC is getting a bit slow, I have run a performance analysis session in Visual Studio and I found out that the <code>Save()</code> call takes 37% of "inclusive samples". Another big parts is used when saving to disk, so these 37% are more likely 80% when not saving to disk. (I don't care about saving to disk at the moment. All data is kept in RAM until a hotkey is pressed and I hardly have influence on hard disk speed.)</p>
  
  <p>From my understanding, the <code>Save()</code> call has to convert the more or less "raw" data of the bitmap into the compressed PNG file format.</p>
  
  <p>I wonder whether someone has a performance overview of the different image formats with respect to the processing time of the <code>Save()</code>method. I'd like to choose the fastest format, even if the file size is larger.</p>
  
  <p>I have tried:</p>
  
  <pre><code>ImageFormat.MemoryBmp
  </code></pre>
  
  <p>but that throws an <code>ArgumentNullException</code>:</p>
  
  <blockquote>
    <p>Value cannot be null. Parameter name: encoder</p>
  </blockquote>
  
  <p>I found <a href="https://stackoverflow.com/questions/25755819/argumentnullexception-bitmap-save-to-memorystream">a related question</a> that describes that some of the image formats are read-only, which reduces the list a bit.</p>