Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 72425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:02:34+00:00 2026-05-10T20:02:34+00:00

I was in need of a way to compress images in .net so i

  • 0

I was in need of a way to compress images in .net so i looked into using the .net GZipStream class (or DeflateStream). However i found that decompression was not always successful, sometimes the images would decompress fine and other times i would get a GDI+ error that something is corrupted.

After investigating the issue i found that the decompression was not giving back all the bytes it compressed. So if i compressed 2257974 bytes i would sometimes get back only 2257870 bytes (real numbers).

The most funny thing is that sometimes it would work. So i created this little test method that compresses only 10 bytes and now i don’t get back anything at all.

I tried it with both compression classes GZipStream and DeflateStream and i double checked my code for possible errors. I even tried positioning the stream to 0 and flushing all the streams but with no luck.

Here is my code:

    public static void TestCompression()     {         byte[] test = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };          byte[] result = Decompress(Compress(test));          // This will fail, result.Length is 0         Debug.Assert(result.Length == test.Length);     }      public static byte[] Compress(byte[] data)     {         var compressedStream = new MemoryStream();         var zipStream = new GZipStream(compressedStream, CompressionMode.Compress);         zipStream.Write(data, 0, data.Length);         return compressedStream.ToArray();     }      public static byte[] Decompress(byte[] data)     {         var compressedStream = new MemoryStream(data);         var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress);         var resultStream = new MemoryStream();          var buffer = new byte[4096];         int read;          while ((read = zipStream.Read(buffer, 0, buffer.Length)) > 0) {             resultStream.Write(buffer, 0, read);         }          return resultStream.ToArray();     } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:02:34+00:00Added an answer on May 10, 2026 at 8:02 pm

    You need to Close() the ZipStream after adding all the data you want to compress; it retains a buffer of unwritten bytes internally (even if you Flush()) that needs to be written.

    More generally, Stream is IDisposable, so you should also be using each… (yes, I know that MemoryStream isn’t going to lose any data, but if you don’t get into this habit, it will bite you with other Streams).

    public static byte[] Compress(byte[] data) {     using (var compressedStream = new MemoryStream())     using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress))     {         zipStream.Write(data, 0, data.Length);         zipStream.Close();         return compressedStream.ToArray();     } }  public static byte[] Decompress(byte[] data) {     using(var compressedStream = new MemoryStream(data))     using(var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))     using (var resultStream = new MemoryStream())     { ... } } 

    [edit : updated re comment] Re not using things like MemoryStream – this is always a fun one, with lots of votes on either side of the fence: but ultimatey…

    (rhetorical – we all know the answer…) How is MemoryStream implemented? is it a byte[] (owned by .NET)? is it a memory-mapped file (owned by the OS)?

    The reason you aren’t using it is because you are letting knowledge of internal implementation details change how you code against a public API – i.e. you just broke the laws of encapsulation. The public API says: I am IDisposable; you ‘own’ me; therefore, it is your job to Dispose() me when you are through.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 74k
  • Answers 74k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Django actually has 3 concepts here: Project (I think this… May 11, 2026 at 2:23 pm
  • added an answer Edit->Advanced->Show Whitespace. May 11, 2026 at 2:23 pm
  • added an answer I've used QualityCenter/TestDirectory for a long time. I'm now using… May 11, 2026 at 2:23 pm

Related Questions

I'm trying to store a lightly filtered copy of a database for offline reference,
I have recently been given a task to add the ability to interact with
I have a priority_queue of some object: typedef priority_queue<Object> Queue; Queue queue; From time
( Updated a little ) I'm not very experienced with internationalization using PHP, it

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.