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

Precisely placing multiple overlapping images

Jan 4, 2015 at 11:49 PM
Edited Jan 5, 2015 at 1:26 AM
How do I precisely place multiple overlapping images? Below is the code I have so far. It overlaps the images at their top left corner. See example below.
string[] files = Directory.GetFiles(".", "*.png", SearchOption.TopDirectoryOnly);
MagickImageCollection collection = new MagickImageCollection();
for (int i = 0; i < files.Length; i++) {
    collection.Add(files[i]);
}
MagickImage result = collection.Mosaic();
result.Write(@"output\output.png");
How do use composite in Magick.NET?
http://www.imagemagick.org/Usage/layers/#composite

Image

Background: My code will intelligently merge multiple images into one so they overlap where the pixels are identical. I'm creating 10 to 20 random screen shots zoomed into paused games and I need to combine them into one large picture. Each picture could be a different width and height. I don't want any distortion of the original images so photo panoramic software will not work for me.

How do I position each image anywhere I want by specifying either an absolute or relative top and left?

Would it be easiest if I copied the pixels of the images into my own 2D arrays then complete the manipulation then copy it back to an output MagickImage image then write it out?
Jan 5, 2015 at 12:07 PM
Edited Jan 6, 2015 at 1:23 PM
You can do this with the Composite method of MagickImage. Below is a simple example:
using (MagickImage canvas = new MagickImage("xc:none", 100, 100))
{
  using (MagickImage red = new MagickImage("xc:red", 50, 50))
  {
    int x = 30;
    int y = 20;
    canvas.Composite(red, new MagickGeometry(x, y, red.Width, red.Height), CompositeOperator.Over);
  }

  using (MagickImage yellow = new MagickImage("xc:yellow", 100, 10))
  {
    int x = 0;
    int y = 30;
    canvas.Composite(yellow, new MagickGeometry(x, y, yellow.Width, yellow.Height), CompositeOperator.Over);
  }

  using (MagickImage purple= new MagickImage("xc:purple", 10, 100))
  {
    int x = 50;
    int y = 0;
    canvas.Composite(purple, new MagickGeometry(x, y, purple.Width, purple.Height), CompositeOperator.Over);
  }

  canvas.Write(@"c:\test.png");
}
Jan 6, 2015 at 12:35 PM
That worked! Thanks! BTW, I compiled your code and discovered one error in the last Composite() call.

This line, yellow
canvas.Composite(yellow, new MagickGeometry(x, y, purple.Width, purple.Height), CompositeOperator.Over);
should be purple
canvas.Composite(purple, new MagickGeometry(x, y, purple.Width, purple.Height), CompositeOperator.Over);
FYI, the hardest part about using ImageMagick is the lack of documentation. When I search Google for MagickGeometry Composite site:magick.codeplex.com, the only pages that mention it are under the Discussion section. I've relied mainly on code samples in the Discussion section, the Visual Studio Object Browser and Visual Studio Intellisense, to figure things out.

2 Questions:
  1. Are there any plans to fully document ImageMagic.NET?
  2. What percentage of ImageMagic functionality is made available in ImageMagic.NET?
Jan 6, 2015 at 1:23 PM
Edited Jan 6, 2015 at 1:24 PM
Sorry about the typo :) That will teach me to change code in this editor instead of Visual Studio. I have fixed the example.

And here is an answer to your questions:
  1. There is already a lot of documentation available for ImageMagick. One of the best sites is this one: http://www.imagemagick.org/Usage. I might document all the API methods in the future but this really is a lot of work.
  2. I am aiming for 100% but please let if me know if something seems to be missing.
p.s. Please don't call it ImageMagic.NET its Magick.NET :)