Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying to compress and decompress any type of image using C# coding.
Anybody have any idea about this plz help..
Thanks in advance.
Posted
Comments
Sergey Alexandrovich Kryukov 9-Mar-12 0:54am    
Do you want your own compression algorithm, your own implementation of the known algorithm or just the use of something existing in .NET? (In last case, just Google, see MSDN.)

What did you try so far? What is the problem?
--SA
Manisha Tambade 9-Mar-12 1:05am    
I am not implementing own algorithm.
i have tried from msdn GZipStream sample code but not working..

thanks

 
Share this answer
 
.NET provides GZipStream class for better compression and decompression. Here is the sample code:

C#
try
{
    string anyString = File.ReadAllText("MyImage.jpg");
    CompressStringToFile("new.gz", anyString);
}
catch
{
    // Couldn't compress.
}
}

public static void CompressStringToFile(string fileName, string value)
{
  string temp = Path.GetTempFileName();
  File.WriteAllText(temp, value);
  byte[] b;
  using (FileStream f = new FileStream(temp, FileMode.Open))
  {
    b = new byte[f.Length];
    f.Read(b, 0, (int)f.Length);
  }
  using (FileStream f2 = new FileStream(fileName, FileMode.Create))
  using (GZipStream gz = new GZipStream(f2, CompressionMode.Compress, false))
  {
    gz.Write(b, 0, b.Length);
  }
}
 
Share this answer
 
Comments
Member 12060589 9-Mar-17 1:40am    
@GanesanSenthilvel - Hi this is a good solution but can you tell the code for decompression as well??
 
Share this answer
 
Comments
Manisha Tambade 9-Mar-12 4:40am    
hi,Thanks for solution link..
In this link example,gZipStream is used and stream object is created.
here CopyTo method of stream object is used which non existing and giving error.
So i tried for any such a method like copy ,but its not there so cant run this sample..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900