How to combine multiple images into one

Jun 5, 2013 at 11:39 AM
I want to create an image, which will be a mixture of multiple images places in different angles and positions, and overlapped partially.

I wonder, would it be possible to do with MagickImage? I tried a lot but can't figure it out.
Coordinator
Jun 5, 2013 at 12:20 PM
You should use the MagickImageCollection class. Here is an example:
using (MagickImageCollection images = new MagickImageCollection())
{
  MagickImage first = new MagickImage("first.png");
  images.Add(first);
  MagickImage second = new MagickImage("second.png");
  second.Page = new MagickGeometry(50, 50, 0, 0);
  images.Add(second);
  
  MagickImage result = images.Merge(LayerMethod.Mosaic);
  result.Write("output.png");
}
Jun 5, 2013 at 12:51 PM
Thanks dlemstra, you saved my day.
Oct 21, 2013 at 4:23 PM
I am using MagickNET to combine multiple tiff files. It can handle 16 files successfully meaning I can combine 16 files and as soon as I tried to add 17th file, I am getting Out of Memory error. Is there any number of files limitation?

Please help me.
Oct 21, 2013 at 5:08 PM
Please see my code below to combine multiple .bin i.e. tiff files and I am getting "AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt"
    string folder = @"C:\C_R\0\31";
    string destFile = Server.MapPath(".") + "\\test.pdf";
    using (MagickImageCollection images = new MagickImageCollection())
    {
        for (int i = 32750; i < 32766; i++) {
            string fileName = string.Format("{0}.bin", i.ToString());                
            images.Add(new MagickImage(Path.Combine(folder, fileName)));
        }
        MagickImage result = images.Merge();
        result.Write(destFile);

    }

Please help.
Coordinator
Oct 21, 2013 at 5:17 PM
Edited Oct 21, 2013 at 6:08 PM
There is no limit to the number of files you can combine. When you try to combine large images you will eventually run into an out of memory error. What are the dimensions/sizes of your files? And are you using the Q8 or the Q16 version of Magick.NET? You should also Dispose your result variable.

Edit:
Disregard my post above because AccessViolationException is different from Out of Memory. Can you put up a link to your input files? Maybe one of them is corrupt. If you don't want to post your images online feel free to contact me through codeplex.

p.s. Next time please use the edit button instead of posting a new message.
Oct 21, 2013 at 5:34 PM
Edited Oct 21, 2013 at 7:03 PM
I'm using Q8. I tried on another development server with 64bits Windows 2008 server and now I am getting an error: Could not load file or assembly Magick.NET-86.dll and when I try 64 version same thing happen. What other dependencies should I load?

I have redistributed Visual C++ for VS 2010 already on this server.
Coordinator
Oct 21, 2013 at 7:10 PM
Edited Oct 21, 2013 at 7:11 PM
When you run Magick.NET in a 64 bit process you need Magick.NET-x64.dll. You need either the VS2008 or the VS2012 redistributable as describe here: https://magick.codeplex.com/documentation.

Can you please post one of your tif images that throws an AccessViolationException? And did disposing your result image solve your out of memory exception?
Feb 9, 2014 at 5:19 PM
I'm new to your tool but am really impressed thus far. My question is.. I have a pre-created .gif file, lets say a animation of snow falling, and a standard .jpg or .png file of a picture of someone. Is there a way that I can use the .gif image as an overlay on top of the .jpg image? It looks like your tool may be able to do that and I'm hoping you'd have a code sample that may show me the way.

Thanks in advance
Coordinator
Feb 9, 2014 at 6:38 PM
I am not sure if the example below is the most efficient way but it produces the result you want.
using (MagickImageCollection images = new MagickImageCollection("snow.gif"))
{
  using (MagickImage background = new MagickImage("background.jpg"))
  {
    // You only need this when your background image is not the same size.
    background.Resize(images[0].Width, images[0].Height); 

    foreach (MagickImage image in images)
    {
      image.Composite(background, Gravity.Center, CompositeOperator.DstOver);
    }
  }

  images.Optimize();
  images.Write("snow-background.gif");
}
Feb 9, 2014 at 8:51 PM
This was exactly what I needed. Thank you so much!

I couldn't seem to find any information on this subject online that was helpful. Again, thank you very much. I'm going to see if I can get my job to purchase something off of you wish list.