Reputation: 805
I'm trying to compress image files to send it via network , here is my test compress method
public void compress(MemoryStream inStream)
{
using (MemoryStream outStream = new MemoryStream())
{
using (DeflateStream deflate = new DeflateStream(outStream, CompressionMode.Compress, true))
{
Console.WriteLine(inStream.Length);
deflate.Write(inStream.GetBuffer(), 0, (int)inStream.Length);
deflate.Close();
Console.WriteLine(outStream.Length);
}
}
}
the result was
375531
354450
I thought when compressing at least I should get 5 digits instead of 6 , is this normal ? am I doing it right ?
Thanks in advance
Upvotes: 0
Views: 561
Reputation: 25200
You are probably attempting to compress a stream that cannot be compressed any further.
For instance, lossy image formats like JPEG typically have very little redundant data, so the ability to compress with DeflateStream
is minimal.
Note that DeflateStream
is intended to compress files on-the-fly while a stream is being read or written, and doesn't assume the whole stream is available during the compression process. Your example has the entire stream in memory, so you can use other "static" styles of compression.
For instance, you may find that you get better results with Zip compression: http://dotnetzip.codeplex.com is a well-regarded library for this.
Even so, for images that are already compressed, expect no better than ~15% compression.
Upvotes: 3