title: How do I control the PixelFormat of images produced by magick.NET
body: <p>I'm using Magick.NET-Q8-AnyCPU v8.6.1 in a .NET Framework 4.8 Winforms app to add support for image types not available in the System.Drawing namespace (e.g. WebP), and to convert images to grayscale. System.Drawing.Bitmap images have two properties that control their internal structure; PixelFormat and ImageFormat.</p>
  <p>The MagickImage.Format maps pretty well to the ImageFormat property, but I have not been able to figure out how to map between MagickImage conversion properties and a desired Bitmap.PixelFormat property. When converting an image to png for example, I'm using:</p>
  <pre><code>    using (var magickImage = new MagickImage(&quot;anImage.webp&quot;)) {
      var ms = new MemoryStream();
      magickImage.Write(ms, MagickFormat.Png);
      Bitmap pngImage = new Bitmap(ms);
  }
  </code></pre>
  <p>The pngImage will sometimes have a PixelFormat that matches that of 'anImage', and sometimes not. I have not been able to predict or set the PixelFormat of pngImage. <strong>How do I force the PixelFormat of the image written by magickImage.Write()?</strong></p>
  <p>Similarly, to convert a png image to grayscale, I'm using:</p>
  <pre><code>    using (var magickImage = new MagickImage(&quot;anotherImage.png&quot;)) {
      magickImage.Grayscale(PixelIntensityMethod.Brightness);
      var ms = new MemoryStream();
      magickImage.Write(ms, MagickFormat.Png);
      Bitmap grayImage = new Bitmap(ms);
  }
  </code></pre>
  <p>The grayImage will have a PixelFormat of Format8bppIndexed if the magickImage.Format is Format.Gif, or Format32bppArgb for all other magickImage formats. <strong>How would I change the code to get PixelFormat.Format8bppIndexed and ImageFormat.Png?</strong></p>
  <p><strong>In general, how do I control the PixelFormat of images created via Magick.NET?</strong></p>