How to merge images in one image


This code is help you to merge images into one image means combination of images but one this we have to remind that in images folder we have to remove Thumbs.db because if Thumbs.db is exist then this code will throw exception so we have to remove Thumbs.db.

First of create a function named Combine. this function merge images. You can put This code on page load also but i calling this function on button click event.


public static Bitmap Combine(string[] files)
{
//read all images into memory
List images = new List();
Bitmap finalImage = null;

try
{
int width = 0;
int height = 0;

foreach (string image in files)
{
//create a Bitmap from the file and add it to the list
Bitmap bitmap = new Bitmap(image);

//update the size of the final bitmap
width += bitmap.Width;
height = bitmap.Height > height ? bitmap.Height : height;

images.Add(bitmap);
}

//create a bitmap to hold the combined image
finalImage = new Bitmap(width, height);

//get a graphics object from the image so we can draw on it
using (Graphics g = Graphics.FromImage(finalImage))
{
//set background color
g.Clear(Color.Black);

//go through each image and draw it on the final image
int offset = 0;
foreach (Bitmap image in images)
{
g.DrawImage(image,
new Rectangle(offset, 0, image.Width, image.Height));
offset += image.Width;
}
}

return finalImage;
}
catch (Exception)
{
if (finalImage != null)
finalImage.Dispose();

throw;
}
finally
{
//clean up memory
foreach (Bitmap image in images)
{
image.Dispose();
}
}
}


And then calling this function on button click event like this


protected void BtnMerge_Click(object sender, EventArgs e)
{
string[] Imagefiles = Directory.GetFiles(Server.MapPath("images"));
Bitmap stitchedImage = Combine(Imagefiles);
stitchedImage.Save(@"D:\Trial_Testing\Images\stitchedImage.jpg", ImageFormat.Jpeg);
}


In above code means button click event

string[] Imagefiles = Directory.GetFiles(Server.MapPath("images"));

In this code i give the path of the images
And

Bitmap stitchedImage = Combine(Imagefiles);

In this code i calling the function of Combine and give file array means file's path array. And finally

stitchedImage.Save(@"D:\Trial_Testing\Images\stitchedImage.jpg", ImageFormat.Jpeg);

In this code i saving image which is merged into one image on my local drive and spesific folder

And in function

List images = new List();
Bitmap finalImage = null;

First create object of list(Bitmap) and Bitmap
And Then

int width = 0;
int height = 0;

foreach (string image in files)
{
//create a Bitmap from the file and add it to the list
Bitmap bitmap = new Bitmap(image);

//update the size of the final bitmap
width += bitmap.Width;
height = bitmap.Height > height ? bitmap.Height : height;

images.Add(bitmap);
}

This code count the number of images and add to a single bitmap. and yes here you can give the spesific height and width of the final image means merged image.
And

//create a bitmap to hold the combined image
finalImage = new Bitmap(width, height);

//get a graphics object from the image so we can draw on it
using (Graphics g = Graphics.FromImage(finalImage))
{
//set background color
g.Clear(Color.Black);

//go through each image and draw it on the final image
int offset = 0;
foreach (Bitmap image in images)
{
g.DrawImage(image,
new Rectangle(offset, 0, image.Width, image.Height));
offset += image.Width;
}
}

return finalImage;
}
catch (Exception)
{
if (finalImage != null)
finalImage.Dispose();

throw;
}
finally
{
//clean up memory
foreach (Bitmap image in images)
{
image.Dispose();
}
}

This code shows hold the images and draw the image with merging of all images and return the final image which is merged.
Imp Note:- Don't forget to remove Thumbs.Db from source folder


Comments

Author: Muhammad Farooq18 Nov 2010 Member Level: Gold   Points : 1

use image merger software or online image merger
See links

http://www.imagemerger.net/

http://www.pagecolumn.com/tool/imagemerger.htm

Regards
__________________________________-
Muhammad Farooq
http://fonts-guru.blogspot.com/

Guest Author: hasan17 Aug 2013

Thanks for the nice code: i try this code with vb.net here 'is vb.net code.

'====================================================

Public Shared Function Combine(files As String()) As Bitmap
'read all images into memory
Dim images As New List(Of Image)

Dim finalImage As Bitmap = Nothing

Try
Dim width As Integer = 0
Dim height As Integer = 0

For Each image As String In files
'create a Bitmap from the file and add it to the list
Dim bitmap As New Bitmap(image)

'update the size of the final bitmap
width += bitmap.Width
height = If(bitmap.Height > height, bitmap.Height, height)

images.Add(bitmap)
Next

'create a bitmap to hold the combined image
finalImage = New Bitmap(width, height)

'get a graphics object from the image so we can draw on it
Using g As Graphics = Graphics.FromImage(finalImage)
'set background color
g.Clear(Color.Black)

'go through each image and draw it on the final image
Dim offset As Integer = 0
For Each image As Bitmap In images
g.DrawImage(image, New Rectangle(offset, 0, image.Width, image.Height))
offset += image.Width
Next
End Using

Return finalImage
Catch generatedExceptionName As Exception
If finalImage IsNot Nothing Then
finalImage.Dispose()
End If

Throw
Finally
'clean up memory
For Each image As Bitmap In images
image.Dispose()
Next
End Try
End Function


=====================================

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim Imagefiles As String() = Directory.GetFiles("c:\images")
Dim stitchedImage As Bitmap = Combine(Imagefiles)
stitchedImage.Save("c:\images\stitchedImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
End Sub



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: