ASP.NET 生成缩略图(防失真)。
来源:互联网 发布:mac切换桌面 编辑:程序博客网 时间:2024/05/17 02:06
BuildSourImage(),避免了使用传统的 SaveAs()方法保存文件。
BuildThumbImage()方法,是生成缩略图的,这个生成缩略图的步骤是,首先读取原图。根据原图的大小,按照比例缩放原图。像下面的代码:
//问题1 //这个判断就是判断,哪一个除数大(即高度大),那长度不变,高度乘以比例。反之,类推。 if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight) { smallWidth = intThumbWidth; smallHeight = intThumbWidth * height / width; //问题2 <br> //其实可以写成下面这样子,不知道对不对?我的理解就是,拿缩略图的和原图的宽的比例,然后乘以高度。就是两者比例下的高度了。 //smallHeight = intThumbWidth / width * height; } else { smallWidth = intThumbHeight / height * width; smallHeight = intThumbHeight; }
全部代码如下
protected void Button1_Click(object sender, EventArgs e) { UpLoadImage(File1, "~/UPLOAD/", "X.JPG", 20, 15); } /// <summary> /// asp.net上传图片并生成缩略图 /// </summary> /// <param name="upImage">HtmlInputFile控件</param> /// <param name="sSavePath">保存的路径,些为相对服务器路径的下的文件夹</param> /// <param name="sThumbExtension">缩略图的thumb</param> /// <param name="intThumbWidth">生成缩略图的宽度</param> /// <param name="intThumbHeight">生成缩略图的高度</param> /// <returns>缩略图名称</returns> public string UpLoadImage(HtmlInputFile upImage, string sSavePath, string sThumbExtension, int intThumbWidth, int intThumbHeight) { string sThumbFile = ""; string sFilename = ""; if (upImage.PostedFile != null) { sFilename = BuildSourImage(upImage, sSavePath);//上传原图 sThumbFile = BuildThumbImage(sSavePath + sFilename, sFilename, sSavePath + "small/", 300, 200); //上传缩略图 return sThumbFile; } return "没有选择图片"; } /// <summary> /// 上传大图 /// </summary> /// <param name="upImage"></param> /// <param name="sSavePath"></param> /// <returns></returns> public string BuildSourImage(HtmlInputFile upImage, string sSavePath) { string sFilename = ""; HttpPostedFile myFile = upImage.PostedFile; int nFileLen = myFile.ContentLength; if (nFileLen == 0) return "没有选择上传图片"; //获取upImage选择文件的扩展名 string extendName = System.IO.Path.GetExtension(myFile.FileName).ToLower(); //判断是否为图片格式 if (extendName != ".jpg" && extendName != ".jpge" && extendName != ".gif" && extendName != ".bmp" && extendName != ".png") return "图片格式不正确"; byte[] myData = new Byte[nFileLen]; myFile.InputStream.Read(myData, 0, nFileLen); sFilename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + extendName; //检查当前文件夹下是否有同名图片,有则在文件名+1 System.IO.FileStream newFile = new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath(sSavePath) + sFilename, System.IO.FileMode.Create, System.IO.FileAccess.Write); newFile.Write(myData, 0, myData.Length); newFile.Close(); return sFilename; } /// <summary> /// 生成缩略图 /// </summary> /// <param name="sSourceFile">原图地址</param> /// <param name="thumbName">缩略图名称</param> /// <param name="sSavePath">保存地址</param> /// <param name="intThumbWidth">缩略图宽度</param> /// <param name="intThumbHeight">缩略图高度</param> /// <returns></returns> public string BuildThumbImage(string sSourceFile, string thumbName, string sSavePath, int intThumbWidth, int intThumbHeight) { try { //原图加载 using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(sSourceFile))) { //原图宽度和高度 int width = sourceImage.Width; int height = sourceImage.Height; int smallWidth; int smallHeight; //获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽 和 原图的高/缩略图的高) if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight) { smallWidth = intThumbWidth; smallHeight = intThumbWidth * height / width; } else { smallWidth = intThumbHeight * width / height; smallHeight = intThumbHeight; } //缩略图保存的绝对路径 string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(sSavePath) + thumbName; //新建一个图板,以最小等比例压缩大小绘制原图 using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight)) { //绘制中间图 using (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(Color.Black); g.DrawImage( sourceImage, new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight), new System.Drawing.Rectangle(0, 0, width, height), System.Drawing.GraphicsUnit.Pixel ); } //新建一个图板,以缩略图大小绘制中间图 using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight)) { //绘制缩略图 using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1)) { //高清,平滑 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.Black); int lwidth = (smallWidth - intThumbWidth) / 2; int bheight = (smallHeight - intThumbHeight) / 2; g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel); g.Dispose(); bitmap1.Save(smallImagePath, System.Drawing.Imaging.ImageFormat.Jpeg); } } } } } catch { //出错则删除 System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(sSavePath + thumbName)); return "图片格式不正确"; } return thumbName; }
- ASP.NET 生成缩略图(防失真)。
- ASP.NET C# 生成不失真的缩略图
- asp.net生成缩略图
- asp.net生成缩略图
- asp.net生成缩略图
- Asp.NET生成缩略图
- asp.net 生成缩略图
- ASP.NET生成缩略图
- asp.net生成缩略图
- ASP.Net生成缩略图
- asp.net生成缩略图
- asp.net生成缩略图
- ASP.NET生成缩略图
- ASP.NET [C#] 生成缩略图
- asp.net 2.0 生成缩略图
- asp.net中生成缩略图
- 用ASP.Net生成缩略图
- ASP.NET生成缩略图代码
- OC中的集合类-NSSet、NSArray、NSDictionary
- 日发明家研制新仪器 可把狗叫声翻译成人话
- hadoop源码研读之路(六)----RPC的Client端和Server端
- 使用jQuery和CSS3来访问Dribbble的API
- 提到刺这种兵器的guandan
- ASP.NET 生成缩略图(防失真)。
- python使用socket非阻塞编程
- SQL SERVER SQLOS的任务调度
- 质量功能配置(QFD)矩阵
- 设置程序占用的CPU运行核数-可以实现别的程序流畅运行
- 如何诊断修正17883,17884,17887,17888错误
- mysql 启动远程连接
- Extjs 多选下拉框 Ext.ux.form.LovCombo默认选择及其它BUG修复版支持多选/全选/全不选ext3.x
- JTree的拖曳