using System.Drawing;
public static System.Drawing.Bitmap Combine(string[] files)
{
Create a list for images and read images
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;
if(bitmap.Height > height )
{
height = bitmap.Height ;
}
else
{
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 System.Drawing.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();
}
}
}
public static System.Drawing.Bitmap Combine(string[] files)
{
Create a list for images and read images
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;
if(bitmap.Height > height )
{
height = bitmap.Height ;
}
else
{
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 System.Drawing.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();
}
}
}