博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# Upload
阅读量:5290 次
发布时间:2019-06-14

本文共 7158 字,大约阅读时间需要 23 分钟。

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 上传任何文件}

 

转载于:https://www.cnblogs.com/ilookbo/p/4988453.html

你可能感兴趣的文章
web前端之路,js的一些好书(摘自聂微东 )
查看>>
【模板】对拍程序
查看>>
微信小程序开发初体验
查看>>
dos批处理(bat)运行exe
查看>>
关键字
查看>>
Pycharm安装Markdown插件
查看>>
上传图片并预览
查看>>
哈夫曼编码_静态库
查看>>
【转】redo与undo
查看>>
C#更新程序设计
查看>>
常用Request对象获取请求信息
查看>>
解决升级系统导致的 curl: (48) An unknown option was passed in to libcurl
查看>>
Shell命令-内置命令及其它之watch、date
查看>>
Java Session 介绍;
查看>>
spoj TBATTLE 质因数分解+二分
查看>>
Django 模型层
查看>>
第8章-方法
查看>>
dedecms讲解-arc.listview.class.php分析,列表页展示
查看>>
Microsoft SQL Server Transact-SQL
查看>>
Font: a C++ class
查看>>