Home > General > Extract picture in zip file and display in a picture box

Extract picture in zip file and display in a picture box

March 13th, 2009

We will be using SharpZipLib which can be downloaded from

http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx

Click on the Download for Assemblies for .NET 1.1, .NET 2.0, .NET CF 1.0, .NET CF 2.0

Once you’ve downloaded the ZIP file, extract it and load the ICSharpCode.SharpZipLib.dll depends on the version of .NET you are using by going to Project > Add Reference > Browse.

The following code load the image Sunset.jpg from D:Sunset.zip and display in PictureBox1.

image

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        FileStream fileStream;
        ZipFile zipFile;
        ZipEntry zipEntry;
        Stream stream;
        Bitmap bitmap;

        private void Form1_Load(object sender, EventArgs e)
        {
            fileStream = new FileStream("D:\Sunset.zip", FileMode.Open, FileAccess.Read);
            zipFile = new ZipFile(fileStream);
            zipEntry = zipFile.GetEntry("Sunset.jpg");
            stream = zipFile.GetInputStream(zipEntry);
            bitmap = new Bitmap(stream);
            pictureBox1.Image = bitmap;
        }
    }
}

admin General , , ,

Comments are closed.