缩略图通常是将图片内容进行一定的缩小展现,或裁剪展现,主要有两个目的,一是提供一定的预览功能,二是节省屏幕展示空间、节省流量。
在网站中我们通常运用在商品的列表,比如商城、图书、新闻等等列表的图片。在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(); } Const.iErrorCount++; 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; gp.CompositingQuality = 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) { LogManager.WriteLog("复制图片:" + originalImagePath + "失败!\r\n" + ex.Message + ex.StackTrace); Const.iErrorCount++; throw; } finally { if (img_OriginalImage != null) { img_OriginalImage.Dispose(); } if (img_BitMap != null) { img_BitMap.Dispose(); } if (gp != null) { gp.Dispose(); } //GC.Collect(); } }
通过如上的几步就完成了缩略图的生成。