C# 上传文件并生成缩略图
2016-04-06 14:09
483 查看
#region 上传文件并生成缩略图 /// <summary> /// 上传文件并生成缩略图 /// </summary> /// <param name="paramFileUpload">上传控件</param> /// <param name="paramVirtualPath">上传目标虚拟路径</param> /// <param name="paramDirectory">用户自定义创建文件夹</param> /// <param name="paramOriginalDirectory">存放原始文件夹</param> /// <param name="paramThumbnailDirectory">存放缩略图文件夹</param> /// <param name="tWidth">缩略图默认宽度</param> /// <param name="tHeight">缩略图默认高度</param> private string UploadAndThumbnail(FileUpload paramFileUpload, string paramVirtualPath, string paramDirectory, string paramOriginalDirectory, string paramThumbnailDirectory, int tWidth, int tHeight, string paramFileName) { string returnValue = string.Empty;//返回值 string imgType;//文件类型 string fullFileName = paramFileUpload.PostedFile.FileName; //上传文件名 imgType = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1).ToLower();//文件类型 string UserPath = Server.MapPath(paramVirtualPath).ToString() + "\\" + paramDirectory;//存放文件目录 //如果文件夹不存在则创建 if (!Directory.Exists(UserPath)) { Directory.CreateDirectory(UserPath); } string originalImagePath = UserPath + "\\" + paramOriginalDirectory;//原始路径 if (!Directory.Exists(originalImagePath)) { Directory.CreateDirectory(originalImagePath); } string thumbnailPath = UserPath + "\\" + paramThumbnailDirectory; if (!Directory.Exists(thumbnailPath)) { Directory.CreateDirectory(thumbnailPath); } // 获取上传图像的文件名 string fileName = paramFileName; if (imgType == "jpg" || imgType == "jpeg" || imgType == "bmp" || imgType == "png" || imgType == "icon") { try { //生成原图 byte[] oFileByte = new byte[paramFileUpload.PostedFile.ContentLength]; Stream oStream = paramFileUpload.PostedFile.InputStream; System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream); int oWidth = oImage.Width;//原图宽度 int oHeight = oImage.Height;//原图高度 if (tWidth == 0) { tWidth = 100; } if (tHeight == 0) { tHeight = 100; } //按比例计算出缩略图的宽度和高度 if (oWidth >= oHeight) { tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oHeight))); } else { tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight))); } //生成缩略图 Bitmap tImage = new Bitmap(tWidth, tHeight); Graphics g = Graphics.FromImage(tImage); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度 g.Clear(Color.Transparent);//清空画布并以透明背景色填充 g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel); string oPath = originalImagePath + "\\" + fileName + ".jpg"; string tPath = thumbnailPath + "\\" + fileName + ".jpg"; //以JPG格式保存图像 oImage.Save(oPath, System.Drawing.Imaging.ImageFormat.Jpeg); tImage.Save(tPath, System.Drawing.Imaging.ImageFormat.Jpeg); returnValue = "上传成功!"; //释放资源 oImage.Dispose(); g.Dispose(); tImage.Dispose(); } catch (Exception ex) { returnValue = "由于网络原因,上载文件错误 " + ex.Message; } } else { returnValue = "你选择的图像格式错误!"; } return returnValue; } #endregion
相关文章推荐
- C# Winform 程序打包部署
- c# 读取xml
- C# 获取本地电脑所有的盘符
- C# 获取本地电脑所有的盘符
- C#目录文件监控
- C#读写txt文件的两种方法介绍
- C#进阶系列——WebApi接口传参不再困惑:传参详解
- C#实现字符串按多个字符采用Split方法分割
- C# IP地址与整数之间的转换
- C#实例之聊天机器人
- C#最简单最完整的webservice实例
- C#界面设计之图像去雾算法
- .NET C# 创建WebService服务简单的例子
- C#界面设计之谷歌地图下载并分块
- C#界面设计之窗体绘制
- C# - Marshal.StructureToPtr方法简介
- C#实现冒泡排序
- C#学习笔记(初级)
- C#实例之随机数的猜想
- C#枚举,常量(初级)