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

Merging Images Together

May 15, 2014 at 7:06 AM
I've asked this question once before, but the original solution no longer works after I recently upgraded to the newer Magick.NET.

I am trying to put an image on a white background. My 'output' must be a specific size, so I am putting a white background behind any image uploaded so that it always ends up the correct size. The code below works, except that the 'from' image needs to be centered. I used to be able to center it, but that option gives a compile error now.

I just need to know how to do the 'merge' and center the top image.
        using (MagickImageCollection images = new MagickImageCollection())
        {
            MagickImage from = new MagickImage(strTempFile);

            Point newP = GetMinRatio(from.Height, from.Width, MaxHeight, MaxWidth);

            MagickImage first = new MagickImage(Color.White, newP.X, newP.Y);
            first.Density = new MagickGeometry(300, 300);
            first.BitDepth(24);
            if (first.ColorSpace != ColorSpace.sRGB && first.ColorSpace != ColorSpace.RGB)
            {
                first.AddProfile(ColorProfile.SRGB);
                first.ColorSpace = ColorSpace.sRGB;
            }
            if (strNewExt.ToLower() == ".tiff"
            || strNewExt.ToLower() == ".tif")
            {
                first.CompressionMethod = CompressionMethod.LZW;
                first.Format = MagickFormat.Tiff;
            }
            if (strNewExt.ToLower() == ".jpeg" || strNewExt.ToLower() == ".jpg")
            {
                first.CompressionMethod = CompressionMethod.LosslessJPEG;
            }

            images.Add(first);

            images.Add(from);

            MagickImage result = images.Merge();
            result.Density = new MagickGeometry(300, 300);
            result.BitDepth(24);
            if (result.ColorSpace != ColorSpace.sRGB && result.ColorSpace != ColorSpace.RGB)
            {
                result.AddProfile(ColorProfile.SRGB);
                result.ColorSpace = ColorSpace.sRGB;
            }
            if (strNewExt.ToLower() == ".tiff"
            || strNewExt.ToLower() == ".tif")
            {
                result.CompressionMethod = CompressionMethod.LZW;
                result.Format = MagickFormat.Tiff;
            }
            if (strNewExt.ToLower() == ".jpeg" || strNewExt.ToLower() == ".jpg")
            {
                result.CompressionMethod = CompressionMethod.LosslessJPEG;
            }
            result.Write(strFileName);
        }
May 15, 2014 at 9:04 PM
I think you can do this much easier with the Extent method of MagickImage.
MagickImage image = new MagickImage(strTempFile);
image.Extent(width, height, Gravity.Center, new MagickColor("#fff"));
May 16, 2014 at 1:24 AM
That worked. Is this documented anywhere? It would have saved a ton of time to know about the 'Extent' command.
May 16, 2014 at 9:08 AM
You could have found it by going to the following page 'Cutting and Bordering' (http://www.imagemagick.org/Usage/crop/) from this page: http://www.imagemagick.org/Usage/. This link is also on the bottom of the documentations page of the Magick.NET project.
May 16, 2014 at 3:29 PM
Oh, that would have been handy!! Thanks!