Results 1 to 8 of 8

Thread: View zipped image directly

  1. #1
    Zexor's Avatar
    Zexor is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 4.5
    Join Date
    Nov 2008
    Posts
    426
    Reputation
    113

    View zipped image directly

    Is there a way to read image files inside a zip file without extracting it? Lets say i have 10 jpg in a zip file, can i load a specific image into a picturebox without first extracting it?

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,815
    Reputation
    1707
    It depends what you mean by "extract". A ZIP file contains bytes, like any other file. Those bytes are a transformed representation of other files; in your case, image files. Anything that knows how to display an image is not going to understand those transformed bytes, so you have no choice but to transform them back, i.e. decompress the data.

    If you mean "decompress" when you say "extract then no, there is no way to display that image without extracting it from the ZIP file. If you mean save all the original files to disk then yes, you can display the image without extracting.

    In order to display an image in a PictureBox in Windows Forms, you need an Image object. As you're using .NET 4.5, you have access to the ZIP functionality in the System.IO.Compression namespace. You can create a FileStream from your ZIP file and then create a ZipArchive from that. You can then get a ZipArchiveEntry for the appropriate entry and call its Open method to get a Stream. Finally, you can call Image.FromStream to get your Image object.

  3. #3
    Zexor's Avatar
    Zexor is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 4.5
    Join Date
    Nov 2008
    Posts
    426
    Reputation
    113
    Thanks that worked great. I have another question. How do i add an image file to an already existing zip file? is it something simular to this?

    Code:
                Using archive As IO.Compression.ZipArchive = IO.Compression.ZipFile.Open(zipFullFilename, IO.Compression.ZipArchiveMode.Update)
                    Dim zipEntry As IO.Compression.ZipArchiveEntry = archive.CreateEntry("abc.jpg")
                    Using writer As IO.StreamWriter = New IO.StreamWriter(zipEntry.Open())
    
                        ' Add an existing image file c:\xyz.jpg to the zipFullFilename
    
    
    
                    End Using
                End Using

  4. #4
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,815
    Reputation
    1707
    I've never done it myself so I don't know the details but I can tell you that you wouldn't use a StreamWriter for writing an Image because it's for text. Presumably that ZipEntry.Open call returns a Stream and the Image.Save method allows you to pass a Stream to write the saved Image to.

  5. #5
    Zexor's Avatar
    Zexor is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 4.5
    Join Date
    Nov 2008
    Posts
    426
    Reputation
    113
    I think CreateEntryFromFile is what i need to use, but after i typed archive. there is no CreateEntryFromFile. It is some zipFileExtensions class

    Code:
                Using archive As IO.Compression.ZipArchive = IO.Compression.ZipFile.Open(zipFullFilename, IO.Compression.ZipArchiveMode.Update)
                     archive.CreateEntryFromFile("c:\xyz.jpg", "xyz.jpg", IO.Compression.CompressionLevel.Fastest)
                End Using

  6. #6
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,815
    Reputation
    1707
    If the class is named ZipFileExtensions then that presumably contains extension methods on the ZipFile class, so you would need to call CreateEntryFromFile on a ZipFile rather then a ZipArchive.

  7. #7
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,090
    Reputation
    2804
    How to format posts with code blocks etc - present the problem/post properly

    Visual Studio Community 2017

  8. #8
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,815
    Reputation
    1707
    Quote Originally Posted by JohnH View Post
    I probably should have had a closer look myself. You always need to import the namespace to use any extension method.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •