using System;using System.Drawing;using System.Drawing.Imaging;using System.IO;using System.Web;using Bo_myCommon;public class Upload{ #region 上传图片 ////// 上传图片 /// /// 字节数组 /// 保存路径。绝对或虚拟路径 /// 图片保存格式 ///上传成功后返回的新的文件名 public static string UploadImage(byte[] imgBuffer, string uploadpath, ImageFormat imgformat) { try { System.IO.MemoryStream m = new MemoryStream(imgBuffer); if (!Directory.Exists(HttpContext.Current.Server.MapPath(uploadpath))) Directory.CreateDirectory(HttpContext.Current.Server.MapPath(uploadpath)); string imgname = StringHelper.CreateIDCode() + "." + imgformat.ToString().ToLower(); string _path = HttpContext.Current.Server.MapPath(uploadpath) + imgname; Image img = System.Drawing.Image.FromStream(m); img.Save(_path, imgformat); m.Close(); return uploadpath + imgname; } catch (Exception ex) { return ex.Message; } } ////// 上传图片 /// /// Stream /// 保存路径。绝对或虚拟路径 /// 图片保存格式 ///上传成功后返回的新的文件名 public static string UploadImage(Stream stream, string uploadpath, ImageFormat imgformat) { try { Image img = Image.FromStream(stream); string filename = StringHelper.CreateIDCode() + "." + imgformat.ToString().ToLower(); filename = HttpContext.Current.Server.MapPath(uploadpath) + filename; img.Save(filename, imgformat); return filename; } catch (Exception ex) { return ex.Message; } } ////// 上传图片 /// /// 客户端上传的文件 /// 保存地址 /// 图片格式 ///public static string UploadImage(HttpPostedFile postfile, string uploadpath, ImageFormat imgformat) { switch (imgformat.ToString().ToLower()) { case "jpeg": return UploadImageForJPEG(postfile, uploadpath); case "bmp": return UploadImageForBMP(postfile, uploadpath); case "png": return UploadImageForPNG(postfile, uploadpath); case "gif": return UploadImageForGIF(postfile, uploadpath); default: return UploadImageForJPEG(postfile, uploadpath); } } /// /// 上传图片,保存为JPEG格式 /// /// HttpPostedFile /// 保存文件地址 ///返回上传后的路径 public static string UploadImage(HttpPostedFile postfile, string uploadpath, bool autoImageName) { if (autoImageName) { switch (Path.GetExtension(postfile.FileName).ToLower()) { case ".jpg": return UploadImageForJPEG(postfile, uploadpath); case ".gif": return UploadImageForGIF(postfile, uploadpath); case ".png": return UploadImageForPNG(postfile, uploadpath); default: return UploadImageForJPEG(postfile, uploadpath); } } else { Image img = Image.FromStream(postfile.InputStream); ImageHelper.ZoomAuto(postfile, uploadpath, img.Width, img.Height, "", "", null); return uploadpath; } } ////// 自动生成新的图片名称 /// /// /// ///public static string UploadImage(HttpPostedFile postfile, string uploadpath) { return UploadImage(postfile, uploadpath, true); } #region 水印 #region 上传图片,不缩放,并添加文字水印 /// /// 上传图片,不缩放,并添加文字水印 /// /// HTTPPOSTEDFILE /// 保存的全路径,包括文件名 /// 水印文字 /// 文字水印字体 public static void UploadImageWithWaterText(HttpPostedFile postedfile, string uploadpath, string text, Font waterTextFont) { Image img = Image.FromStream(postedfile.InputStream); ImageHelper.ZoomAuto(postedfile, uploadpath, img.Width, img.Height, text, "", waterTextFont); } ////// 上传图片,不缩放,并添加文字水印 /// /// HTTPPOSTEDFILE /// 保存的全路径,包括文件名 /// 水印文字 public static void UploadImageWithWaterText(HttpPostedFile postedfile, string uploadpath, string text) { Image img = Image.FromStream(postedfile.InputStream); ImageHelper.ZoomAuto(postedfile, uploadpath, img.Width, img.Height, text, "", null); } #endregion 上传图片,不缩放,并添加文字水印 #region 上传图片,不缩放,并添加图片水印 ////// 上传图片,不缩放,并添加图片水印 /// /// 源图 /// 保存的路径,包含上传后的文件名 /// 水印图片的虚拟路径 public static void UploadImageWithWaterImage(HttpPostedFile postedfile, string uploadpath, string waterimg) { Image img = Image.FromStream(postedfile.InputStream); waterimg = HttpContext.Current.Server.MapPath(waterimg); ImageHelper.ZoomAuto(postedfile, uploadpath, img.Width, img.Height, "", waterimg, null); } #endregion 上传图片,不缩放,并添加图片水印 ////// 图片等比缩放 /// /// 源图 /// 保存路径及文件名 /// 宽度 /// 高度 public static void CutImageAutoZoom(HttpPostedFile postfile, string uploadpath, int width, int height) { ImageHelper.ZoomAuto(postfile, uploadpath, width, height, "", "", null); } #endregion 水印 private static byte[] GetPostFileByte(HttpPostedFile postfile) { int filelength = postfile.ContentLength; byte[] buffer = new byte[filelength]; postfile.InputStream.Read(buffer, 0, filelength); return buffer; } private static string UploadImageForBMP(HttpPostedFile postfile, string uploadpath) { byte[] buffer = GetPostFileByte(postfile); return UploadImage(buffer, uploadpath, ImageFormat.Bmp); } private static string UploadImageForGIF(HttpPostedFile postfile, string uploadpath) { byte[] buffer = GetPostFileByte(postfile); return UploadImage(buffer, uploadpath, ImageFormat.Gif); } private static string UploadImageForJPEG(HttpPostedFile postfile, string uploadpath) { byte[] buffer = GetPostFileByte(postfile); return UploadImage(buffer, uploadpath, ImageFormat.Jpeg); } private static string UploadImageForPNG(HttpPostedFile postfile, string uploadpath) { byte[] buffer = GetPostFileByte(postfile); return UploadImage(buffer, uploadpath, ImageFormat.Png); } #endregion 上传图片 #region 上传任何文件 ////// 上传文件 /// /// 上传的原始文件 /// 保存地址,如:'/upload/images/aaaa.jpg' ///返回上传后的文件名 public static string UploadFile(HttpPostedFile postfile, string uploadpath) { try { string savepath = HttpContext.Current.Server.MapPath(uploadpath); if (!Directory.Exists(uploadpath)) Directory.CreateDirectory(uploadpath); string ext = Path.GetExtension(postfile.FileName); string filename = StringHelper.CreateIDCode() + ext; if (uploadpath.IndexOf(ext) == -1) //判断 { savepath = savepath + filename; } postfile.SaveAs(savepath); return uploadpath + filename; } catch (Exception ex) { return ex.Message; } } #endregion 上传任何文件}