2

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)

  • Try adding `flatten` after opening since the whites in your image are actually in the alpha layer not the colour layer. Your image is actually entirely black, all the info is in the alpha layer. – Mark Setchell Jun 01 '16 at 11:37
  • @MarkSetchell oh, excellent catch - I'll try that - could you possibly provide some code as an answer (I would love to give you some points for that discovery) :) –  Jun 01 '16 at 11:39

1 Answers1

3

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.stack.imgur.com/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.stack.imgur.com/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;
}
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • 2
    fyi: I did some research, and the easiest version to get this working is `magickImage.ColorAlpha(MagickColors.White);` - but your input totally saved my bacon today - thank you! –  Jun 01 '16 at 11:49
  • 1
    @AndreasNiedermair You saved me so many hours of tireless research with a comment. – Adam Jun 19 '18 at 17:32