This project has moved and is read-only. For the latest updates, please go here.

Question about composite command and images of different sizes

Aug 26, 2014 at 9:47 PM
Hello,
I'm trying to apply a watermark to an image.
I've got an example image that is 1024x768 and a watermark image that is 584x116.
I'm resizing the watermark to match the example, then using the composite command to merge the two images.
using (MagickImage image = new MagickImage("example.jpg"))
{
 using (MagickImage watermark = new MagickImage("watermark.png"))
  {
           //resize watermark to fit the proof image
           watermark.Resize(image.Width,image.Height);
 
           image.Composite(watermark, Gravity.Center, CompositeOperator.Dissolve, "11");
           image.Write("examplewatermarked.png");
   }
}
The result is an image with solid black on the top and bottom, while the middle where the watermark is applied is displaying correctly. Looking at the watermark after the resize command, the height is scaled up as expected but it does not match the example image's canvas height (768). Is there any way to make the result not black if the canvas size doesn't match?
I'm sure there is something simple I'm missing in order to make it work.

Thanks for any help.
Aug 27, 2014 at 6:14 AM
Your image is not Resized to the specified width and height because it will maintain aspect ratio. When the image is drawn on top it will make the 'unknown' part black. Settings the background to Transparent and using the Extent method should solve your problem.
using (MagickImage image = new MagickImage("example.jpg"))
{
  using (MagickImage watermark = new MagickImage("watermark.png"))
  {
    //resize watermark to fit the proof image
    watermark.Resize(image.Width, image.Height);
    watermark.BackgroundColor = MagickColor.Transparent;
    watermark.Extent(image.Width, image.Height);

    image.Composite(watermark, Gravity.Center, CompositeOperator.Dissolve, "11");
    image.Write("examplewatermarked.png");
  }
}
Aug 27, 2014 at 3:25 PM
Thanks for the reply dlemstra.
I've tried your suggestion, and I'm no longer getting black borders, however, now the entire image is white(though the watemark is visible). It appears that the transparency is lost once the Extent command is applied to the watermark. Is there an alternative to Extent, or is there another command to keep the transparency after the Extent command?

Thanks again!
Aug 28, 2014 at 4:08 PM
dlemstra,
Originally I was using the recommended download (x64) and I was not getting a transparent background. I switched over the the x86 version and now I'm getting the transparent background by using your example code above.
Aug 28, 2014 at 6:52 PM
Was your old x86 Magick.NET 7.0.0.0001? I fixed a bug in the composite method that could explain this. I cannot reproduce your problem with the latest version in 64 bit.
Aug 29, 2014 at 5:43 PM
Yes, I was using the x64 version of 7.0.0.0001.
I've switched to the x86 version and it's working as intended. I'll redownload the x64 version and try it again.
Mar 5, 2015 at 7:39 PM
I just downloaded 7.0.0.0010 x64 and I'm having the same issue. My png watermark is no longer transparent. I haven't tried the x86 version yet.
Mar 5, 2015 at 7:53 PM
Matt what is the exact problem that you are experiencing? Can you post a small example and any images that you are using in that example?
Mar 5, 2015 at 7:57 PM
                    using (MagickImageCollection images = new MagickImageCollection())
                    {
                        //add the 1st image
                        MagickImage image = new MagickImage(f.FullName.ToString());
                        images.Add(image);

                        //add the watermark
                        MagickImage watermark = new MagickImage(Path.Combine(appPath, "watermark.png"));
                        images.Add(watermark);

                        //do the composite and write over the image
                        image.Composite(watermark, Gravity.Southeast);
                        image.Write(f.FullName.ToString());
                    }
works pretty good. Except the PNG has a black background.

On an different note... back when I ran composite at the command prompt I was able to do -geometry +5+0. How do I do that here?

Thanks.
Mar 5, 2015 at 8:12 PM
I really need a bit more information. Which PNG has a black background? Your watermark?

If you need to specify a position you should use the overload of Composite that accepts an x and y position. You might also want to experiment with different CompositeOperator values.
Mar 5, 2015 at 8:23 PM
Sorry,
So yeah the main image is a jpg and the watermark is a png. When composited, the transparent area of the PNG turns black

Here's the composited image
Image


here's the logo:
Image
Mar 5, 2015 at 8:32 PM
Edited Mar 5, 2015 at 8:33 PM
The default composite operator is CompositeOperator.In. And because your source image does not contain any transparency it will use black for the transparent pixels.

You probably want to switch to 'CompositeOperator.Over'. Or use Dissolve if you want to hide it a bit (see my example above for the syntax).