/// <summary> /// 通用图片上传类 /// </summary> /// <param name="PostedFile">HttpPostedFile控件</param> /// <param name="SaveFolder">保存路径【sys.config配置路径】</param> /// <returns>返回上传信息</returns> public ResponseMessage FileSaveAs(System.Web.HttpPostedFile PostedFile, string SaveFolder) { ResponseMessage rm = new ResponseMessage(); try { if (string.IsNullOrEmpty(PostedFile.FileName)) { TryError(rm, 4); return(rm); } Random rd = new Random(); int rdInt = rd.Next(1000, 9999); //重命名名称 string NewfileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + rdInt; //获取上传文件的扩展名 string sEx = System.IO.Path.GetExtension(PostedFile.FileName); if (!CheckValidExt(SetAllowFormat, sEx)) { TryError(rm, 2); return(rm); } //获取上传文件的大小 double PostFileSize = PostedFile.ContentLength / 1024.0 / 1024.0; if (PostFileSize > SetAllowSize) { TryError(rm, 3); return(rm); } if (!System.IO.Directory.Exists(SaveFolder)) { System.IO.Directory.CreateDirectory(SaveFolder); } rm.FileName = NewfileName + sEx; string fullPath = SaveFolder.Trim('\\') + "\\" + rm.FileName; rm.WebPath = "/" + fullPath.Replace(HttpContext.Current.Server.MapPath("~/"), "").Replace("\\", "/"); rm.WebFilePath = rm.WebPath; rm.filePath = fullPath; rm.Size = PostFileSize; PostedFile.SaveAs(fullPath); System.Drawing.Bitmap bmp = new Bitmap(fullPath); int realWidth = bmp.Width; int realHeight = bmp.Height; bmp.Dispose(); #region 检测图片宽度限制 if (SetMinWidth > 0) { if (realWidth < SetMinWidth) { TryError(rm, 7); return(rm); } } #endregion #region 监测图片宽度是否超过600,超过的话,自动压缩到600 if (SetLimitWidth && realWidth > SetMaxWidth) { int mWidth = SetMaxWidth; int mHeight = mWidth * realHeight / realWidth; string tempFile = SaveFolder + Guid.NewGuid().ToString() + sEx; File.Move(fullPath, tempFile); CreateSmallPhoto(tempFile, mWidth, mHeight, fullPath, "", ""); File.Delete(tempFile); } #endregion #region 压缩图片存储尺寸 if (sEx.ToLower() != ".gif") { CompressPhoto(fullPath, 100); } #endregion //生成缩略图片高宽 if (string.IsNullOrEmpty(SetSmallImgWidth)) { rm.Message = "上传成功,无缩略图"; return(rm); } string[] oWidthArray = SetSmallImgWidth.Split(','); string[] oHeightArray = SetSmallImgHeight.Split(','); if (oWidthArray.Length != oHeightArray.Length) { TryError(rm, 6); return(rm); } for (int i = 0; i < oWidthArray.Length; i++) { if (Convert.ToInt32(oWidthArray[i]) <= 0 || Convert.ToInt32(oHeightArray[i]) <= 0) { continue; } string sImg = SaveFolder.TrimEnd('\\') + '\\' + NewfileName + "_" + i.ToString() + sEx; //判断图片高宽是否大于生成高宽。否则用原图 if (realWidth > Convert.ToInt32(oWidthArray[i])) { if (SetCutImage) { CreateSmallPhoto(fullPath, Convert.ToInt32(oWidthArray[i]), Convert.ToInt32(oHeightArray[i]), sImg, "", ""); } else { CreateSmallPhoto(fullPath, Convert.ToInt32(oWidthArray[i]), Convert.ToInt32(oHeightArray[i]), sImg, "", "", CutMode.CutNo); } } else { if (SetCutImage) { CreateSmallPhoto(fullPath, realWidth, realHeight, sImg, "", ""); } else { CreateSmallPhoto(fullPath, realWidth, realHeight, sImg, "", "", CutMode.CutNo); } } } #region 给大图添加水印 if (!string.IsNullOrEmpty(SetPicWater)) { AttachPng(SetPicWater, fullPath); } else if (!string.IsNullOrEmpty(SetWordWater)) { AttachText(SetWordWater, fullPath); } #endregion } catch (Exception ex) { TryError(rm, ex.Message); } return(rm); }
/// <summary> /// 通用图片上传类 /// </summary> /// <param name="postedFile">HttpPostedFile控件</param> /// <param name="savePath">保存路径</param> /// <returns>返回上传信息</returns> public UploadImageMessage FileSaveAs(HttpPostedFile postedFile, string savePath) { UploadImageMessage _uploadImageMsg = new UploadImageMessage(); try { if (string.IsNullOrEmpty(postedFile.FileName)) { AddUploadImageMessage(_uploadImageMsg, 4); return(_uploadImageMsg); } int _randomNumber = RandomHelper.NextNumber(1000, 9999); string _fileName = DateTime.Now.FormatDate(12) + _randomNumber, _fileEx = Path.GetExtension(postedFile.FileName); if (!FileHelper.CheckValidExt(SetAllowFormat, _fileEx)) { AddUploadImageMessage(_uploadImageMsg, 2); return(_uploadImageMsg); } double _fileSize = postedFile.ContentLength / 1024.0 / 1024.0; if (_fileSize > SetAllowSize) { AddUploadImageMessage(_uploadImageMsg, 3); return(_uploadImageMsg); } if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } _uploadImageMsg.FileName = _fileName + _fileEx; string _fullPath = savePath.Trim('\\') + "\\" + _uploadImageMsg.FileName; _uploadImageMsg.WebPath = "/" + _fullPath.Replace(HttpContext.Current.Server.MapPath("~/"), "").Replace("\\", "/"); _uploadImageMsg.FilePath = _fullPath; _uploadImageMsg.Size = _fileSize; postedFile.SaveAs(_fullPath); Bitmap _sourceBmp = new Bitmap(_fullPath); int _sourceWidth = _sourceBmp.Width, _sourceHeight = _sourceBmp.Height; _sourceBmp.Dispose(); if (SetMinWidth > 0) { if (_sourceWidth < SetMinWidth) { AddUploadImageMessage(_uploadImageMsg, 7); return(_uploadImageMsg); } } if (SetLimitWidth && _sourceWidth > SetMaxWidth) { int _width = SetMaxWidth; int _height = _width * _sourceHeight / _sourceWidth; string _tempFile = savePath + Guid.NewGuid().ToString() + _fileEx; File.Move(_fullPath, _tempFile); ImageHelper.CreateSmallPhoto(_tempFile, _width, _height, _fullPath); File.Delete(_tempFile); } if (_fileEx.ToLower() != ".gif") { ImageHelper.CompressPhoto(_fullPath, 100); } if (string.IsNullOrEmpty(SetSmallImgWidth)) { _uploadImageMsg.Message = "上传成功,无缩略图"; return(_uploadImageMsg); } string[] _widthArray = SetSmallImgWidth.Split(','); string[] _heightArray = SetSmallImgHeight.Split(','); if (_widthArray.Length != _heightArray.Length) { AddUploadImageMessage(_uploadImageMsg, 6); return(_uploadImageMsg); } for (int i = 0; i < _widthArray.Length; i++) { if (Convert.ToInt32(_widthArray[i]) <= 0 || Convert.ToInt32(_heightArray[i]) <= 0) { continue; } string _descFile = savePath.TrimEnd('\\') + '\\' + _fileName + "_" + i.ToString() + _fileEx; //判断图片高宽是否大于生成高宽。否则用原图 if (_sourceWidth > Convert.ToInt32(_widthArray[i])) { if (SetCutImage) { ImageHelper.CreateSmallPhoto(_fullPath, Convert.ToInt32(_widthArray[i]), Convert.ToInt32(_heightArray[i]), _descFile); } else { ImageHelper.CreateSmallPhoto(_fullPath, Convert.ToInt32(_widthArray[i]), Convert.ToInt32(_heightArray[i]), _descFile, CutType.CutNo); } } else { if (SetCutImage) { ImageHelper.CreateSmallPhoto(_fullPath, _sourceWidth, _sourceHeight, _descFile); } else { ImageHelper.CreateSmallPhoto(_fullPath, _sourceWidth, _sourceHeight, _descFile, CutType.CutNo); } } } if (!string.IsNullOrEmpty(SetPicWater)) { ImageHelper.AttachPng(SetPicWater, _fullPath, SetWaterPosition.bottomRight); } if (!string.IsNullOrEmpty(SetWordWater)) { ImageHelper.AttachText(SetWordWater, _fullPath); } } catch (Exception ex) { AddUploadImageMessage(_uploadImageMsg, ex.Message); } return(_uploadImageMsg); }