.Net下压缩图片文件,该如何做呢?请问各位大虾。

fengyunhongri 2009-04-03 11:26:31
DeflateStream 提供用于使用 Deflate 算法压缩和解压缩流的方法和属性。
GZipStream 提供用于压缩和解压缩流的方法和属性。
为何用它们压缩,越压缩越大呢?请高手支招。
...全文
251 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
fengyunhongri 2009-04-03
  • 打赏
  • 举报
回复
谢谢cppfaq的支持!
wuyq11 2009-04-03
  • 打赏
  • 举报
回复
cppfaq 2009-04-03
  • 打赏
  • 举报
回复
果然越压越大

有人说
GZip对文本压缩比较好,
对视频,MP3,JPG图片这些已经经过压缩的文件再次压缩可能会越压越大
对可执行文件 exe , dll这些,压缩比也是很小的

楼主试一下SharpZipLib
http://www.icsharpcode.net/OpenSource/SharpZipLib/
fengyunhongri 2009-04-03
  • 打赏
  • 举报
回复
CSDN上的实例。
fengyunhongri 2009-04-03
  • 打赏
  • 举报
回复
public static void GZipCompressDecompress(string filename)
{
Console.WriteLine("Test compression and decompression on file {0}", filename);
FileStream infile;
try
{
// Open the file as a FileStream object.
infile = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] buffer = new byte[infile.Length];
// Read the file to ensure it is readable.
int count = infile.Read(buffer, 0, buffer.Length);
if ( count != buffer.Length)
{
infile.Close();
Console.WriteLine("Test Failed: Unable to read data from file");
return;
}
infile.Close();
MemoryStream ms = new MemoryStream();
// Use the newly created memory stream for the compressed data.
GZipStream compressedzipStream = new GZipStream(ms , CompressionMode.Compress, true);
Console.WriteLine("Compression");
compressedzipStream.Write(buffer, 0, buffer.Length);
// Close the stream.
compressedzipStream.Close();
Console.WriteLine("Original size: {0}, Compressed size: {1}", buffer.Length, ms.Length);

// Reset the memory stream position to begin decompression.
ms.Position = 0;
GZipStream zipStream = new GZipStream(ms, CompressionMode.Decompress);
Console.WriteLine("Decompression");
byte[] decompressedBuffer = new byte[buffer.Length + 100];
// Use the ReadAllBytesFromStream to read the stream.
int totalCount = GZipTest.ReadAllBytesFromStream(zipStream, decompressedBuffer);
Console.WriteLine("Decompressed {0} bytes", totalCount);

if( !GZipTest.CompareData(buffer, buffer.Length, decompressedBuffer, totalCount) )
{
Console.WriteLine("Error. The two buffers did not compare.");
}
zipStream.Close();
} // end try
catch (InvalidDataException)
{
Console.WriteLine("Error: The file being read contains invalid data.");
}
catch (FileNotFoundException)
{
Console.WriteLine("Error:The file specified was not found.");
}
catch (ArgumentException)
{
Console.WriteLine("Error: path is a zero-length string, contains only white space, or contains one or more invalid characters");
}
catch (PathTooLongException)
{
Console.WriteLine("Error: The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.");
}
catch (DirectoryNotFoundException)
{
Console.WriteLine("Error: The specified path is invalid, such as being on an unmapped drive.");
}
catch (IOException)
{
Console.WriteLine("Error: An I/O error occurred while opening the file.");
}
catch (UnauthorizedAccessException)
{
Console.WriteLine("Error: path specified a file that is read-only, the path is a directory, or caller does not have the required permissions.");
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("Error: You must provide parameters for MyGZIP.");
}
}
cppfaq 2009-04-03
  • 打赏
  • 举报
回复
PS
你怎么压的?贴出来看看
cppfaq 2009-04-03
  • 打赏
  • 举报
回复
C# 批量压缩图片(转载以收藏)
http://adxlong.blog.163.com/blog/static/22830932009263341526/
homejiji 2009-04-03
  • 打赏
  • 举报
回复
学习

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧