C#實(shí)現(xiàn)圖片壓縮方法
來源:易賢網(wǎng) 閱讀:5726 次 日期:2014-08-29 10:43:33
溫馨提示:易賢網(wǎng)小編為您整理了“C#實(shí)現(xiàn)圖片壓縮方法”,方便廣大網(wǎng)友查閱!

這個(gè)是未經(jīng)優(yōu)化的簡單實(shí)現(xiàn)

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;

}

這個(gè)方法比較簡單,用到的是高質(zhì)量壓縮。經(jīng)過這個(gè)方法壓縮后,200K的圖片只能壓縮到160k左右。

經(jīng)過改寫代碼實(shí)現(xiàn)了如下的方法

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;

}

這樣實(shí)現(xiàn)的壓縮使壓縮率大幅度上升。其實(shí)代碼并沒有變多少,最主要的是在保存的時(shí)候要是用jpg格式,

如果不指定格式,默認(rèn)使用的是png格式。

下面這個(gè)是園友寫的根據(jù)設(shè)置圖片質(zhì)量數(shù)值來壓縮圖片的方法:

public static bool GetPicThumbnail(string sFile, string outPath, int flag)

{

System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);

ImageFormat tFormat = iSource.RawFormat;

//以下代碼為保存圖片時(shí),設(shè)置壓縮質(zhì)量

EncoderParameters ep = new EncoderParameters();

long[] qy = new long[1];

qy[0] = flag;//設(shè)置壓縮的比例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();

}

}

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:C#實(shí)現(xiàn)圖片壓縮方法
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)