/// <summary>
/// 獲取一個圖片按等比例縮小後的大小。
/// </summary>
/// <param name="maxWidth">需要縮小到的寬度</param>
/// <param name="maxHeight">需要縮小到的高度</param>
/// <param name="imageOriginalWidth">圖片的原始寬度</param>
/// <param name="imageOriginalHeight">圖片的原始高度</param>
/// <returns>返回圖片按等比例縮小後的實際大小</returns>
public static Size GetNewSize(int maxWidth, int maxHeight, int imageOriginalWidth, int imageOriginalHeight)
{
double w = 0.0;
double h = 0.0;
double sw = Convert.ToDouble(imageOriginalWidth);
double sh = Convert.ToDouble(imageOriginalHeight);
double mw = Convert.ToDouble(maxWidth);
double mh = Convert.ToDouble(maxHeight);
if (sw < mw && sh < mh)
{
w = sw;
h = sh;
}
else if ((sw / sh) > (mw / mh))
{
w = maxWidth;
h = (w * sh) / sw;
}
else
{
h = maxHeight;
w = (h * sw) / sh;
}
return new Size(Convert.ToInt32(w), Convert.ToInt32(h));
}
/// <summary>
/// 對給定的一個圖片(Image對象)生成一個指定大小的縮略圖。
/// </summary>
/// <param name="originalImage">原始圖片</param>
/// <param name="thumMaxWidth">縮略圖的寬度</param>
/// <param name="thumMaxHeight">縮略圖的高度</param>
/// <returns>返回縮略圖的Image對象</returns>
public static System.Drawing.Image GetThumbNailImage(System.Drawing.Image originalImage, int thumMaxWidth, int thumMaxHeight)
{
Size thumRealSize = Size.Empty;
System.Drawing.Image newImage = originalImage;
Graphics graphics = null;
try
{
thumRealSize = GetNewSize(thumMaxWidth, thumMaxHeight, originalImage.Width, originalImage.Height);
newImage = new Bitmap(thumRealSize.Width, thumRealSize.Height);
graphics = Graphics.FromImage(newImage);
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.Clear(Color.Transparent);
graphics.DrawImage(originalImage, new Rectangle(0, 0, thumRealSize.Width, thumRealSize.Height), new Rectangle(0, 0, originalImage.Width, originalImage.Height), GraphicsUnit.Pixel);
}
catch { }
finally
{
if (graphics != null)
{
graphics.Dispose();
graphics = null;
}
}
return newImage;
}
/// <summary>
/// 對給定的一個圖片文件生成一個指定大小的縮略圖。
/// </summary>
/// <param name="originalImage">圖片的物理文件地址</param>
/// <param name="thumMaxWidth">縮略圖的寬度</param>
/// <param name="thumMaxHeight">縮略圖的高度</param>
/// <returns>返回縮略圖的Image對象</returns>
public static System.Drawing.Image GetThumbNailImage(string imageFile, int thumMaxWidth, int thumMaxHeight)
{
System.Drawing.Image originalImage = null;
System.Drawing.Image newImage = null;
try
{
originalImage = System.Drawing.Image.FromFile(imageFile);
newImage = GetThumbNailImage(originalImage, thumMaxWidth, thumMaxHeight);
}
catch { }
finally
{
if (originalImage != null)
{
originalImage.Dispose();
originalImage = null;
}
}
return newImage;
}
/// <summary>
/// 對給定的一個圖片文件生成一個指定大小的縮略圖,並將縮略圖保存到指定位置。
/// </summary>
/// <param name="originalImageFile">圖片的物理文件地址</param>
/// <param name="thumbNailImageFile">縮略圖的物理文件地址</param>
/// <param name="thumMaxWidth">縮略圖的寬度</param>
/// <param name="thumMaxHeight">縮略圖的高度</param>
public static void MakeThumbNail(string originalImageFile, string thumbNailImageFile, int thumMaxWidth, int thumMaxHeight)
{
System.Drawing.Image newImage = GetThumbNailImage(originalImageFile, thumMaxWidth, thumMaxHeight);
try
{