C#無損壓縮圖片
話不多說,請看代碼:
/// <summary>
/// 根據(jù)指定尺寸得到按比例縮放的尺寸,返回true表示以更改尺寸
/// </summary>
/// <param name="picWidth">圖片寬度</param>
/// <param name="picHeight">圖片高度</param>
/// <param name="specifiedWidth">指定寬度</param>
/// /// <param name="specifiedHeight">指定高度</param>
/// <returns>返回true表示以更改尺寸</returns>
private bool GetPicZoomSize(ref int picWidth, ref int picHeight, int specifiedWidth, int specifiedHeight)
{
int sW = 0, sH = 0;
Boolean isZoomSize = false;
//按比例縮放
Size tem_size = new Size(picWidth, picHeight);
if (tem_size.Width > specifiedWidth || tem_size.Height > specifiedHeight) //將**改成c#中的或者操作符號
{
if ((tem_size.Width * specifiedHeight) > (tem_size.Height * specifiedWidth))
{
sW = specifiedWidth;
sH = (specifiedWidth * tem_size.Height) / tem_size.Width;
}
else
{
sH = specifiedHeight;
sW = (tem_size.Width * specifiedHeight) / tem_size.Height;
}
isZoomSize = true;
}
else
{
sW = tem_size.Width;
sH = tem_size.Height;
}
picHeight = sH;
picWidth = sW;
return isZoomSize;
}
/// <summary>
/// 無損壓縮圖片
/// </summary>
/// <param name="sFile">原圖片</param>
/// <param name="dFile">壓縮后保存位置</param>
/// <param name="dHeight">高度</param>
/// <param name="dWidth">寬度</param>
/// <param name="flag">壓縮質(zhì)量 1-100</param>
/// <returns></returns>
public bool GetPicThumbnail(string sFile, string dFile, int dHeight, int dWidth, int flag)
{
System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);
ImageFormat tFormat = iSource.RawFormat;
int sW = iSource.Width, sH = iSource.Height;
GetPicZoomSize(ref sW, ref sH, dWidth, dHeight);
Bitmap ob = new Bitmap(dWidth, dHeight);
Graphics g = Graphics.FromImage(ob);
g.Clear(Color.WhiteSmoke);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
g.Dispose();
//以下代碼為保存圖片時(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)
{
ob.Save(dFile, jpegICIinfo, ep);//dFile是壓縮后的新路徑
}
else
{
ob.Save(dFile, tFormat);
}
return true;
}
catch
{
return false;
}
finally
{
iSource.Dispose();
ob.Dispose();
}
}
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持我們!
上一篇:用Newtonsoft將json串轉(zhuǎn)為對象的方法(詳解)
欄 目:C#教程
本文標(biāo)題:C#無損壓縮圖片
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5792.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)打開畫圖的同時(shí)載入圖片、最大化顯示畫圖窗體的方法
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10http圖片上傳安全性問題 根據(jù)ContentType (MIME) 判斷其實(shí)不準(zhǔn)確、不
- 01-10C#將圖片存放到SQL SERVER數(shù)據(jù)庫中的方法
- 01-10C#操作數(shù)據(jù)庫中存取圖片文件的方法
- 01-10C#圖片處理3種高級應(yīng)用
- 01-10c#實(shí)現(xiàn)識別圖片上的驗(yàn)證碼數(shù)字
- 01-10Silverlight將圖片轉(zhuǎn)換為byte的實(shí)現(xiàn)代碼
- 01-10C#簡易圖片格式轉(zhuǎn)換器實(shí)現(xiàn)方法
- 01-10C#給圖片添加水印完整實(shí)例


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什


