即時通訊使用SLsharpziplip在將網絡發送到服務器之前試圖壓縮一個字節[]。 byte []包含jpeg編碼器已經壓縮的jpeg數據。在Silverlight中壓縮內存流
你可能會問,如果jpeg已經壓縮了圖像,爲什麼我需要更多地壓縮它,好吧,因爲我試過了,它工作。
這裏是發生了什麼事:
我寫字節的字節[]到一個txt文件,txt文件的大小是〜5K,我使用WinZip壓縮它,結果文件爲〜2K,因此文件大小減少了大約50%。但是,當我嘗試使用byte []並使用slsharziplip壓縮字節[]時,它的大小減小很小。
這裏是我使用的代碼:
MemoryStream msCompressed = new MemoryStream();
GZipOutputStream gzCompressed = new GZipOutputStream(msCompressed);
gzCompressed.SetLevel(9);
// allframes is a byte array.
gzCompressed.Write(allframes, 0, allframes.Length);
gzCompressed.Finish();
gzCompressed.IsStreamOwner = false;
gzCompressed.Close();
// i used byte[] compresseddata = msCompressed.ToArray() but i thought i'll try this too.
msCompressed.Seek(0, SeekOrigin.Begin);
byte[] compresseddata = new byte[msCompressed.Length];
msCompressed.Read(compresseddata, 0, compresseddata.Length);
================================ ==================================================
從調試代碼,我可以看到allframes.Length和compresseddata.lenght之間的大小差異是最小的。但如果將相同的數據寫入文本文件並使用winzip進行壓縮,則其大小會減少50%。
我這是怎麼了相同的數據寫入到一個txt文件:
TextWriter tw = new StreamWriter(MainPage.fs); // fs is a filestream.
foreach (byte b in allframes)
{
tw.Write(b);
}
========================== ================================================== ===
我做錯了什麼?我誤解了一些東西!
感謝前面:)