有没有一个好的c#图片压缩优化算法?


QQ的上次图片后,好几Mb的照片,能压缩到几十k,这个怎么压缩的啊?没找到开源的代码?有知道的吗 ? 谢谢!

编码 c#

讨厌烦死人了 10 years, 4 months ago

//引用ICSharpCode.SharpZipLib.dll免费的

///<summary>
/// zip压缩解压类
/// </summary>
public class ZipHelper
{
public byte[] Zip(byte[] data)
{
return Zip(data, 0, data.Length);
}

public byte[] Unzip(byte[] data)
{
return Unzip(data, 0, data.Length);
}
/// <summary>
/// 压缩byte[]
/// </summary>
/// <param name="data"></param>
/// <param name="offset"></param>
/// <param name="size"></param>
/// <returns></returns>
public byte[] Zip(byte[] data, int offset, int size)
{
MemoryStream inStream = new MemoryStream(data, offset, size);
MemoryStream outStream = new MemoryStream();
BZip2.Compress(inStream, outStream, size);

byte[] result = outStream.ToArray();
inStream.Close();
outStream.Close();
return result;
}
/// <summary>
/// 解压byte[]
/// </summary>
/// <param name="data"></param>
/// <param name="offset"></param>
/// <param name="size"></param>
/// <returns></returns>
public byte[] Unzip(byte[] data, int offset, int size)
{
MemoryStream inStream = new MemoryStream(data, offset, size);
MemoryStream outStream = new MemoryStream();
BZip2.Decompress(inStream, outStream);

byte[] result = outStream.ToArray();
inStream.Close();
outStream.Close();
return result;
}
/// <summary>
/// 压缩byte数组
/// </summary>
/// <param name="inBytes">需要压缩的byte数组</param>
/// <returns>压缩好的byte数组</returns>
public byte[] CompressByte(byte[] inBytes)
{
MemoryStream outStream = new MemoryStream();
Stream zipStream = new GZipOutputStream(outStream);
zipStream.Write(inBytes, 0, inBytes.Length);
zipStream.Close();
byte[] outData = outStream.ToArray();
outStream.Close();
return outData;
}

/// <summary>
/// 解压缩byte数组
/// </summary>
/// <param name="inBytes">需要解压缩的byte数组</param>
/// <returns></returns>
public byte[] DecompressByte(byte[] inBytes)
{
byte[] writeData = new byte[2048];
MemoryStream inStream = new MemoryStream(inBytes);
Stream zipStream = new GZipInputStream(inStream) as Stream;
MemoryStream outStream = new MemoryStream();
while (true)
{
int size = zipStream.Read(writeData, 0, writeData.Length);
if (size > 0)
{
outStream.Write(writeData, 0, size);
}
else
{
break;
}
}
inStream.Close();
byte[] outData = outStream.ToArray();
outStream.Close();
return outData;
}

}

无意识电波恋 answered 10 years, 4 months ago

Your Answer