5

I need to convert a JPG image to PNG and change its white background to transparent instead. I am using ImageMagick.NET and I have found the following ImageMagick command that is supposed to do what I am trying to achieve:

convert image.jpg -fuzz XX% -transparent white result.png

I have tried converting this to c# but all I am getting is a png image with a white background. My code snippet:

using (var img = new MagickImage("image.jpg"))
{
     img.Format = MagickFormat.Png;
     img.BackgroundColor = MagickColors.White;
     img.ColorFuzz = new Percentage(10);
     img.BackgroundColor = MagickColors.None;
     img.Write("image.png");
}

Any kind of help will be greatly appreciated. Thank you!!

Jason Roner
  • 865
  • 1
  • 9
  • 14
cara_
  • 63
  • 1
  • 4
  • `img.BackgroundColor = MagickColors.White;` Cant you use `MagickColours.Transparent` instead? (if that exists) – SCramphorn Jul 03 '17 at 07:49
  • Does it make sense that you use BackgroundColor twice?? first a MagickColors.White and then a MagickColors.None? – Blaatz0r Jul 03 '17 at 07:51
  • Thanks a lot for the suggestions. It does exist. MagickColors.Transparent sets the RGBA value to #FFFFFF00. Tried replacing `MagickColors.White` with `MagickColours.Transparent` and removing the second instance of BackgroundColor but it's still giving me the same result. A PNG image with a white background... – cara_ Jul 03 '17 at 07:57

2 Answers2

6

This is a late response as it took me a while to find an answer myself, but this seems to work for me quite well. Look at where the Background property is assigned the Transparent value.

using (var magicImage = new MagickImage())
            {
                var magicReadSettings = new MagickReadSettings
                {
                    Format = MagickFormat.Svg,
                    ColorSpace = ColorSpace.Transparent,
                    BackgroundColor = MagickColors.Transparent,
                    // increasing the Density here makes a larger and sharper output to PNG
                    Density = new Density(950, DensityUnit.PixelsPerInch)
                };

                magicImage.Read("someimage.svg", magicReadSettings);
                magicImage.Format = MagickFormat.Png;

                magicImage.Write("someimage.png");

            }

In my case, I wanted to send this to UWP Image element, so instead of Write(), I did the following after the steps above:

                // Create byte array that contains a png file
                byte[] imageData = magicImage.ToByteArray();

                using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
                {
                    using (DataWriter writer = new DataWriter(stream.GetOutputStreamAt(0)))
                    {
                        writer.WriteBytes(imageData);
                        await writer.StoreAsync();
                    }

                    await bitmapImage.SetSourceAsync(stream);
                }


return bitMapImage; // new BitMapImage() was scoped before all of this

Then on the UWP Image element, simply use:

imageElement.Source = bitMapImage;
Jason Roner
  • 865
  • 1
  • 9
  • 14
5

Most of the arguments on the command line are either properties or method on the MagickImage class. Your command would translate to this:

using (var img = new MagickImage("image.jpg"))
{
    // -fuzz XX%
    img.ColorFuzz = new Percentage(10);
    // -transparent white
    img.Transparent(MagickColors.White);
    img.Write("image.png");
}
dlemstra
  • 7,813
  • 2
  • 27
  • 43
  • I cannot set transparent with this method. The background is still white.I tried many simple png images with white background but the result is the same. The version I used is Magick.NET-Q16-AnyCPU – Martinez Apr 14 '18 at 03:40
  • I used this code, But it also cuts the middle of the original image. For example, suppose a person with a striped shirt (black and white) with a white background in the image. White T-shirt bar is also known as background and cut them. – MohammadSoori Jul 28 '18 at 09:09