Overlaying / compositing images using c# – System.Drawing

If you have an image or images you want to apply another image on top of, it’s easy using CompositingMode & DrawImage in .net. Below is simple example showing a .jpg photo overlaying with a transparent .png file. You could use this technique to apply a custom watermark, logo, copyright, etc. to images. In the code are also some options for showing the image and saving the final file. Happy compositing!

            Bitmap baseImage;
            Bitmap overlayImage;

            baseImage = (Bitmap)Image.FromFile(@"C:\temp\base.jpg");

            overlayImage = (Bitmap)Image.FromFile(@"C:\temp\overlay.png");

            var finalImage = new Bitmap(overlayImage.Width, overlayImage.Height, PixelFormat.Format32bppArgb);
            var graphics = Graphics.FromImage(finalImage);
            graphics.CompositingMode = CompositingMode.SourceOver;

            graphics.DrawImage(baseImage, 0, 0);
            graphics.DrawImage(overlayImage, 0, 0);

            //show in a winform picturebox
            pictureBox1.Image = finalImage;

            //save the final composite image to disk
            finalImage.Save(@"C:\temp\final.jpg", ImageFormat.Jpeg);

You’ll probably want to include:

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
Overlaying / compositing images using c# – System.Drawing