Hi nauna,
Check this example. Now please take its reference and correct your code.
HTML
<b>Original Images</b>
<hr />
<asp:Image ID="Image1" runat="server" />
<asp:Image ID="Image2" runat="server" />
<asp:Image ID="Image3" runat="server" />
<br />
<br />
<b>Combined Image</b>
<hr />
<asp:Image ID="imgFinal" runat="server" />
C#
protected void Page_Load(object sender, EventArgs e)
{
System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(Server.MapPath("~/Images"));
if (directory != null)
{
System.IO.FileInfo[] files = directory.GetFiles();
Image1.ImageUrl = "Images/" + files[0].Name;
Image2.ImageUrl = "Images/" + files[1].Name;
Image3.ImageUrl = "Images/" + files[2].Name;
string finalImage = Server.MapPath("~/Final/") + "FinalImage.jpg";
CombineImages(files, finalImage);
imgFinal.ImageUrl = "Final/FinalImage.jpg";
}
}
private void CombineImages(System.IO.FileInfo[] files, string destination)
{
System.Collections.Generic.List<int> imageHeights = new System.Collections.Generic.List<int>();
int nIndex = 0;
int width = 0;
foreach (System.IO.FileInfo file in files)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(file.FullName);
imageHeights.Add(img.Height);
width += img.Width;
img.Dispose();
}
imageHeights.Sort();
int height = imageHeights[imageHeights.Count - 1];
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height);
System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(bitmap);
graphic.Clear(System.Drawing.SystemColors.AppWorkspace);
foreach (System.IO.FileInfo file in files)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(file.FullName);
if (nIndex == 0)
{
graphic.DrawImage(img, new System.Drawing.Point(0, 0));
nIndex++;
width = img.Width;
}
else
{
graphic.DrawImage(img, new System.Drawing.Point(width, 0));
width += img.Width;
}
img.Dispose();
}
graphic.Dispose();
bitmap.Save(destination, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim directory As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(Server.MapPath("~/Images"))
If directory IsNot Nothing Then
Dim files As System.IO.FileInfo() = directory.GetFiles()
Image1.ImageUrl = "Images/" & files(0).Name
Image2.ImageUrl = "Images/" & files(1).Name
Image3.ImageUrl = "Images/" & files(2).Name
Dim finalImage As String = Server.MapPath("~/Final/") & "FinalImage.jpg"
CombineImages(files, finalImage)
imgFinal.ImageUrl = "Final/FinalImage.jpg"
End If
End Sub
Private Sub CombineImages(ByVal files As System.IO.FileInfo(), ByVal destination As String)
Dim imageHeights As System.Collections.Generic.List(Of Integer) = New System.Collections.Generic.List(Of Integer)()
Dim nIndex As Integer = 0
Dim width As Integer = 0
For Each file As System.IO.FileInfo In files
Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(file.FullName)
imageHeights.Add(img.Height)
width += img.Width
img.Dispose()
Next
imageHeights.Sort()
Dim height As Integer = imageHeights(imageHeights.Count - 1)
Dim bitmap As System.Drawing.Bitmap = New System.Drawing.Bitmap(width, height)
Dim graphic As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bitmap)
graphic.Clear(System.Drawing.SystemColors.AppWorkspace)
For Each file As System.IO.FileInfo In files
Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(file.FullName)
If nIndex = 0 Then
graphic.DrawImage(img, New System.Drawing.Point(0, 0))
nIndex += 1
width = img.Width
Else
graphic.DrawImage(img, New System.Drawing.Point(width, 0))
width += img.Width
End If
img.Dispose()
Next
graphic.Dispose()
bitmap.Save(destination, System.Drawing.Imaging.ImageFormat.Jpeg)
bitmap.Dispose()
End Sub
Screenshot