生成规定大小的图片(缩略图生成)
2010-08-19 17:42
239 查看
做一购物网站,改版N次,每次改版那产品列表图的大小都会变,第一次是90*70,第二次改版又变成160*120,每次改版都得把产品图片文件夹中的2W多张图片一个一个转为对应的大小的图片,以前用的是网上找的一个方法:
缩略图生成
缩略图生成
/// <summary>生成缩略图 /// /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> /// public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode) { System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; switch (mode) { case "HW"://指定高宽缩放(可能变形) break; case "W"://指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case "H"://指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case "Cut"://指定高宽裁减(不变形) if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; default: break; } //新建一个bmp图片 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以白色背景色填充 g.Clear(System.Drawing.Color.White); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel); try { //以jpg格式保存缩略图 bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (System.Exception e) { throw e; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } } 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Chenindex/archive/2010/08/18/5821646.aspx
相关文章推荐
- 生成规定大小的图片(缩略图生成)
- 生成规定大小的图片(缩略图生成)
- 生成规定大小的图片(缩略图生成)
- 生成规定大小的图片(缩略图生成)
- 生成规定大小的图片(缩略图生成)
- .net上传图片生成大小缩略图
- 限制图片 大小 生成等比例缩略图 asp.net和js代码
- ASP.NET C# 按原图片大小等比例缩放生成缩略图
- ASP.NET C# 按原图片大小缩放生成缩略图
- ASP.NET C# 按原图片大小缩放生成缩略图
- php实现按指定大小等比缩放生成上传图片缩略图的方法
- 以图片的中心为基准,按照任意尺寸自动调整大小并截取出规定的素材,保存为缩略图或以php形式输出为图片
- 学习 ------ ASP.NET C# 按原图片大小缩放生成缩略图
- 使用PHP实现生成固定大小图片缩略图功能(智能裁剪,图片不失真)
- net上传图片生成大小缩略图
- php按指定大小等比缩放生成上传图片缩略图
- php实现按指定大小等比缩放生成上传图片缩略图的方法
- .net上传图片生成大小缩略图
- .net上传图片生成大小缩略图
- 上传图片 生成指定大小的缩略图