using System;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace Baolee.GeneralMethod
{
/// <summary>
/// CropImage 的摘要说明。
/// </summary>
public class CropImage
{
/// <summary>
/// 构造函数
/// </summary>
public CropImage()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式 HW|W|H|Cut </param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
Image image = Image.FromFile(originalImagePath);
int num = width;
int num2 = height;
int x = 0;
int y = 0;
int num5 = image.Width;
int num6 = image.Height;
string str = mode;
if (str != null)
{
str = string.IsInterned(str);
if (str != "HW")
{
if (str == "W")
{
num2 = (image.Height * width) / image.Width;
}
else if (str == "H")
{
num = (image.Width * height) / image.Height;
}
else if (str == "Cut")
{
if ((((double) image.Width) / ((double) image.Height)) > (((double) num) / ((double) num2)))
{
num6 = image.Height;
num5 = (image.Height * num) / num2;
y = 0;
x = (image.Width - num5) / 2;
}
else
{
num5 = image.Width;
num6 = (image.Width * height) / num;
x = 0;
y = (image.Height - num6) / 2;
}
}
}
}
Image image2 = new Bitmap(num, num2);
Graphics graphics = Graphics.FromImage(image2);
graphics.InterpolationMode = InterpolationMode.High;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.Clear(Color.Transparent);
graphics.DrawImage(image, new Rectangle(0, 0, num, num2), new Rectangle(x, y, num5, num6), GraphicsUnit.Pixel);
try
{
image2.Save(thumbnailPath, ImageFormat.Jpeg);
}
catch (Exception exception)
{
throw exception;
}
finally
{
image.Dispose();
image2.Dispose();
graphics.Dispose();
}
}
}
}