Page 1 of 1

Struggling with the basics: Adding an alpha channel

Posted: 2019-07-17T13:28:24-07:00
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 :)

Re: Struggling with the basics: Adding an alpha channel

Posted: 2019-07-17T14:26:27-07:00
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.