Overlaying Images

Graphics Mill provides rich image blending features. It allows the overlaying of images, including masked overlaying. Also you can select one of many available combine modes and set the foreground image coordinates. The topic describes both simple and masked overlaying of images, and provides watermarking and making of a collage code snippets.

Simple Overlaying

There are two ways to overlay bitmaps in Graphics Mill:

  1. Using the Bitmap.Draw method. The method should be called for the background bitmap, and the foreground bitmap is passed as an argument of the method.
  2. Using the Graphics.DrawImage method. A Drawing.Graphics instance is created for the background bitmap, and the foreground bitmap is passed as an argument of the method.

The following code adds a semitransparent watermark to an image using the Bitmap.Draw(Bitmap, Int32, Int32, CombineMode) method:

C#
using (var bitmap = new Bitmap(@"Images\in.jpg"))
using (var watermark = new Bitmap(@"Images\watermark.png"))
{
    //Make the watermark semitransparent.
    watermark.Channels.ScaleAlpha(0.8F);

    //Watermark image
    bitmap.Draw(watermark, 10, bitmap.Height - watermark.Height - 40, CombineMode.Alpha);

    //Save the resulting image
    bitmap.Save(@"Images\Output\out.jpg");
}

The following code adds a semitransparent watermark to an image using the Graphics.DrawImage(Bitmap, Int32, Int32, CombineMode) method:

C#
using (var bitmap = new Bitmap(@"Images\in.jpg"))
using (var watermark = new Bitmap(@"Images\watermark.png"))
{
    //Make the watermark semitransparent.
    watermark.Channels.ScaleAlpha(0.8F);

    //Watermark an image.
    using (var graphics = bitmap.GetGraphics())
    {
        graphics.DrawImage(watermark, 10, bitmap.Height - watermark.Height - 40, CombineMode.Alpha);
        //Save the resulting image
        bitmap.Save(@"Images\Output\out.jpg");
    }
}

The watermarked image:

Watermarking an image using alpha blending combine mode

The code above utilizes the Alpha combine mode. This mode preserves the opacity of the foreground image. For more information see the Combine Modes topic.

Overlaying Using Masks

Another way to combine images is to use one of masked transforms, the Combiner transform. Masked transform allows applying effects to a region defined by a mask. All transforms which can be applied with a mask have a common base class, MaskTransform. Using the Combiner transform you can create a collage from several images on the same background, like the following one:

A collage created using Combiner

In the collage above two photos are inserted to the placeholders over a background. The following diagram illustrates the idea:

Masked transform diagram

To create a collage like this you should perform the following steps:

  1. Define the mask bitmap. The mask should be a grayscale image (Format8bppGrayscale) of the same size as the background image. White areas of the mask are placeholders. If the placeholder area edges are blurred, it will result in a gradient effect at the edges.
  2. Define the placeholder coordinates. They will be used to position the photos.
  3. Create the Combiner transform. You can set the combine mode by passing it as a constructor argument. For more information see the Combine Modes topic.
  4. Specify the photo to combine via the Combiner.TopImage property.
  5. Apply the Combiner transform using the MaskTransform.ApplyMaskTransform(Bitmap, Bitmap) method and save the result.
  6. Repeat Steps 4 and 5 for the second photo.
  7. Save the resulting image to a file.

The following code creates a collage from the two photos, given mask and background images:

C#
using (var photo1 = new Bitmap(@"Images\monkey.jpg"))
using (var photo2 = new Bitmap(@"Images\dog.jpg"))
using (var background = new Bitmap(@"Images\CollageBackground.jpg"))
using (var mask = new Bitmap(@"Images\CollageMask.jpg"))
{
    //Resulting collage.
    var collage = new Bitmap();

    //Coordinates of placeholders.
    var point1 = new System.Drawing.Point(130, 165);
    var point2 = new System.Drawing.Point(460, 200);

    //Create the transform.
    var combiner = new Combiner(CombineMode.Copy);

    //Combine with the first image.
    combiner.TopImage = photo1;
    combiner.X = point1.X - photo1.Width / 2;
    combiner.Y = point1.Y - photo1.Height / 2;
    collage = combiner.ApplyMaskTransform(background, mask);

    //Combine with the second image.
    combiner.TopImage = photo2;
    combiner.X = point2.X - photo2.Width / 2;
    combiner.Y = point2.Y - photo2.Height / 2;
    collage = combiner.ApplyMaskTransform(collage, mask);

    collage.Save(@"Images\Output\collage.jpg");

    collage.Dispose();
}

See Also

Reference

Manual