C#圖片壓縮的實現方法

來源:互聯網
上載者:User

一般在web應用中,對用戶端提交上來的圖片肯定需要進行壓縮的。尤其是比較大的圖片,如果不經過壓縮會導致頁面變的很大,開啟速度比較慢,當然了如果是需要高品質的圖片也得需要生產縮圖。

下面貼出我自己琢磨的圖片壓縮演算法,首先這個是未經最佳化的簡單實現:

複製代碼 代碼如下:public static System.Drawing.Image GetImageThumb(System.Drawing.Image sourceImg, int width, int height)
{
System.Drawing.Image targetImg = new System.Drawing.Bitmap(width, height);
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(targetImg))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.DrawImage(sourceImg, new System.Drawing.Rectangle(0, 0, width, height), new System.Drawing.Rectangle(0, 0, sourceImg.Width, sourceImg.Height), System.Drawing.GraphicsUnit.Pixel);
g.Dispose();
}
return targetImg;
}

這個方法比較簡單,用到的是高品質壓縮。經過這個方法壓縮後,200K的圖片只能壓縮到160k左右。經過改寫代碼實現了如下的方法:

複製代碼 代碼如下:public Bitmap GetImageThumb(Bitmap mg, Size newSize)
{
double ratio = 0d;
double myThumbWidth = 0d;
double myThumbHeight = 0d;
int x = 0;
int y = 0;

Bitmap bp;

if ((mg.Width / Convert.ToDouble(newSize.Width)) > (mg.Height /
Convert.ToDouble(newSize.Height)))
ratio = Convert.ToDouble(mg.Width) / Convert.ToDouble(newSize.Width);
else
ratio = Convert.ToDouble(mg.Height) / Convert.ToDouble(newSize.Height);
myThumbHeight = Math.Ceiling(mg.Height / ratio);
myThumbWidth = Math.Ceiling(mg.Width / ratio);

Size thumbSize = new Size((int)newSize.Width, (int)newSize.Height);
bp = new Bitmap(newSize.Width, newSize.Height);
x = (newSize.Width - thumbSize.Width) / 2;
y = (newSize.Height - thumbSize.Height);
System.Drawing.Graphics g = Graphics.FromImage(bp);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
Rectangle rect = new Rectangle(x, y, thumbSize.Width, thumbSize.Height);
g.DrawImage(mg, rect, 0, 0, mg.Width, mg.Height, GraphicsUnit.Pixel);

return bp;
}

這樣實現的壓縮使壓縮率大幅度上升。其實代碼並沒有變多少,最主要的是在儲存的時候要是用jpg格式,如果不指定格式,預設使用的是png格式。

下面這個是園友寫的根據設定圖片品質數值來壓縮圖片的方法:

複製代碼 代碼如下:public static bool GetPicThumbnail(string sFile, string outPath, int flag)
{
System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);
ImageFormat tFormat = iSource.RawFormat;

//以下代碼為儲存圖片時,設定壓縮品質
EncoderParameters ep = new EncoderParameters();
long[] qy = new long[1];
qy[0] = flag;//設定壓縮的比例1-100
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;
try
{
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICIinfo = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICIinfo = arrayICI[x];
break;
}
}
if (jpegICIinfo != null)
{
iSource.Save(outPath, jpegICIinfo, ep);//dFile是壓縮後的新路徑
}
else
{
iSource.Save(outPath, tFormat);
}
return true;
}
catch
{
return false;
}
finally
{
iSource.Dispose();
iSource.Dispose();
}
}

轉載來源:http://www.cnblogs.com/lifeil/archive/2013/02/25/2931683.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.