Search code examples
asp.netasp.net-mvcasp.net-mvc-3asp.net-mvc-4asp-classic

converting images in a folder to new format


I am trying to convert a group of images in a folder to animated gif and facing error

"Cannot access a closed Stream."

Also there must be error in display image as lable.

protected void Button1_Click(object sender, EventArgs e)
{
    //Variable declaration
    MemoryStream memoryStream;

    binaryWriter = new BinaryWriter(stream);

   int numFiles = fileEntries.Length;
    for (int picCount = 0; picCount < numFiles; picCount++)
    {
        image = System.Drawing.Image.FromFile(fileEntries[picCount]);

         image.Save(memoryStream, ImageFormat.Gif);
         buf1 = memoryStream.ToArray();

         memoryStream.SetLength(0);
    }
    binaryWriter.Close();
    System.Drawing.Image streamGIF = System.Drawing.Image.FromStream(stream);
   Label1.Text += "<img src=" + streamGIF + " /><br />";
}

Solution

  • You need to store the image first on drive and then give its path to image tag.

    System.Drawing.Image streamGIF = System.Drawing.Image.FromStream(stream);
    string imageName = "~/Images/test.gif";
    string savePath = Server.MapPath(@"Images\test.gif");       
    streamGIF.Save(savePath, System.Drawing.Imaging.ImageFormat.Gif);           
    binaryWriter.Close();
    img1.ImageUrl = imageName;