缩略图通常是将图片内容进行一定的缩小展现,或裁剪展现,主要有两个目的,一是提供一定的预览功能,二是节省屏幕展示空间、节省流量。
在网站中我们通常运用在商品的列表,比如商城、图书、新闻等等列表的图片。在C#中我们如何生成缩略图呢,也就是缩小图片,下面我们来看一看如何缩小图片。
其中方法参数如下:
originalImagePath:源图路径(物理路径)
thumbnailPath:缩略图路径(物理路径)
width:生成缩略图宽度
height:生成缩略图高度
mode:生成缩略图的模式
代码如下
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, ThumbnailMode mode){System.Drawing.Image img_OriginalImage = null;ImageFormat tFormat = null;try{img_OriginalImage = (Bitmap)System.Drawing.Image.FromFile(originalImagePath);tFormat = img_OriginalImage.RawFormat;}catch (Exception ex){if (img_OriginalImage != null){img_OriginalImage.Dispose();}throw new Exception("压缩图片:" + originalImagePath + "失败!\r\n" + ex.Message);}int i_ToWidth = width;int i_ToHeight = height;int x = 0;int y = 0;int i_OriginalWidth = img_OriginalImage.Width;int i_OriginalHeight = img_OriginalImage.Height;switch (mode){case ThumbnailMode.Width:i_ToHeight = img_OriginalImage.Height * width / img_OriginalImage.Width;break;case ThumbnailMode.Height:i_ToWidth = img_OriginalImage.Width * height / img_OriginalImage.Height;break;case ThumbnailMode.Cut:if ((double)img_OriginalImage.Width / (double)img_OriginalImage.Height > (double)i_ToWidth / (double)i_ToHeight){i_OriginalHeight = img_OriginalImage.Height;i_OriginalWidth = img_OriginalImage.Height * i_ToWidth / i_ToHeight;y = 0;x = (img_OriginalImage.Width - i_OriginalWidth) / 2;}else{i_OriginalWidth = img_OriginalImage.Width;i_OriginalHeight = img_OriginalImage.Width * height / i_ToWidth;x = 0;y = (img_OriginalImage.Height - i_OriginalHeight) / 2;}break;default:break;}//新建一个BMP图片System.Drawing.Image img_BitMap = new System.Drawing.Bitmap(i_ToWidth, i_ToHeight);//新建一个画板System.Drawing.Graphics gp = Graphics.FromImage(img_BitMap);//设置高质量插值法gp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;//设置高质量、低速度呈现平滑程度gp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;positingQuality = CompositingQuality.HighQuality;//清空画布并以透明背景色填充gp.Clear(Color.Transparent);//指定位置并按大小绘制原图片的指定部分gp.DrawImage(img_OriginalImage, new Rectangle(0, 0, i_ToWidth, i_ToHeight), new Rectangle(x, y, i_OriginalWidth, i_OriginalHeight), GraphicsUnit.Pixel);try{EncoderParameters ep = new EncoderParameters();long[] qy = new long[1];qy[0] = 100;//设置压缩的比例1-100 EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);ep.Param[0] = eParam;ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();ImageCodecInfo jpegICIinfo = null;for (int i = 0; i < arrayICI.Length; i++){if (arrayICI[i].FormatDescription.Equals("JPEG")){jpegICIinfo = arrayICI[i];break;}}if (jpegICIinfo != null && Equals(tFormat, ImageFormat.Jpeg)){//以JPG格式保存图片img_BitMap.Save(thumbnailPath, jpegICIinfo, ep);//Const.iSuccessCount++;}else{img_BitMap.Save(thumbnailPath, tFormat);//Const.iSuccessCount++;}}catch (Exception ex){Console.WriteLine("复制图片:" + originalImagePath + "失败!\r\n" + ex.Message + ex.StackTrace);}finally{if (img_OriginalImage != null){img_OriginalImage.Dispose();}if (img_BitMap != null){img_BitMap.Dispose();}if (gp != null){gp.Dispose();}//GC.Collect();}}
public enum ThumbnailMode{/// <summary>/// 指定宽度,高度按照比例缩放/// </summary>Width = 0,/// <summary>/// 指定高度,宽度按照比例缩放/// </summary>Height = 1,/// <summary>/// 按照指定的高度和宽度剪裁/// </summary>Cut = 2,/// <summary>/// 指定宽度,高度按照比例缩放/// </summary>None = 3}
在该方法中我们首先使用(Bitmap)System.Drawing.Image.FromFile将文件加载到bitmap。根据mode传入的压缩模式进行原始绘图区域的计算。将计算出的区域绘制到新的BitMap。将绘制的BitMap图片保存到指定文件。
通过如上的几步就完成了缩略图的生成。