Zip for WPF and Silverlight
C1Zip Task-Based Help / Retrieving Images from a Zip File
In This Topic
    Retrieving Images from a Zip File
    In This Topic

    In this example, two buttons and a listbox will be used to show how images can be retrieved from a .zip file.

    To retrieve images directly from a zip file, first add the following code to compress several image files into a zip file. In this example, the code is added to the btnNew_Click event, which creates a new .zip file for the images when a button is clicked.

    Make sure to add these using statements at the top of the code:

    C#
    Copy Code
    using C1.C1Zip;
    using Microsoft.Win32;
    using System.IO;
    

     

    C#
    Copy Code
    public MainWindow()
                   {
                       InitializeComponent();    
                       // Get the application directory.  
                       string s = Environment.CurrentDirectory;  
                       s = s.Substring(0, s.IndexOf(@"\bin")) + @"\resources";   
                       // Create the zip file.  
                       zipFile.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(System.IO.Path.GetFileName(fname));     
                           // Add to the zip file.      
                           zipFile.Entries.Add(fname);
                       }
                   }
    

    To allow you to select an image, retrieve a stream with the image data (OpenReader method), and add the following code to the listBox1_SelectionChanged and StreamCopy events:

    C#
    Copy Code
    private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
                   {
                       // Get the selected item.
                       string item = (string)listBox1.SelectedItem;
                       // Load the image directly from a compressed stream.
                       var stream = zipFile.Entries[item].OpenReader();     
                       BitmapImage img = new BitmapImage();
                       img.BeginInit();
                       img.StreamSource = stream;
                       img.EndInit();
                       this.image1.Source = img;
                   }
    

    This topic illustrates the following:

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