前言:
最近在做一个电商系统,需要用户上传商品图片后生成对应的缩列图。应该说上传图片并生成对应的缩列图是一种非常常见的功能,今天我们就分享一段C#生成缩略图的代码,供大家参考。
生成缩略图代码:
FileThumbnail.cs
using com.ycat.cloud.common.Extentions; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace com.ycat.cloud.common.FileExt { /// <summary> /// 堆代码 duidaima.com /// 图片压缩处理 /// </summary> public class FileThumbnail { /// <summary> /// 图片缩放 宽带、高度分别为:1000, 1500 /// 指定宽度,宽度大于指定宽度按指定宽度进行等比缩放 /// by:lb 2015年6月9日 11:59:24 /// </summary> /// <param name="savePath">图片相对路径</param> /// <param name="fileName">图片名称</param> public static void ReducesPic(string savePath, string fileName) { ReducesPic(savePath, fileName, 1000, 1500, 3, fileName); } /// <summary> /// 生成缩略图 /// </summary> /// <param name="savePath"></param> /// <param name="fileName"></param> public static void Thumbnail(string savePath, string fileName) { ReducesPic(savePath, fileName, 120, 120, 3, fileName.getThumbnailName()); } /// <summary> /// 图片缩放 /// </summary> /// <param name="savePath">图片相对路径</param> /// <param name="fileName">图片名称</param> /// <param name="destWidth">缩放宽度</param> /// <param name="destHeight">高度</param> /// <param name="type">1--固定缩放;2--按比例缩放;3--指定宽度,宽度大于指定宽度按指定宽度进行等比缩放,小于指定宽度按原图大小上传;4--原图直接上传</param> /// <returns></returns> public static void ReducesPic(string savePath, string fileName, int destWidth, int destHeight, int type, string newFileName) { if (!fileName.Equals("")) { string Allpath = savePath; //生成原图 System.IO.Stream stream = System.IO.File.OpenRead(Allpath + fileName); System.Drawing.Image oImage = System.Drawing.Image.FromStream(stream); stream.Close(); stream.Dispose(); System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); string fileType = fileName.Substring(fileName.LastIndexOf(".") + 1); int oWidth = oImage.Width; int oHeight = oImage.Height; int tWidth = destWidth; //设置缩略图初始宽度 int tHeight = destHeight; //设置缩略图初始高度 if (destHeight < destWidth)//如果宽度大于高度,则高宽对换 { tWidth = destHeight; tHeight = destWidth; } if (destHeight > oWidth && destHeight > oHeight) { return; } //按指定宽高缩放 if (type == 1) { tWidth = destWidth; tHeight = destHeight; } //按比例计算出缩略图的宽度和高度 else if (type == 2) { if (oWidth > tWidth || oHeight > tHeight) { if (oWidth >= oHeight) { tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth))); } else { tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight))); } } else { tWidth = oWidth; //原图宽度 tHeight = oHeight; //原图高度 } } //指定宽度,宽度大于指定宽度按指定宽度进行等比缩放,小于指定宽度按原图大小上传 else if (type == 3) { if (oWidth >= tWidth) { if (oWidth >= oHeight) { tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth))); } else { tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight))); } } else { tWidth = oWidth; //原图宽度 tHeight = oHeight; //原图高度 } } else { tWidth = oWidth; //原图宽度 tHeight = oHeight; //原图高度 } //生成缩略原图 oImage = oImage.GetThumbnailImage(tWidth, tHeight, callb, IntPtr.Zero); oImage.Save(Allpath + newFileName); } } public static bool ThumbnailCallback() { return false; } } }
总结:
以上就是C#生成缩略图的代码片段,总体思路就是先把图片上传到服务器,然后读取服务器上的图片,调用 System.Drawing的画图类来新建一个缩略图。