title: Error decompressing gzipstream -- The magic number in GZip header is not correct
body: <p>I'm using the C# <code>System.IO</code> class (framework 4.0) to compress an image pulled off the file system with <code>FileDialog</code> and then inserted into a SQL Server database as a <code>varbinary(max)</code> data type.  The problem I'm having is when I pull the data out of the database and attempt to decompress I get the subject error with an additional message --  make sure you are passing in a gzip stream.</p>
  
  <p>The code to get the file:</p>
  
  <pre><code>    OpenFileDialog dlgOpen = new OpenFileDialog();
  
      if (dlgOpen.ShowDialog() == DialogResult.OK)
      {
          FileStream fs = File.OpenRead(dlgOpen.FileName);
          byte[] picbyte1 = new byte[fs.Length];
          byte[] picbyte = Compress(picbyte1);
          fs.Read(picbyte, 0, System.Convert.ToInt32(picbyte.Length));
          String ImageName = dlgOpen.FileName;
          //String bs64OfBytes = Convert.ToBase64String(picbyte);
          fs.Close();
          //additional code inserts into database
          ....
  
       }
  </code></pre>
  
  <p>The compress method:</p>
  
  <pre><code>        private static byte[] Compress(byte[] data)
          {
              var output = new MemoryStream();
              using (var gzip = new GZipStream(output, CompressionMode.Compress, true))
              {
                  gzip.Write(data, 0, data.Length);
                  gzip.Close();
              }
              return output.ToArray();
          }
  </code></pre>
  
  <p>The decompress method:</p>
  
  <pre><code>        private static byte[] Decompress(byte[] data)
          {
              var output = new MemoryStream();
              var input = new MemoryStream();
              input.Write(data, 0, data.Length);
              input.Position = 0; 
  
              using (var gzip = new GZipStream(input, CompressionMode.Decompress, true))
              {
                  var buff = new byte[64];//also used 32
                  var read = gzip.Read(buff, 0, buff.Length);//error occurs here
  
                  while (read &gt; 0)
                  {
                      output.Write(buff, 0, read);
                      read = gzip.Read(buff, 0, buff.Length);
                  }
  
                  gzip.Close();
              }
              return output.ToArray();
          }
  </code></pre>