ASP.NET 按规定尺寸缩放图片
2016-03-25 14:07
393 查看
public string ImgFileUp(HttpPostedFile HPF, string saveDir) { string returnStr1 = ""; string Extension = new FileInfo(HPF.FileName).Extension; #region 保存原始文件 //清除多余字符串 saveDir = saveDir.TrimEnd('/').TrimStart('/'); //获取上传日期 string time = string.Concat("/", DateTime.Now.ToString("yyyyMMdd")); //判读上传文件夹是否存在 bool existDir = Directory.Exists(Server.MapPath(string.Concat("~/", saveDir, time))); //不存在文件夹,自动创建 if (!existDir) { //创建文件夹 Directory.CreateDirectory(Server.MapPath(string.Concat("~/", saveDir, time))); } //上传的最初文件名+后缀 string dbImageName = ""; if (n == false) { dbImageName = System.IO.Path.GetFileName(HPF.FileName); } else { dbImageName = Guid.NewGuid() + DateTime.Now.ToString("yyyyMMddHHmmssfff") + Extension; } //上传的图像保存的物理路径 string originalImagePath = Server.MapPath(string.Concat("~/", saveDir, time, "/", dbImageName)); //保存图片 //HPF.SaveAs(originalImagePath); //string originalImagePath = Server.MapPath("/" + saveDir + "/" + time + "/" + dbImageName); //生成压缩文件 System.Drawing.Image originalImage = System.Drawing.Image.FromStream(HPF.InputStream); int maxWidth =900; //图片宽度最大限制 int maxHeight = 900; //图片高度最大限制 int imgWidth = originalImage.Width; int imgHeight = originalImage.Height; if (imgHeight > maxHeight || imgWidth>maxWidth) //如果宽度超或者高度超过进行压缩 { if (imgWidth > imgHeight) { if (imgWidth > maxWidth) //如果图片宽度超过限制 { float toImgWidth = maxWidth; //图片压缩后的宽度 float toImgHeight = imgHeight / (float)(imgWidth / toImgWidth); //图片压缩后的高度 //新建一个bmp图片 System.Drawing.Image img = new System.Drawing.Bitmap(Convert.ToInt32(toImgWidth), Convert.ToInt32(toImgHeight)); Graphics g = System.Drawing.Graphics.FromImage(img);//新建画板 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度 g.Clear(Color.Transparent);//清空画布 g.DrawImage(originalImage, new Rectangle(0, 0, Convert.ToInt32(toImgWidth), Convert.ToInt32(toImgHeight)), new System.Drawing.Rectangle(0, 0, imgWidth, imgHeight), GraphicsUnit.Pixel);//绘制原图片 img.Save(originalImagePath, System.Drawing.Imaging.ImageFormat.Jpeg); originalImage.Dispose(); img.Dispose(); g.Dispose(); } } else { if (imgHeight > maxHeight) { float toImgHeight = maxHeight; //图片压缩后的高度 float toImgWidth = imgWidth / (float)(imgHeight / toImgHeight); //图片压缩后的宽度 //新建一个bmp图片 System.Drawing.Image img = new System.Drawing.Bitmap(Convert.ToInt32(toImgWidth), Convert.ToInt32(toImgHeight)); Graphics g = System.Drawing.Graphics.FromImage(img);//新建画板 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度 g.Clear(Color.Transparent);//清空画布 g.DrawImage(originalImage, new Rectangle(0, 0, Convert.ToInt32(toImgWidth), Convert.ToInt32(toImgHeight)), new System.Drawing.Rectangle(0, 0, imgWidth, imgHeight), GraphicsUnit.Pixel);//绘制原图片 img.Save(originalImagePath, System.Drawing.Imaging.ImageFormat.Jpeg); originalImage.Dispose(); img.Dispose(); g.Dispose(); } } } else { HPF.SaveAs(originalImagePath); } returnStr1 = time + "/" + dbImageName; #endregion return returnStr1; }
相关文章推荐
- 分享微信开发Html5轻游戏中的几个坑
- 按右键另存图片只能存BMP
- photoshop去除图片上的水印
- 星外ASP.Net的安全设置相关说明
- C#、ASP.NET通用扩展工具类之TypeParse
- upload上传单张图片
- 图片引发的溢出危机(图)
- 实现ASP.NET无刷新下载并提示下载完成的开发思路
- C#实现把彩色图片灰度化代码分享
- C#、ASP.NET通用扩展工具类之LogicSugar
- C#将图片和字节流互相转换并显示到页面上
- C#监控文件夹并自动给图片文件打水印的方法
- 纯CSS实现的当鼠标移上图片添加阴影效果代码
- 如何使用C#从word文档中提取图片
- C#实现打开画图的同时载入图片、最大化显示画图窗体的方法
- C#、ASP.NET通用工具类IsWhat?(可以判断数字、身份证、数据类型等等)
- C#图片添加水印的实现代码
- 随鼠标移动的图片或文字特效代码
- CSS 图片横向排列实现代码
- 程序中常用的种代码