6

Let's say I want to transform the transparent background of an image into white (flatten a png): A simple google search points me to: convert -flatten image.png noTransparency.png. This works fine if I invoke imageMagick from the command line. But let's say I want to use the .NET wrapper magick.net to do the same thing. Neither Google nor magick.net's documentation is very helpful for this case.

Same thing happens with convert original.png +transparent "#E6B15A" singleColor.png (A way to create a new image containing only a specific color from the original) I can't find ANY way to do this using the magick.NET wrapper

Why is so hard to map imageMagick commands into .net code using magick.net??

Is there any way I could use magick.net to execute imageMagick scripts instead? I could easily create a cmd Process from .net and call imageMagick commands from there, but if there is a wrapper it must be for some reason... Is there any good and extensive magick.net documentation that I'm missing?

Alejandro Lozdziejski
  • 1,063
  • 4
  • 12
  • 30

1 Answers1

9

I am sorry but there is no extensive version of the documentation available at this moment. I have only created a couple of examples and documented them on the website. I don't have the time/team to create more and better documentation. But I can help you with the answer to both questions.

convert -flatten image.png noTransparency.png

This should actually be written like this:

convert image.png -flatten noTransparency.png

First image.png is read and then it is flattened and written to noTransparency.png. But in ImageMagick it 'secretly' adds another image that has the same size as image.png and uses that to flatten image.png. You can write it like this in Magick.NET:

using (MagickImageCollection images = new MagickImageCollection())
{
  MagickImage imagePng = new MagickImage("image.png");

  // 'add background'
  MagickImage background = new MagickImage(imagePng.BackgroundColor, imagePng.Width, imagePng.Height);
  images.Add(background);

  // image.png
  images.Add(imagePng);

  // +flatten
  using (MagickImage result = images.Flatten())
  {
    // noTransparency.png
    result.Write("noTransparency.png");
  }
}

But you can also write it like this:

using (MagickImage imagePng = new MagickImage("image.png"))
{
  imagePng.ColorAlpha(imagePngg.BackgroundColor);
  result.Write("noTransparency.png");
}

And your other example cannot be translated with the current release (7.0.0.0016) because +transparent is not yet supported.

convert original.png +transparent "#E6B15A" singleColor.png1

In the next version you will be able to do this:

using (MagickImage original = new MagickImage("original.png"))
{
  original.InverseTransparent(new MagickColor("#E6B15A"));
  original.Write("singleColor.png");
}

But you could do it like this for now:

using (MagickImage original = new MagickImage("original.png"))
{
  original.InverseOpaque(new MagickColor("#E6B15A"), MagickColor.Transparent);
  original.Write("singleColor.png");
}

And you can always ask for more help here: https://magick.codeplex.com/discussions.

dlemstra
  • 7,813
  • 2
  • 27
  • 43
  • For me, the `MagickImageCollection`+`Flatten` example works great. The `InverseTransparent` example is not yet available, and the `InverseOpaque` example produces a completely white image. – bzlm Oct 08 '15 at 11:43
  • 1
    It seems I completely forgot to add the InverseTransparent method. I created an issue for this: https://magick.codeplex.com/workitem/1365. You can ask for help about InverseOpaque here: https://magick.codeplex.com/discussions – dlemstra Oct 08 '15 at 12:38
  • Just to clarify, the example that creates a canvas and flattens the images does exactly what I want, and I'm not the OP. :) @dlemstra – bzlm Oct 08 '15 at 13:27
  • Is there a Wiki somewhere where we can crowd-source these kinds of how-tos for Magick.Net? – Chris Moschini Nov 13 '15 at 02:12
  • 1
    At this moment there is no such wiki but maybe we could do that on github? https://github.com/dlemstra/Magick.NET – dlemstra Nov 13 '15 at 08:36