C#图片压缩算法

 1 /// <summary>
2 /// 壓縮圖片 /// </summary>
3 /// <param name="fileStream">圖片流</param>
4 /// <param name="quality">壓縮質量0-100之間 數值越大質量越高</param>
5 /// <returns></returns>
6 private byte[] CompressionImage(Stream fileStream, long quality) {
7 using (System.Drawing.Image img = System.Drawing.Image.FromStream(fileStream)) {
8 using (Bitmap bitmap = new Bitmap(img)) {
9 ImageCodecInfo CodecInfo = GetEncoder(img.RawFormat);
10 System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
11 EncoderParameters myEncoderParameters = new EncoderParameters(1);
12 EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality);
13 myEncoderParameters.Param[0] = myEncoderParameter;
14 using (MemoryStream ms = new MemoryStream()) {
15 bitmap.Save(ms, CodecInfo, myEncoderParameters);
16 myEncoderParameters.Dispose();
17 myEncoderParameter.Dispose();
18 return ms.ToArray();
19 }
20 }
21 }
22 }
23 調用:CompressionImage(stream,0L)//最低質量 1M jpg圖大概能壓成180k左右
posted @ 2012-01-09 12:43  lanmiao  阅读(2302)  评论(0编辑  收藏  举报