/// <summary> /// 会产生graphics异常的PixelFormat /// </summary> private static PixelFormat[] indexedPixelFormats = { PixelFormat.Undefined, PixelFormat.DontCare, PixelFormat.Format16bppArgb1555, PixelFormat.Format1bppIndexed, PixelFormat.Format4bppIndexed, PixelFormat.Format8bppIndexed };
public string Upload() { Response.ContentType = "text/plain"; string str1 = Request.Form["str1"]; string str2 = Request.Form["str2"]; string filetype = Request.Form["filetype"]; var obj = RoadFlow.Cache.IO.Opation.Get(str1 ?? ""); if (str1.IsNullOrEmpty() || str2.IsNullOrEmpty() || obj == null || obj.ToString() != str2) { return "您不能上传文件"; } //接收上传后的文件 HttpPostedFileBase file = Request.Files["Filedata"]; if (filetype.IsNullOrEmpty()) { if (!RoadFlow.Utility.Config.UploadFileType.Contains(Path.GetExtension(file.FileName).TrimStart(‘.‘), StringComparison.CurrentCultureIgnoreCase)) { return "您上传的文件类型不被允许"; } } else { if (!filetype.Contains(Path.GetExtension(file.FileName).TrimStart(‘.‘), StringComparison.CurrentCultureIgnoreCase)) { return "您上传的文件类型不被允许"; } } var reg = new Regex(@"^/w+$"); var fname = file.FileName.Split(‘.‘)[0]; if (!reg.IsMatch(fname) && !new Regex(@"^[a-zA-Z0-9_\u4e00-\u9fa5]+$").IsMatch(fname) && !new Regex(@"^[u4e00-u9fa5]+$").IsMatch(fname)) return "上传的文件名只能由数字字母汉字或者下划线组成"; //获取文件的保存路径 string uploadPath; string uploadFullPath = Server.MapPath(getFilePath(out uploadPath)); //判断上传的文件是否为空 if (file != null) { if (!Directory.Exists(uploadFullPath)) { Directory.CreateDirectory(uploadFullPath); } //保存文件 string newFileName = getFileName(uploadFullPath, file.FileName); string newFileFullPath = uploadFullPath + newFileName; try { int fileLength = file.ContentLength; //添加水印 var str = "jpg,png,gif,bmp"; if (str.Contains(Path.GetExtension(file.FileName).TrimStart(‘.‘))) { System.Drawing.Image image = System.Drawing.Image.FromStream(file.InputStream); System.Drawing.Graphics g; System.Drawing.Font f = new System.Drawing.Font("宋体", 15, System.Drawing.FontStyle.Bold); if (IsPixelFormatIndexed(image.PixelFormat)) { Bitmap bmp = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb); using (g = Graphics.FromImage(bmp)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = SmoothingMode.AntiAlias; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(image, 0, 0, image.Width, image.Height); System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Red); PointF pointF = new PointF(image.Width - (RoadFlow.Platform.Users.CurrentUserName.Length * f.Size + 30), image.Height - 30); SizeF sizeF = g.MeasureString(RoadFlow.Platform.Users.CurrentUserName, f); g.FillRectangle(Brushes.Gray, new RectangleF(pointF, sizeF)); g.DrawString(RoadFlow.Platform.Users.CurrentUserName, f, Brushes.Red, pointF); g.DrawString(RoadFlow.Platform.Users.CurrentUserName, f, b, image.Width - (RoadFlow.Platform.Users.CurrentUserName.Length * f.Size + 30), image.Height - 30); } bmp.Save(uploadFullPath + "ShuiYin_" + newFileName); bmp.Dispose(); } else { //g = Graphics.FromImage(image); g = System.Drawing.Graphics.FromImage(image); g.DrawImage(image, 0, 0, image.Width, image.Height); System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Red); PointF pointF = new PointF(image.Width - (RoadFlow.Platform.Users.CurrentUserName.Length * f.Size + 30), image.Height - 30); SizeF sizeF = g.MeasureString(RoadFlow.Platform.Users.CurrentUserName, f); g.FillRectangle(Brushes.Gray, new RectangleF(pointF, sizeF)); g.DrawString(RoadFlow.Platform.Users.CurrentUserName, f, Brushes.Red, pointF); g.DrawString(RoadFlow.Platform.Users.CurrentUserName, f, b, image.Width - (RoadFlow.Platform.Users.CurrentUserName.Length * f.Size + 30), image.Height - 30); g.Dispose(); image.Save(uploadFullPath + "ShuiYin_" + newFileName); image.Dispose(); } //保存原图 file.SaveAs(newFileFullPath); return "1|" + (uploadPath + "ShuiYin_" + newFileName) + "|" + (fileLength / 1000).ToString("###,###") + "|" + "ShuiYin_" + newFileName; } else { file.SaveAs(newFileFullPath); return "1|" + (uploadPath + newFileName) + "|" + (fileLength / 1000).ToString("###,###") + "|" + newFileName; } file.SaveAs(newFileFullPath); return "1|" + (uploadPath + newFileName) + "|" + (fileLength / 1000).ToString("###,###") + "|" + newFileName; } catch { return "上传文件发生了错误"; } } else { return "上传文件为空"; } } /// <summary> /// 得到上传文件名 /// </summary> /// <param name="fileName"></param> /// <returns></returns> private string getFileName(string filePath, string fileName) { while (System.IO.File.Exists(filePath + fileName)) { fileName = Path.GetFileNameWithoutExtension(fileName) + "_" + RoadFlow.Utility.Tools.GetRandomString() + Path.GetExtension(fileName); } return fileName; } /// <summary> /// 得到文件保存路径 /// </summary> /// <returns></returns> private string getFilePath(out string path1) { DateTime date = RoadFlow.Utility.DateTimeNew.Now; path1 = Url.Content("~/Content/UploadFiles/" + date.ToString("yyyyMM") + "/" + date.ToString("dd") + "/"); return path1; } private static bool IsPixelFormatIndexed(PixelFormat imgPixelFormat) { foreach (PixelFormat pf in indexedPixelFormats) { if (pf.Equals(imgPixelFormat)) return true; } return false; }
说明:
1.有时候上传会出现无法添加水印的情况,需要,图片下载下来生成bmp格式的图片才可以添加。
时间: 2024-10-02 20:55:44