public static Bitmap Combine(Bitmap[] images)
{
Bitmap finalImage = null;
try
{
int width = 0;
int height = 0;
foreach (Bitmap bitmap in images)
{
//update the size of the final bitmap
width += bitmap.Width;
height = bitmap.Height > height ? bitmap.Height : height;
}
//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.Transparent);
//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 ex)
{
if (finalImage != null)
finalImage.Dispose();
throw ex;
}
finally
{
//clean up memory
foreach (Bitmap image in images)
{
image.Dispose();
}
}
}