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 blending modes and set the foreground image coordinates. This topic describes both the simple and masked overlaying of images, and provides code snippets for the watermarking and making a collage.

Simple Overlaying

There are two ways to overlay bitmaps in Graphics Mill:

  1. Using the Graphics.DrawImage method. An AdvancedDrawing.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 Graphics.DrawImage(Bitmap, Single, Single, Single) method:

    C#
    using (var bitmap = new Bitmap(@"Images\in.jpg"))
    using (var watermark = new Bitmap(@"Images\watermark.png"))
    using (var graphics = bitmap.GetAdvancedGraphics())
    {
        graphics.DrawImage(watermark, 10, bitmap.Height - watermark.Height - 40, 0.8f);
        bitmap.Save(@"Images\Output\out.jpg");
    }
    
  2. Using the Bitmap.Draw method. This method should be called 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:

    Note

    This method takes values of the Transforms.CombineMode enumeration, which is out of date.

    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);
    
        bitmap.Draw(watermark, 10, bitmap.Height - watermark.Height - 40,
            CombineMode.Alpha);
        bitmap.Save(@"Images\Output\out.jpg");
    }
    

The watermarked image:

Watermarking an image using alpha blending mode

Overlaying Using Masks

Another way to combine images is to use one of the masked transforms, such as the Blender transform. Masked transforms allow for 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 Blender transform, you can create a collage from several images on the same background, like the following one:

A collage created using Blender

In the collage above, two photos are inserted into 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's 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 Blender transform. You can set the blending mode by passing it as a constructor argument.
  4. Specify the photo to combine via the Blender.TopImage property.
  5. Apply the Blender 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 blender = new Blender(Aurigma.GraphicsMill.Transforms.BlendMode.Normal);

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

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

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

    collage.Dispose();
}

See Also

Reference

Manual