I am showing images in "img" control and image name in "Label" control. My requirement is that if downloads the image then image name should be displayed on image. I am using the following code to write on the image and download. It is working fine but when i see the downloaded image. The image is corrupt. If i download the image without writing then the image is o.k. Please suggest me that what i should make changes in my following coding that the image will not corrupt after writing. The code is given below:
protected void Download_Click(object sender, ImageClickEventArgs e)
{
string imgPath = hdnId.Value;
string[] a = imgPath.Split(new string[] { "demoProject/" }, StringSplitOptions.None);
string ik = a[1].Replace("%20", " ");
string[] a1 = imgPath.Split(new string[] { "Gallery/" }, StringSplitOptions.None);
string sh = a1[1];
FileInfo file = new FileInfo(Server.MapPath(ik));
if (file.Exists)
{
// Coding for write Text on the Image
Bitmap bitMapImage = new System.Drawing.Bitmap(Server.MapPath(ik));
Graphics graphicImage = Graphics.FromImage(bitMapImage);
graphicImage.SmoothingMode = SmoothingMode.AntiAlias;
SolidBrush brush = new SolidBrush(Color.Red);
graphicImage.DrawString(lblText.Text, new Font("Arial", 17, FontStyle.Bold), brush, new Point(100, 250));
Response.ContentType = "image/jpeg";
bitMapImage.Save(Response.OutputStream, ImageFormat.Jpeg);
bitMapImage.Save(Server.MapPath("images/PGImages/") + sh);
graphicImage.Dispose();
bitMapImage.Dispose();
//This is the image where we are going to write the text on it.
string imgName = "images/PGImages/" + sh;
// Coding for download the image.
Response.Clear();
Response.ContentType = (Path.GetExtension(imgName));
Response.AddHeader("Content-Disposition",string.Format("attachment; filename = {0}",Path.GetFileName(imgName)));
Response.AddHeader("Content-Length", file.Length.ToString("F0"));
Response.TransmitFile(imgName);
Response.End();
}
else
{
Response.Write("This file does not exist.");
}
}