using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
namespace Eyu.Common
...{
/**//// <summary>
/// 縮放處理
/// </summary>
public class Thumbnail
...{
private string workingDirectory = string.Empty; //圖片所在目錄
private string imgFileName = string.Empty; //圖片名稱
private ThumbnailMode mode = ThumbnailMode.ByWidth;
private int scaleValue = 0;
/**//// <summary>
/// 圖片所在目錄
/// </summary>
public string WorkingDirectory
...{
get
...{
return workingDirectory;
}
set
...{
workingDirectory = value;
}
}
/**//// <summary>
/// 圖片名稱
/// </summary>
public string ImageName
...{
get
...{
return imgFileName;
}
set
...{
imgFileName = value;
}
}
/**//// <summary>
/// 設置縮略模式
/// </summary>
public ThumbnailMode Mode
...{
get ...{ return mode; }
set ...{ mode = value; }
}
/**//// <summary>
/// 縮放尺寸
/// </summary>
public int ScaleValue
...{
get ...{ return scaleValue; }
set ...{ scaleValue = value; }
}
/**//// <summary>
/// 圖片縮略模式
/// </summary>
public enum ThumbnailMode
...{
ByPercent, //按百分比縮放
ByHeight, //按高度等比縮放
ByWidth, //按寬度等比縮放
}
/**//// <summary>
/// 按模式縮放圖片,輸出圖片名格式為:原圖文件名T.jpg
/// </summary>
public void MakeThumbnail()
...{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(WorkingDirectory + ImageName);
int towidth = 0;
int toheight = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (Mode)
...{
case ThumbnailMode.ByWidth://指定寬,高按比例
if (ow >= ScaleValue)
...{
toheight = originalImage.Height * ScaleValue / originalImage.Width;
towidth = ScaleValue;
}
else
...{
towidth = ow;
toheight = oh;
}
break;
case ThumbnailMode.ByHeight://指定高,寬按比例
if (oh >= ScaleValue)
...{
toheight = ScaleValue;
towidth = originalImage.Width * ScaleValue / originalImage.Height;
}
else
...{
towidth = ow;
toheight = oh;
}
break;
case ThumbnailMode.ByPercent://按比例縮放
towidth = ow / ScaleValue;
&nb toheight = oh / ScaleValue;
break;
default:
break;
}
//新建一個bmp圖片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一個畫板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//設置高質量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//設置高質量,低速度呈現平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空畫布並以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置並且按指定大小繪制原圖片的指定部分
g.DrawImage(originalImage, 0, 0, towidth, toheight);
g.Dispose();
originalImage.Dispose();
originalImage = (System.Drawing.Image)bitmap.Clone();
originalImage.Save(WorkingDirectory + ImageName + "T.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
}
}
}