分為兩部分,一個是操作類,另外一個是測試。直接上代碼了
一、測試代碼
private void button1_Click(object sender, EventArgs e)
{
string newSourcePath = ImgPath;//源圖存放目錄
string newNewDir = MakePath; //新圖存放目錄
string sourceFile = Path.Combine(ImgPath, "app1.jpg"); //獲取原圖路徑
string newFile = string.Empty; //新圖路徑
ImgThumbnail iz = new ImgThumbnail();
Action<ImgThumbnail.ImgThumbnailType> Thumbnail = (type =>
{
//生成質量
for (int i = 100; i >= 10; i -= 10)
{
//計算新圖路徑,測試命名:原圖名_new_縮放類型_壓縮質量.jpg
newFile = Path.Combine(newNewDir, string.Format("app1_new_{1}_{0}.jpg", i, type.ToString()));
//壓縮圖片
iz.Thumbnail(sourceFile, newFile, 100, 100, i, type);
}
});
Thumbnail(ImgThumbnail.ImgThumbnailType.Cut);//裁剪壓縮(裁剪壓縮還不好用,待調整!)
Thumbnail(ImgThumbnail.ImgThumbnailType.H); //以高度為參照壓縮,不變形
Thumbnail(ImgThumbnail.ImgThumbnailType.W); //以寬度為參照壓縮,不變形
Thumbnail(ImgThumbnail.ImgThumbnailType.WH); //固定寬高壓縮,變形
MessageBox.Show("完成");
}
二、操作類
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace MagickNet.App
{
/// <summary>
/// 圖片壓縮
/// </summary>
public class ImgThumbnail
{
/// <summary>
/// 指定縮放類型
/// </summary>
public enum ImgThumbnailType
{
/// <summary>
/// 指定高寬縮放(可能變形)
/// </summary>
WH = 0,
/// <summary>
/// 指定寬,高按比例
/// </summary>
W = 1,
/// <summary>
/// 指定高,寬按比例
/// </summary>
H = 2,
/// <summary>
/// 指定高寬裁減(不變形)
/// </summary>
Cut = 3
}
#region Thumbnail
/// <summary>
/// 無損壓縮圖片
/// </summary>
/// <param name="sFile">原圖片</param>
/// <param name="dFile">壓縮後保存位置</param>
/// <param name="height">高度</param>
/// <param name="width"></param>
/// <param name="flag">壓縮質量 1-100</param>
/// <param name="type">壓縮縮放類型</param>
/// <returns></returns>
public bool Thumbnail(string sFile, string dFile, int height, int width, int flag, ImgThumbnailType type)
{
System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);
ImageFormat tFormat = iSource.RawFormat;
//縮放後的寬度和高度
int towidth = width;
int toheight = height;
//
int x = 0;
int y = 0;
int ow = iSource.Width;
int oh = iSource.Height;
switch (type)
{
case ImgThumbnailType.WH://指定高寬縮放(可能變形)
{
break;
}
case ImgThumbnailType.W://指定寬,高按比例
{
toheight = iSource.Height * width / iSource.Width;
break;
}
case ImgThumbnailType.H://指定高,寬按比例
{
towidth = iSource.Width * height / iSource.Height;
break;
}
case ImgThumbnailType.Cut://指定高寬裁減(不變形)
{
if ((double)iSource.Width / (double)iSource.Height > (double)towidth / (double)toheight)
{
oh = iSource.Height;
ow = iSource.Height * towidth / toheight;
y = 0;
x = (iSource.Width - ow) / 2;
}
else
{
ow = iSource.Width;
oh = iSource.Width * height / towidth;
x = 0;
y = (iSource.Height - oh) / 2;
}
break;
}
default:
break;
}
Bitmap ob = new Bitmap(towidth, toheight);
Graphics g = Graphics.FromImage(ob);
g.Clear(System.Drawing.Color.WhiteSmoke);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(iSource
, new Rectangle(x, y, towidth, toheight)
, new Rectangle(0,0, iSource.Width, iSource.Height)
, GraphicsUnit.Pixel);
g.Dispose();
//以下代碼為保存圖片時,設置壓縮質量
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 i = 0; i < arrayICI.Length; i++)
{
if (arrayICI[i].FormatDescription.Equals("JPEG"))
{
jpegICIinfo = arrayICI[i];
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();
}
}
#endregion
}
}
作者:xxj_jing