/// <summary> /// 上传图片,只有在宽度>0且高度>0时创建缩略图 /// </summary> /// <param name="imgFile">上传控件</param> /// <param name="maxSize">最大上传文件大小(单位:M)</param> /// <param name="thumbnailWidth">缩略图宽度</param> /// <param name="thumbnailHeight">缩略图高度</param> /// <param name="mode">创建缩略图方式</param> /// <param name="imgUrl">返回的图片链接</param> /// <returns></returns> public UploadStatus UploadImage(HttpPostedFile imgFile, int maxSize, int thumbnailWidth, int thumbnailHeight, CreateThumbnailMode mode, ref string imgUrl) { if (imgFile == null || string.IsNullOrEmpty(imgFile.FileName)) { return(UploadStatus.US_NOT_FOUND); } else { try { HttpContext context = HttpContext.Current; string appPath = context.Request.ApplicationPath; if (!appPath.EndsWith("/")) { appPath += "/"; } string uploadPath = ConfigurationManager.AppSettings["UploadPath"]; if (string.IsNullOrEmpty(uploadPath)) { uploadPath = "~/Files/"; } if (uploadPath.StartsWith("~/")) { uploadPath = appPath + uploadPath.Substring(2); } else if (uploadPath.StartsWith("/")) { uploadPath = appPath + uploadPath.Substring(1); } else { uploadPath = appPath + uploadPath; } if (!uploadPath.EndsWith("/")) { uploadPath += "/"; } //文件保存目录路径 string savePath = context.Server.MapPath(uploadPath); //文件保存目录URL string saveUrl = uploadPath; //定义允许上传的文件扩展名 Hashtable extTable = new Hashtable(); extTable.Add("image", "gif,jpg,jpeg,png,bmp"); //最大文件大小 //int maxSize = 5; //5M //string fileMaxSize = ConfigurationManager.AppSettings["FileMaxSize"]; //if (!string.IsNullOrEmpty(fileMaxSize)) // maxSize = Convert.ToInt32(fileMaxSize); if (maxSize < 1) { maxSize = 5; } maxSize = maxSize * 1024 * 1024; string dirPath = savePath; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } string dirName = "image"; string fileName = imgFile.FileName; string fileExt = Path.GetExtension(fileName).ToLower(); if (imgFile.InputStream == null || imgFile.InputStream.Length > maxSize) { //上传文件大小超过限制 return(UploadStatus.US_TOO_LONG); } if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(((string)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1) { //上传文件扩展名是不允许的扩展名。 return(UploadStatus.US_NOT_SUPPORT); } //创建文件夹 dirPath += dirName + "\\"; saveUrl += dirName + "/"; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } string ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo); dirPath += ymd + "\\"; saveUrl += ymd + "/"; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff_b", DateTimeFormatInfo.InvariantInfo) + fileExt; string filePath = dirPath + newFileName; imgFile.SaveAs(filePath); string thumbnailFile = newFileName.Replace("_b", "_s"); string thumbnailFilePath = dirPath + thumbnailFile; if (mode != CreateThumbnailMode.CTM_NONE && (thumbnailWidth > 0 && thumbnailHeight > 0)) { // 生成缩略图 CreateThumbnail(filePath, thumbnailFilePath, thumbnailWidth, thumbnailHeight, mode); } // 添加水印 if (WatermarkInfo.AddWatermark == 1 || WatermarkInfo.AddWatermark == 3) { newFileName = "w_" + newFileName; addWaterMark(filePath, dirPath + newFileName, true); } if (WatermarkInfo.AddWatermark == 2 || WatermarkInfo.AddWatermark == 3) { thumbnailFile = "w_" + thumbnailFile; addWaterMark(thumbnailFilePath, dirPath + thumbnailFile, false); } // 返回图片链接 imgUrl = saveUrl + newFileName; return(UploadStatus.US_SUCCESSED); } catch { return(UploadStatus.US_FAILED); } } }
/// <summary> /// 上传图片,只有在宽度>0且高度>0时创建缩略图 /// </summary> /// <param name="imgFile">上传控件</param> /// <param name="maxSize">最大上传文件大小(单位:M)</param> /// <param name="thumbnailWidth">缩略图宽度</param> /// <param name="thumbnailHeight">缩略图高度</param> /// <param name="mode">创建缩略图方式</param> /// <param name="imgUrl">返回的图片链接</param> /// <returns></returns> public UploadStatus UploadImage(HttpPostedFile imgFile, int maxSize, int thumbnailWidth, int thumbnailHeight, CreateThumbnailMode mode, ref string imgUrl) { if (imgFile == null || string.IsNullOrEmpty(imgFile.FileName)) { return UploadStatus.US_NOT_FOUND; } else { try { HttpContext context = HttpContext.Current; string appPath = context.Request.ApplicationPath; if (!appPath.EndsWith("/")) appPath += "/"; string uploadPath = ConfigurationManager.AppSettings["UploadPath"]; if (string.IsNullOrEmpty(uploadPath)) { uploadPath = "~/Files/"; } if (uploadPath.StartsWith("~/")) { uploadPath = appPath + uploadPath.Substring(2); } else if (uploadPath.StartsWith("/")) { uploadPath = appPath + uploadPath.Substring(1); } else { uploadPath = appPath + uploadPath; } if (!uploadPath.EndsWith("/")) uploadPath += "/"; //文件保存目录路径 string savePath = context.Server.MapPath(uploadPath); //文件保存目录URL string saveUrl = uploadPath; //定义允许上传的文件扩展名 Hashtable extTable = new Hashtable(); extTable.Add("image", "gif,jpg,jpeg,png,bmp"); //最大文件大小 //int maxSize = 5; //5M //string fileMaxSize = ConfigurationManager.AppSettings["FileMaxSize"]; //if (!string.IsNullOrEmpty(fileMaxSize)) // maxSize = Convert.ToInt32(fileMaxSize); if (maxSize < 1) maxSize = 5; maxSize = maxSize * 1024 * 1024; string dirPath = savePath; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } string dirName = "image"; string fileName = imgFile.FileName; string fileExt = Path.GetExtension(fileName).ToLower(); if (imgFile.InputStream == null || imgFile.InputStream.Length > maxSize) { //上传文件大小超过限制 return UploadStatus.US_TOO_LONG; } if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(((string)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1) { //上传文件扩展名是不允许的扩展名。 return UploadStatus.US_NOT_SUPPORT; } //创建文件夹 dirPath += dirName + "\\"; saveUrl += dirName + "/"; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } string ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo); dirPath += ymd + "\\"; saveUrl += ymd + "/"; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff_b", DateTimeFormatInfo.InvariantInfo) + fileExt; string filePath = dirPath + newFileName; imgFile.SaveAs(filePath); string thumbnailFile = newFileName.Replace("_b", "_s"); string thumbnailFilePath = dirPath + thumbnailFile; if (mode != CreateThumbnailMode.CTM_NONE && (thumbnailWidth > 0 && thumbnailHeight > 0)) { // 生成缩略图 CreateThumbnail(filePath, thumbnailFilePath, thumbnailWidth, thumbnailHeight, mode); } // 添加水印 if (WatermarkInfo.AddWatermark == 1 || WatermarkInfo.AddWatermark == 3) { newFileName = "w_" + newFileName; addWaterMark(filePath, dirPath + newFileName, true); } if (WatermarkInfo.AddWatermark == 2 || WatermarkInfo.AddWatermark == 3) { thumbnailFile = "w_" + thumbnailFile; addWaterMark(thumbnailFilePath, dirPath + thumbnailFile, false); } // 返回图片链接 imgUrl = saveUrl + newFileName; return UploadStatus.US_SUCCESSED; } catch { return UploadStatus.US_FAILED; } } }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> public bool CreateThumbnail(string originalImagePath, string thumbnailPath, int width, int height, CreateThumbnailMode mode) { bool ret = true; Image originalImage = 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 CreateThumbnailMode.CTM_WIDTH_HEIGHT: //指定高宽缩放(可能变形) break; case CreateThumbnailMode.CTM_WIDTH: //指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case CreateThumbnailMode.CTM_HEIGHT: //指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case CreateThumbnailMode.CTM_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图片 Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板 Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法 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, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); try { //以jpg格式保存缩略图 bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (System.Exception e) { //throw e; ret = false; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } return(ret); }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> public bool CreateThumbnail(string originalImagePath, string thumbnailPath, int width, int height, CreateThumbnailMode mode) { bool ret = true; Image originalImage = 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 CreateThumbnailMode.CTM_WIDTH_HEIGHT://指定高宽缩放(可能变形) break; case CreateThumbnailMode.CTM_WIDTH://指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case CreateThumbnailMode.CTM_HEIGHT://指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case CreateThumbnailMode.CTM_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图片 Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板 Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法 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, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); try { //以jpg格式保存缩略图 bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (System.Exception e) { //throw e; ret = false; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } return ret; }