///<summary>///生成缩略图
///</summary>///<paramname="originalImagePath">源图路径(物理路径)</param>///<paramname="thumbnailPath">缩略图路径(物理路径)</param>///<paramname="width">缩略图宽度</param>///<paramname="height">缩略图高度</param>publicvoidMakeThumbnail(stringoriginalImagePath,stringthumbnailPath,intwidth,intheight)
{
System.Drawing.ImageoriginalImage=System.Drawing.Image.FromFile(originalImagePath);
inttowidth=0;
inttoheight=0;
if(originalImage.Width>width&&originalImage.Height<height)
{
towidth=width;
toheight=originalImage.Height;
}
if(originalImage.Width<width&&originalImage.Height>height)
{
towidth=originalImage.Width;
toheight=height;
}
if(originalImage.Width>width&&originalImage.Height>height)
{
towidth=width;
toheight=height;
}
if(originalImage.Width<width&&originalImage.Height<height)
{
towidth=originalImage.Width;
toheight=originalImage.Height;
}
intx=0;//左上角的x坐标inty=0;//左上角的y坐标
//新建一个bmp图片System.Drawing.Imagebitmap=newSystem.Drawing.Bitmap(towidth,toheight);
//新建一个画板Graphicsg=System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法g.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充g.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分g.DrawImage(originalImage,x,y,towidth,toheight);
try
{
//以jpg格式保存缩略图bitmap.Save(thumbnailPath,System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch(System.Exceptione)
{
throwe;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}