据我所知,JPEG文件在所有图像格式中具有最佳的压缩比。如果我没记错的话,我们无法再对JPEG文件进行更多压缩,因为它已经具有最佳压缩率。请帮助我确认一下。以下是我创建的一些JPEG文件:
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach(ImageCodecInfo codec in codecs) {
if(codec.MimeType == "image/jpeg")
ici = codec;
}
EncoderParameters ep = new EncoderParameters();
ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, _quality);
using(MemoryStream ms = new MemoryStream()) {
Bitmap capture = GetImage();
capture.Save(ms, ici, ep);
}
我用SharpZipLib压缩了它们,每个JPEG的平均大小为130KB,在压缩后,每个文件的大小约为70KB。这是怎么做到的?我只能想到两个答案。
1.我们可以通过zip库将JPEG文件压缩得更加紧凑。
2.我的JPEG文件创建不正确,我们可以创建更好的JPEG(由于无法使用zip库进一步压缩它们,因此具有更高的压缩比)。
有人知道这方面的知识吗?如果我们可以创建更好的JPEG,请帮我解决这个问题。
编辑:这是我用来压缩JPEG的zip代码:
void addnewentry(MemoryStream stream, string pass,
string ZipFilePath, string entryname){
ICSharpCode.SharpZipLib.Zip.ZipFile zf = new ZipFile(ZipFilePath);
if(!String.IsNullOrEmpty(pass))
zf.Password = pass;
StaticDataSource sds = new StaticDataSource(Stream);
zf.BeginUpdate();
zf.Add(sds, entryName);
zf.CommitUpdate();
zf.IsStreamOwner = true;
zf.Close();
}
public class StaticDataSource : IStaticDataSource {
public Stream stream { get; set; }
public StaticDataSource() {
this.stream.Position = 0;
}
public StaticDataSource(Stream stream) {
this.stream = stream;
this.stream.Position = 0;
}
public Stream GetSource() {
this.stream.Position = 0;
return stream;
}
}