Creating Overlay Images in C#


This article shows how easily we can overlay one image onto another and use them in our ASP.NET websites.

Introduction

Most of the times mainly in e-commerce sites, we see lot of overlay images shown to display offers that we get along with the actual product. Example, you get Bluetooth device free with your phone for which Bluetooth image gets displayed on top of the phone image.

Phone Image:

Overlay Promo Image:

It’s easy to create these kinds of Overlay images in ASP.NET with the help of System.Drawing namespace.

Following OverlayImages class will help in creation of overlaying images with different amounts of details and writing the final bitmap image to the file system.

OverlayImages class:
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
 
public class OverlayImages
{
	/// 
	/// Generate Overlay Images 
	/// 
	/// "workingDir">The working dir.
	/// "imagePath">Base image
	/// "transparentImagePath">Overlay image
	/// 
	/// path to generated Overlay image
	/// 
	public static string CreateOverlayImages(string workingDir, string imagePath, string transparentImagePath)
	{
		string finalImagePath;
		string fullPath = string.Empty;
 
		//create a image object containing the photograph to watermark
		fullPath = System.IO.Path.Combine(workingDir, imagePath).ToString();
		finalImagePath = workingDir + System.IO.Path.GetFileNameWithoutExtension(imagePath)+ System.IO.Path.GetFileNameWithoutExtension(transparentImagePath)+".jpg";
 
		// create a bitmap with new PixelFormal and bitmap of actual image to draw from to resolve issue
		// where "A Graphics object cannot be created from an image that has an indexed pixel format."
		Bitmap bmTemp = new Bitmap(fullPath);
		Bitmap bmPhoto = new Bitmap(bmTemp.Width, bmTemp.Height, PixelFormat.Format16bppRgb555);
 
		//load the Bitmap into a Graphics object 
		Graphics grPhoto = Graphics.FromImage(bmPhoto);
 
		//Set the rendering quality for this Graphics object
		grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
 
		//Draws the photo Image object at original size to the graphics object.
		grPhoto.DrawImage(
			bmTemp,							   // Photo Image object
			new Rectangle(0, 0, bmPhoto.Width, bmPhoto.Height), // Rectangle structure
			0,									  // x-coordinate of the portion of the source image to draw. 
			0,									  // y-coordinate of the portion of the source image to draw. 
			bmPhoto.Width,								// Width of the portion of the source image to draw. 
			bmPhoto.Height,							   // Height of the portion of the source image to draw. 
			GraphicsUnit.Pixel);					// Units of measure 
 
		//create a Bitmap the Size of the original photograph
		fullPath = System.IO.Path.Combine(workingDir, transparentImagePath).ToString();
		Bitmap bmOver = new Bitmap(fullPath);
 
		//Draws the photo Image object at original size to the graphics object.
		grPhoto.DrawImage(
			bmOver,							   // Photo Image object
			new Rectangle(0, 0, bmOver.Width, bmOver.Height), // Rectangle structure
			0,									  // x-coordinate of the portion of the source image to draw. 
			0,									  // y-coordinate of the portion of the source image to draw. 
			bmOver.Width,								// Width of the portion of the source image to draw. 
			bmOver.Height,							   // Height of the portion of the source image to draw. 
			GraphicsUnit.Pixel);					// Units of measure 
 
		grPhoto.Dispose();
		bmPhoto.Save(finalImagePath.ToString(), ImageFormat.Jpeg);
		bmPhoto.Dispose();
		bmOver.Dispose();
		finalImagePath.Remove(0, workingDir.Length);
 
		return finalImagePath.ToString();
	}
}

Conclusion 

Hope this simple idea helps you in merging two images in C#.

Related tags

Overlay Images in c#, merging images