Search code examples
imagemagickmagick.net

Why does my conversion with Magick.NET result in a black PCX file?


I have following .png-file

which I want to convert with Magick.NET to a .pcx-file. I use following code for the conversion:

using System.Drawing;
using using ImageMagick;

using (var bitmap = (Bitmap) Bitmap.FromFile("ptOHf.png"))
using (var magickImage = new MagickImage(bitmap))
{
  magickImage.Format = MagickFormat.Pcx;
  magickImage.ColorType = ColorType.Palette;
  magickImage.ColorSpace = ColorSpace.Gray;

  magickImage.Write("C:\\somefile.pcx");
}

This results in the following output:

Package used: Magick.NET-Q8-AnyCPU 7.0.1.500 (Net40)


Solution

  • I don't really speak .NET, Dirk (@dlemstra) is the man for that, but the problem is that all the (white) information is actually in the alpha layer and the basic image itself is just solid black and ImageMagick has done that correctly as PCX cannot render transparency.

    You can see what I mean if you extract the alpha layer like this:

    convert https://i.sstatic.net/ptOHf.png -alpha extract a.jpg
    

    enter image description here

    At the command line, you would make ImageMagick account for the alpha layer using -flatten

    convert https://i.sstatic.net/ptOHf.png -flatten result.pcx
    

    I have no idea, but I guess in .NET, you would do something like:

    using (var magickImage = new MagickImage(bitmap))
    {
      magickImage.Flatten();
      magickImage.Format = MagickFormat.Pcx;
      magickImage.ColorType = ColorType.Palette;
      magickImage.ColorSpace = ColorSpace.Gray;
    }