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.
There are two ways to overlay bitmaps in Graphics Mill:
The following code adds a semitransparent watermark to an image using the Bitmap.Draw(Bitmap, Int32, Int32, CombineMode) method:
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:
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:
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.
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:
In the collage above two photos are inserted to the placeholders over a background. The following diagram illustrates the idea:
To create a collage like this you should perform the following steps:
The following code creates a collage from the two photos, given mask and background images:
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(); }