title: C# Why Bitmap.Save ignores PixelFormat of Bitmap?
body: <p>I need to process and save images in their original bpp (1 - for BnW, 8 - for gray, 24 - color).
  After processing I always have 24bpp (due to processing with Aforge.Net). I pass original bpp to saving function and I'm using the next code for saving:</p>
  
  <pre><code>private static Bitmap changePixelFormat(Bitmap input, PixelFormat format)
      {
          Bitmap retval = new Bitmap(input.Width, input.Height, format);
          retval.SetResolution(input.HorizontalResolution, input.VerticalResolution);
          Graphics g = Graphics.FromImage(retval);
          g.DrawImage(input, 0, 0);
          g.Dispose();
          return retval;
      }
  </code></pre>
  
  <p>When I pass:</p>
  
  <pre><code>PixelFormat.Format8bppIndexed
  </code></pre>
  
  <p>I'm getting an exception: "Graphics object can't create image in indexed pixel format" in the line:</p>
  
  <pre><code>Graphics g = Graphics.FromImage(retval);
  </code></pre>
  
  <p>I've tried the next code:</p>
  
  <pre><code>Bitmap s = new Bitmap("gray.jpg");
          Bitmap NewPicture = s.Clone(new Rectangle(0, 0, s.Width, s.Height),                   PixelFormat.Format8bppIndexed);
  </code></pre>
  
  <p>After this "NewImage" has PixelFormat 8bppIndexed, but when I'm saving NewImage:</p>
  
  <pre><code>NewPicture.Save("hello.jpg", ImageFormat.Jpeg);
  </code></pre>
  
  <p>hello.jpg has 24bpp.</p>
  
  <p>I need to keep bpp and image format of original image.</p>
  
  <p>Why Bitmap.Save ignores PixelFormat of Bitmap?</p>
  
  <p>=======================================================</p>
  
  <p>Thanks guys, I've found a solution:
  <a href="http://freeimage.sourceforge.net/license.html" rel="nofollow">http://freeimage.sourceforge.net/license.html</a></p>
  
  <p>With help of this free library it is possible to save images (especially JPEG ) to Grayscale 8-bit.</p>