Combine two or more images into one in C#

How to combine two or more pictures into one creating the illusion that it’s just one picture?

public static System.IO.MemoryStream CombineImages(byte[] imageBytes1, byte[] imageBytes2)
{
    if ((imageBytes1 == null || imageBytes1.Length == 0) ||
        (imageBytes2 == null || imageBytes2.Length == 0))
        return null;

    //convert bytes to Image
    var image1 = System.Drawing.Image.FromStream(new System.IO.MemoryStream(imageBytes1));
    var image2 = System.Drawing.Image.FromStream(new System.IO.MemoryStream(imageBytes2));

    //create the Bitmap object
    var bitmap = new System.Drawing.Bitmap(image1.Width, image1.Height, PixelFormat.Format32bppPArgb);
    //create the Graphics object
    var g = System.Drawing.Graphics.FromImage(bitmap);

    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

    g.DrawImage(image1, 0, 0);

    g.DrawImage(image2, 0, 0);

    var ms = new System.IO.MemoryStream(bitmap.Width * bitmap.Height);

    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

    return ms;

}

 

 

Advertisements

4 thoughts on “Combine two or more images into one in C#

  1. hey thanks for this code but i need some help.. when we combine 2 images the size of the image gets varied. ie.. the image is not same as the original image

    1. The resulting size should be same as the image you pass via parameter imageBytes1. The bitmap variable sets the height and width of the resulting image.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s