/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="serverImagePath">图片地址</param>
/// <param name="thumbnailImagePath">缩略图地址</param>
/// <param name="width">图片宽度</param>
/// <param name="height">图片高度</param>
/// <param name="p"></param>
public
static
void
GetThumbnail(
string
serverImagePath,
string
thumbnailImagePath,
int
width,
int
height)
{
System.Drawing.Image serverImage = System.Drawing.Image.FromFile(serverImagePath);
int
towidth = width;
int
toheight = height;
int
x = 0;
int
y = 0;
int
ow = serverImage.Width;
int
oh = serverImage.Height;
if
(ow > oh)
{
toheight = serverImage.Height * width / serverImage.Width;
}
else
{
towidth = serverImage.Width * height / serverImage.Height;
}
System.Drawing.Image bm =
new
System.Drawing.Bitmap(width, height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bm);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(System.Drawing.Color.White);
g.DrawImage(serverImage,
new
System.Drawing.Rectangle((width - towidth) / 2, (height - toheight) / 2, towidth, toheight),
0, 0, ow, oh,
System.Drawing.GraphicsUnit.Pixel);
try
{
bm.Save(thumbnailImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch
(System.Exception e)
{
throw
e;
}
finally
{
serverImage.Dispose();
bm.Dispose();
g.Dispose();
}
}