cara_
cara_

Reputation: 63

Convert JPG to PNG with background transparency using ImageMagick.Net

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!!

Upvotes: 5

Views: 7708

Answers (2)

Jason Roner
Jason Roner

Reputation: 905

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;

Upvotes: 7

dlemstra
dlemstra

Reputation: 8153

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");
}

Upvotes: 6

Related Questions