Overlaying two images with opacity

Dec 16, 2013 at 9:21 AM
Hi

What would be the easiest way of combining two images if you want to set the transparancy of the upper one to say 50%. I'm working in C#. I've been looking into using composite and changing the alpha channel but I haven't been able to get it to work. Any suggestions? Or links to examples?

Thanks
Coordinator
Dec 16, 2013 at 11:04 AM
I think you are looking for the QuantumOperator method. The following example should make your image 50% transparent.
using (MagickImage image = new MagickImage("input.png"))
{
  image.Alpha(AlphaOption.Set); // If your image does not contain an alpha channel.
  image.QuantumOperator(Channels.Alpha, EvaluateOperator.Set, Quantum.Max / 2);
  image.Write("output.png");
}
After that you should be able to combine both your images using the correct CompositeOperator. You probably have to experiment a bit to find the right one.
Dec 16, 2013 at 1:25 PM
Edited Dec 16, 2013 at 1:29 PM
Thank you! It works great now.
I'm currently overlaying the images by simply merging them, but mainly out of curiosity, if I wanted to use a CompositeOperator, what's the correct syntax for the input args? I'm writing it like this:
public bool combineImagesInCollection(double transparency)
        {
            _images[0].Composite(_images[1], Gravity.Center, CompositeOperator.Blend, transparency.ToString());
            _outImage = _images[0];
            return true;
        }
Thanks again!
Coordinator
Dec 16, 2013 at 2:39 PM
You don't always need the input arguments. You can find the syntax for the operators that need an argument here: http://www.imagemagick.org/script/compose.php.
Feb 4, 2015 at 6:07 PM
Edited Feb 4, 2015 at 6:08 PM
QuantumOperator method is removed on the latest version. Do you have a workaround?
Feb 4, 2015 at 6:41 PM
keremdemirer wrote:
QuantumOperator method is removed on the latest version. Do you have a workaround?
Nevermind, I found the solution from another discussion. http://magick.codeplex.com/discussions/575691