Struggling with the basics: Adding an alpha channel

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
AfterEight_17
Posts: 1
Joined: 2019-07-17T13:14:03-07:00
Authentication code: 1152

Struggling with the basics: Adding an alpha channel

Post by AfterEight_17 »

Hello,
I have recently started to learn about Magick.NET, however, I am struggling with understanding how to add masks to an image.

Here is my input image (all blue background for simplicity but in reality I will be using a more complex image):
Image

Here is what I want to create (A house shape with a transparent background):
Image

I understand that I need to add an alpha channel to my "input" image but the below code does not seem to be working.
I just get a completely white image when saving.

Code: Select all

using (MagickImage input = new MagickImage("INPUT.png"),
                               mask = new MagickImage(MagickColors.White, 500, 500)
            )
            {
                mask.Settings.FillColor = MagickColors.Black;
                List<PointD> points = new List<PointD>
                {
                    new PointD(250, 50),
                    new PointD(150, 150),
                    new PointD(150, 400),
                    new PointD(350, 400),
                    new PointD(350, 150)
                };
                mask.Draw(new DrawablePolygon(points));

                input.Alpha(AlphaOption.Transparent);
                input.Composite(mask, CompositeOperator.Alpha);

                input.Write("OUTPUT.png");
            }
Any advice would be greatly appreciated :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Struggling with the basics: Adding an alpha channel

Post by fmw42 »

Sorry, I do not know Magick.net. But in command line you would do that with a composite using a compose method of copy_opacity. See https://imagemagick.org/Usage/compose/#copyopacity. Look for similar named methods in Magick.net.
Post Reply