private void HanlderSetPicWater(string fullPath, UploadImageInfo uploadImageInfo) { if (uploadImageInfo.IsSetPicWater) { ImageHelper.AttachPng(SetPicWater, fullPath, SetPositionWater); } }
/// <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); }