This documentation is for the old version. Go to the latest Graphics Mill docs

Overlaying Images

Graphics Mill for .NET provides rich image blending features. It allows overlaying one image over another one. You can set different combine modes, coordinates of the source image, the rectangle at the destination bitmap to fit the image in, and even a portion of source image to get. Also you can specify overall opacity of the source image.

Simple Overlaying

There are two equivalent ways to draw the bitmap with Graphics Mill for .NET:

  1. Use Draw method of the Bitmap class. This bitmap is used as source, destination bitmap is passed as an argument into this method.
  2. Use DrawImage method of the GdiGraphics class. When you create GdiGraphics instance, you associate it with destination bitmap. Source bitmap is passed as an argument.

The code example below demonstrates the how to blend two bitmaps with alpha blending combine mode.

Alpha blending combine mode example
Fig. 1. Alpha blending combine mode example

Visual Basic
'Load background image
Dim bitmap As New Aurigma.GraphicsMill.Bitmap("C:\pyramid.jpg")

'Load small image (foreground image)
Dim smallBitmap As New Aurigma.GraphicsMill.Bitmap("C:\watermark.png")

'Draw foreground image on background with transparency
smallBitmap.Draw(bitmap, 10, bitmap.Height - smallBitmap.Height - 10, _
    smallBitmap.Width, smallBitmap.Height, _
    Aurigma.GraphicsMill.Transforms.CombineMode.Alpha, 0.7F, _
    Aurigma.GraphicsMill.Transforms.InterpolationMode.HighQuality)
C#
//Load background image
Aurigma.GraphicsMill.Bitmap bitmap =
    new Aurigma.GraphicsMill.Bitmap(@"C:\pyramid.jpg");

//Load small image (foreground image)
Aurigma.GraphicsMill.Bitmap smallBitmap =
    new Aurigma.GraphicsMill.Bitmap(@"C:\watermark.png");

//Draw foreground image on background with transparency
smallBitmap.Draw(bitmap, 10, bitmap.Height-smallBitmap.Height-10, 
    smallBitmap.Width, smallBitmap.Height, 
    Aurigma.GraphicsMill.Transforms.CombineMode.Alpha, 0.7f, 
    Aurigma.GraphicsMill.Transforms.InterpolationMode.HighQuality);

The arguments of the Draw method above mean the following:

  1. bitmap is the image on which the drawing will be made.
  2. 10 is the top left X-coordinate of the overlaying image.
  3. bitmap.Height-smallBitmap.Height-10 is the top left Y-coordinate of the overlaying image.
  4. smallBitmap.Width is the width of the overlaying image.
  5. smallBitmap.Height is the height of the overlaying image.
  6. Aurigma.GraphicsMill.Transforms.CombineMode.Alpha is the algorithm of blending images. In this example it is alpha blending.
  7. 0.7f is the opacity of the overlaying image. The smaller this value is, the more transparent it will be.
  8. Aurigma.GraphicsMill.Transforms.InterpolationMode.HighQuality is the algorithm of resizing the overlaying image.

Masked Transforms

There is also another way to combine images—using a masked transform. A masked transform is a transform that allows applying effects to a region defined by a mask. Such transforms inherit from the MaskedBitmapTransform. The mask that sets the areas where the changes will be made is a grayscale bitmap, where the black areas will be transparent, and the white ones will be opaque.

The Combiner transform belongs to to such transforms. It lets you combine several images on the same background using a mask. Look at the following picture:

Masked transform example
Fig. 2. Masked transform example

In this picture, two photographs are positioned at the specified placeholders over a background. To do that you will need to carry out these steps:

  1. Define the mask. The mask should be a grayscale image of the same size as the background image. Pixels in the areas of the placeholders should be white. If the edges of these areas are blurred, you will achieve a gradient effect at the edges.
  2. Define the placeholder coordinates. They will be used to position the photographs.
  3. Create and initialize the combine transform. When initializing it, you can choose from available combine modes. For their list and description, see the Combine Modes topic.
  4. Apply the combine transform to the background using the first photograph that needs to be merged. For that the MaskedBitmapTransform.ApplyMaskTransform method should be called; it takes a reference to the mask bitmap as a parameter.
  5. Repeat Steps 3 and 4 for the second photograph.
  6. Save the background.

To make the above instructions clearer, look at the following diagram and at the code example below.

Masked transform diagram
Fig. 3. Masked transform exploded diagram

Visual Basic
'Photos for the collage.
Dim photo1 As New Aurigma.GraphicsMill.Bitmap("D:\photo1.jpg")
Dim photo2 As New Aurigma.GraphicsMill.Bitmap("D:\photo2.jpg")

'Background image.
Dim background As New Aurigma.GraphicsMill.Bitmap("D:\background.jpg")

'Mask.
Dim mask As New Aurigma.GraphicsMill.Bitmap("D:\mask.jpg")

'Coordinates of placeholders.
Dim point1 As New Point(160, 160)
Dim point2 As New Point(550, 420)

'Create the transform.
Dim combiner As New Aurigma.GraphicsMill.Transforms.Combiner
combiner.CombineMode = Aurigma.GraphicsMill.Transforms.CombineMode.Copy

'Combine with the first image.
combiner.SourceBitmap = photo1
combiner.SourceRectangle = New RectangleF(0, 0, photo1.Width, photo1.Height)
combiner.DestinationRectangle = New RectangleF(point1.X - photo1.Width / 2, _
  point1.Y - photo1.Height / 2, photo1.Width, photo1.Height)
combiner.ApplyMaskTransform(background, mask)

'Combine with the second image.
combiner.SourceBitmap = photo2
combiner.SourceRectangle = New System.Drawing.RectangleF(0, 0, photo2.Width, _
  photo2.Height)
combiner.DestinationRectangle = New System.Drawing.RectangleF(point2.X - _
  photo2.Width / 2, point2.Y - photo2.Height / 2, photo2.Width, _
  photo2.Height)
combiner.ApplyMaskTransform(background, mask)

background.Save("D:\collage.jpg")
C#
//Photos for the collage.
Aurigma.GraphicsMill.Bitmap photo1 = new Aurigma.GraphicsMill.Bitmap(@"D:\photo1.jpg");
Aurigma.GraphicsMill.Bitmap photo2 = new Aurigma.GraphicsMill.Bitmap(@"D:\photo2.jpg");

//Background image.
Aurigma.GraphicsMill.Bitmap background = new
    Aurigma.GraphicsMill.Bitmap(@"D:\background.jpg");

//Mask.
Aurigma.GraphicsMill.Bitmap mask = new Aurigma.GraphicsMill.Bitmap(@"D:\mask.jpg");

//Coordinates of placeholders.
Point point1 = new Point(160, 160);
Point point2 = new Point(550, 420);

//Create the transform.
Aurigma.GraphicsMill.Transforms.Combiner combiner = new
    Aurigma.GraphicsMill.Transforms.Combiner();
combiner.CombineMode = Aurigma.GraphicsMill.Transforms.CombineMode.Copy;

//Combine with the first image.
combiner.SourceBitmap = photo1;
combiner.SourceRectangle = new RectangleF(0, 0, photo1.Width, photo1.Height);
combiner.DestinationRectangle = new RectangleF(point1.X - photo1.Width / 2,
    point1.Y - photo1.Height / 2, photo1.Width, photo1.Height);
combiner.ApplyMaskTransform(background, mask);

//Combine with the second image.
combiner.SourceBitmap = photo2;
combiner.SourceRectangle = new System.Drawing.RectangleF(0, 0, photo2.Width,
    photo2.Height);
combiner.DestinationRectangle = new System.Drawing.RectangleF(point2.X -
    photo2.Width / 2, point2.Y - photo2.Height / 2, photo2.Width, photo2.Height);
combiner.ApplyMaskTransform(background, mask);

background.Save(@"D:\collage.jpg");

See Also

Reference

Manual