C#图片处理之: 另存为压缩质量可自己控制的JPEG .
2012-01-17 14:01
176 查看
处理图片时常用的过程是:读入图片文件并转化为Bitmap -> 处理此Bitmap的每个点以得到需要的效果 -> 保存新的Bitmap到文件
使用C#很方便的就可以把多种格式的图片文件读到Bitmap对象中。一句话就够了,常见的格式都支持,诸如JPEG,BMP,PNG等等。
Bitmap bmp = new Bitmap("文件名");
然后就是怎么处理这个图片的问题了,与本案无关,pass。
最后就是保存。JPEG虽然是有损压缩方案,但是它在缩减文件体积和尽可能好的保留原有信息的矛盾上很好的找到了平衡点,所以在很多情况下成为首选的保存方案。
C#当然不会无视这一点,Bitmap类提供了默认的另存为JPEG的方法:
bmp.Save("输出文件", System.Drawing.Imaging.ImageFormat.Jpeg);
这样当然很方便,但有时候更在乎文件体积而有时候更在乎图像质量,是不是有什么办法可以让自己来控制压缩质量呢?
答案是肯定的,bmp.Save方法中有个重载用到了EncoderParameters参数。我们可以在这个参数中加入自己的控制质量。
private static ImageCodecInfo GetCodecInfo(string mimeType)
public static bool KiSaveAsJPEG(Bitmap bmp, string FileName, int Qty)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EncoderParameter p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EncoderParameters ps;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ps = new EncoderParameters(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Qty);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ps.Param[0] = p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bmp.Save(FileName, GetCodecInfo("image/jpeg"), ps);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catch
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
使用C#很方便的就可以把多种格式的图片文件读到Bitmap对象中。一句话就够了,常见的格式都支持,诸如JPEG,BMP,PNG等等。
Bitmap bmp = new Bitmap("文件名");
然后就是怎么处理这个图片的问题了,与本案无关,pass。
最后就是保存。JPEG虽然是有损压缩方案,但是它在缩减文件体积和尽可能好的保留原有信息的矛盾上很好的找到了平衡点,所以在很多情况下成为首选的保存方案。
C#当然不会无视这一点,Bitmap类提供了默认的另存为JPEG的方法:
bmp.Save("输出文件", System.Drawing.Imaging.ImageFormat.Jpeg);
这样当然很方便,但有时候更在乎文件体积而有时候更在乎图像质量,是不是有什么办法可以让自己来控制压缩质量呢?
答案是肯定的,bmp.Save方法中有个重载用到了EncoderParameters参数。我们可以在这个参数中加入自己的控制质量。
private static ImageCodecInfo GetCodecInfo(string mimeType)
public static bool KiSaveAsJPEG(Bitmap bmp, string FileName, int Qty)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EncoderParameter p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EncoderParameters ps;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ps = new EncoderParameters(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Qty);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ps.Param[0] = p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bmp.Save(FileName, GetCodecInfo("image/jpeg"), ps);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catch
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
相关文章推荐
- C#图片处理之: 另存为压缩质量可自己控制的JPEG
- C#图片处理之: 另存为压缩质量可自己控制的JPEG
- C#图片处理之: 另存为压缩质量可自己控制的JPEG
- C#图片处理之: 另存为压缩质量可自己控制的JPEG
- C#图片处理之: 另存为压缩质量可自己控制的JPEG
- C#图片处理之另存为压缩质量可自己控制的JPEG
- C#图片处理之:另存为压缩质量可自己控制的JPEG
- C#图片处理之: 另存为压缩质量可自己控制的JPEG
- 如何获取JPEG图片质量和预测压缩图片大小
- Android图片质量压缩和尺寸压缩处理
- 图片处理之-Bitmap.Config,jpeg压缩与大小
- android 图片过大如何处理?压缩后图片有多大? 分辨率压缩、质量压缩
- 自己封装的一个Java版图片工具,具备压缩,伸缩变换,透明处理,格式转换等功能.
- 图片质量压缩 与 图片格式 JPEG PNG
- 调用摄像头和相册,从相册中选择(含有google发布的图片压缩以及自己的图片截取)
- Java图片上传压缩处理
- android 中图片的压缩处理
- bitmap compress 图片压缩 图片文件大小处理
- IOS 图片上传处理 图片压缩 图片处理
- C#压缩图片质量或长宽