public class GenerateThumbnail { private Image imageFrom; /// <summary> /// 源图的路径(含文件名及扩展名 /// </summary> /// <param name="pathImageFrom">源图的路径(含文件名及扩展名</param> public GenerateThumbnail(string pathImageFrom) { imageFrom = Image.FromFile(pathImageFrom); } /// <summary> /// 生成缩略图 静态方法 /// </summary> /// <param name="pathImageTo">生成的缩略图所保存的路径(含文件名及扩展名)注意:扩展名一定要与生成的缩略图格式相对应</param> /// <param name="Percent">比例 例如 0.8...</param> public void GenThumbnail(string pathImageTo, double Percent) { GenThumbnail(pathImageTo, Convert.ToInt32(imageFrom.Width * Percent), Convert.ToInt32(imageFrom.Height * Percent)); } /**/ /// <summary> /// 生成缩略图 静态方法 /// </summary> /// <param name="pathImageTo"> 生成的缩略图所保存的路径(含文件名及扩展名) /// 注意:扩展名一定要与生成的缩略图格式相对应 </param> /// <param name="width"> 欲生成的缩略图 "画布" 的宽度(像素值) </param> /// <param name="height"> 欲生成的缩略图 "画布" 的高度(像素值) </param> public void GenThumbnail(string pathImageTo, int width, int height) { Bitmap bmp = new Bitmap(width, height); Graphics g = Graphics.FromImage(bmp); g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.SmoothingMode = SmoothingMode.HighQuality; g.DrawImage(imageFrom, new Rectangle(0, 0, width, height), new Rectangle(0, 0, imageFrom.Width, imageFrom.Height), GraphicsUnit.Pixel); try { if (imageFrom.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg)) bmp.Save(pathImageTo, ImageFormat.Jpeg); else if (imageFrom.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png)) bmp.Save(pathImageTo, ImageFormat.Png); else if (imageFrom.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif)) bmp.Save(pathImageTo, ImageFormat.Gif); else if (imageFrom.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Icon)) bmp.Save(pathImageTo, ImageFormat.Icon); else if (imageFrom.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Tiff)) bmp.Save(pathImageTo, ImageFormat.Tiff); else if (imageFrom.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Wmf)) bmp.Save(pathImageTo, ImageFormat.Wmf); else if (imageFrom.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp)) bmp.Save(pathImageTo, ImageFormat.Bmp); else if (imageFrom.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Emf)) bmp.Save(pathImageTo, ImageFormat.Emf); else if (imageFrom.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Exif)) bmp.Save(pathImageTo, ImageFormat.Exif); else throw new Exception("无此类型图片"); } finally { //显示释放资源 imageFrom.Dispose(); bmp.Dispose(); g.Dispose(); } } }
时间: 2024-10-10 06:24:35