ComponentOne Zip for .NET
Zip for .NET Task-Based Help / Retrieving Images from a Zip File
In This Topic
    Retrieving Images from a Zip File
    In This Topic

    To retrieve images directly from a zip file, first add the following code to the Form_Load event to compress several image files into a zip file:

    To write code in Visual Basic

    Visual Basic
    Copy Code
     ' Build a list of images in resource directory, and add them all to a zip file.
    Dim zip As New C1ZipFile()
    Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
         ' Get the application directory.
        Dim s As String = Application.ExecutablePath
        s = s.Substring(0, s.IndexOf("\bin")) + "\resources"
         ' Create the zip file.
        zip.Create((s + "\images.zip"))
         ' Populate the zip file and list.
        Dim f As String
        For Each f In  Directory.GetFiles(s) 
            Dim fname As String = f.ToLower()
             ' Skip self.
            If fname.EndsWith("zip") Then
                GoTo ContinueForEach1
            End If 
             ' Add to the list.
            ListBox1.Items.Add(Path.GetFileName(fname))
             ' Add to the zip file.
            zip.Entries.Add(fname)
            ContinueForEach1:
        Next f
    End Sub
    

    To write code in C#

    C#
    Copy Code
     // Build a list of images in resource directory, and add them all to a zip file.
    C1ZipFile zip = new C1ZipFile();
    private void Form1_Load(object sender, System.EventArgs e)
    {
     
         // Get the application directory.
        string s = Application.ExecutablePath;
        s = s.Substring(0, s.IndexOf(@"\bin")) + @"\resources";
     
         // Create the zip file.
        zip.Create(s + @"\images.zip");
     
         // Populate the zip file and list.
        foreach (string f in Directory.GetFiles(s))
        {
            string fname = f.ToLower();
     
             // Skip self.
            if (fname.EndsWith("zip")) continue;
     
             // Add to the list.
            listBox1.Items.Add(Path.GetFileName(fname));
     
             // Add to the zip file.
            zip.Entries.Add(fname);
        }
    }
    

    To allow you to select an image, retrieve a stream with the image data (OpenReader method), and load the image (Image.FromStream method), add the following code to the SelectedIndexChanged event:

    To write code in Visual Basic

    Visual Basic
    Copy Code
     ' Show selected image.
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
         ' Get the selected item.
        Dim item As String = CStr(listBox1.SelectedItem)
         ' Load the image directly from a compressed stream.
        Dim s As Stream = zip.Entries(item).OpenReader()
        Try
            pictureBox1.Image = CType(Image.FromStream(s), Image)
        Catch
        End Try
         ' Done with stream.
        s.Close()
    End Sub
    

    To write code in C#

    C#
    Copy Code
     // Show selected image.
    private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
         // Get the selected item.
        string item = (string)listBox1.SelectedItem;
         // Load the image directly from a compressed stream.
        Stream s = zip.Entries[item].OpenReader();
        try
        {
            pictureBox1.Image = (Image)Image.FromStream(s);
        }
        catch {}
         // Done with stream.
        s.Close();
    }
    

    This topic illustrates the following:

    This example shows several types of images, including ICO, GIF, TIFF, BMP, and JPG images.