title: C# Converting 32bpp image to 8bpp
body: <p>I'm trying to convert a 32bpp screenshot image to an 8bpp (or 4bpp, or 1bpp) format using C#. I've already looked at several stackoverflow answers on similar subjects and most suggest variations using the following code:</p>
  
  <pre><code>public static Bitmap Convert(Bitmap oldbmp) 
  {
      Bitmap newbmp = new Bitmap(oldbmp.Width, oldbmp.Height, PixelFormat.Format8bppIndexed);
  
      Graphics gr = Graphics.FromImage(newbmp);
  
      gr.PageUnit = GraphicsUnit.Pixel;
      gr.DrawImageUnscaled(oldbmp, 0, 0);
  
      return newbmp;
  }
  </code></pre>
  
  <p>However, when this executes, I get a the exception: <code>A graphics object cannot be created from an image that has an indexed pixel format</code>. I understand that 8, 4 and 1bpp images have colour table mappings rather than the actual colour pixels themselves (as in 32 or 16bpp images) so I assume I'm missing some conversion step somewhere, but I'm fairly new to C# (coming from a C++ background) and would prefer to be able do this using native C# calls rather than resorting to PInvoking <code>BitBlt</code> and <code>GetDIBits</code> etc. Anybody able to help me solve this? Thanks.</p>
  
  <p><strong>EDIT</strong>: I should point out that I need this to be backwardly compatible to .NET framework 2.0</p>