Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

how to download image directly from database into zip file in C#.net

.net with C#

user-image
Question added by Praveen Kumar Yadav , Technical Lead , L&T INFOTECH LTD
Date Posted: 2013/06/05
Hamid Anjum
by Hamid Anjum , Team Lead \ Sr. Software Engineer , University of Business and Technology, Jeddah

 

 

 

using (ZipFile zipFile = new ZipFile())

{

    foreach (var userPicture in userPictures)

    {

        string pictureName = userPicture.Name + ".png";

        using (MemoryStream tempstream = new MemoryStream())

        {

            Image userImage = //method that returns Drawing.Image from byte[];   

            userImage.Save(tempstream, ImageFormat.Png);  

            tempstream.Seek(0, SeekOrigin.Begin);

            byte[] imageData = new byte[tempstream.Length];

            tempstream.Read(imageData,0, imageData.Length);

            zipFile.AddEntry(pictureName, imageData);

        }

    }

 

    zipFile.Save(Response.OutputStream);

}

Ashraf Sabry
by Ashraf Sabry , Freelancer developer , N/A

If you are running on 4.5, you may use the ZipArchive class ().
First, set your response type to "application/zip" then create a ZipArchive object that writes to the response stream (putStream).
Extract your image as binary (byte[]) from the database (or from the file system if you store images as files) and write it to a ZipArchiveEntry object created from the archive () I suggest that you use a generic handler (ashx).
The examples in the documentation are helpful.
Unfortunately, I don't have .Net4.5 installed on my machine so, I couldn't write a sample for you.

Eslam Gamal
by Eslam Gamal , MIS Executive - Credit Risk , QIB

   public static Image byteArrayToImage(byte[] byteArrayIn)          {            var ms = new MemoryStream(byteArrayIn);                   var returnImage = Image.FromStream(ms);              return returnImage;          } The above Method Will Convert Byte Arrays To Images   For the Zip Part  http://dotnetzip.codeplex.com/ you can use this Library with .NET2.0 or higher

Asif ahmed Syed
by Asif ahmed Syed , Lead SharePoint Application Developer , International Center for Sports Security

string str = String.Format("{0:MM-DD-YY}", System.DateTime.Now); ZipOutputStream Zip_Output_Stream = new ZipOutputStream(File.Create (Server.MapPath("./") + strNow + ".zip"));Zip_Output_Stream.SetLevel(9);foreach (DataRow dr in ds.Tables["FILES"].Rows) { ZipEntry zip_Entry = new ZipEntry(dr["FILE_NAME"].ToString()); Zip_Output_Stream.PutNextEntry(zip_Entry); Zip_Output_Stream.Write((byte[])dr["FILE_DATA"], 0, System.Convert.ToInt32(dr["FILE_SIZE"])); }Zip_Output_Stream.Finish(); Zip_Output_Stream.Close();

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.