using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using Bo_myCommon; public class Upload
{
#region 上传图片 /// <summary>
/// 上传图片
/// </summary>
/// <param name="imgBuffer">字节数组</param>
/// <param name="uploadpath">保存路径。绝对或虚拟路径</param>
/// <param name="imgformat">图片保存格式</param>
/// <returns>上传成功后返回的新的文件名</returns>
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;
}
} /// <summary>
/// 上传图片
/// </summary>
/// <param name="stream">Stream</param>
/// <param name="uploadpath">保存路径。绝对或虚拟路径</param>
/// <param name="imgformat">图片保存格式</param>
/// <returns>上传成功后返回的新的文件名</returns>
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;
}
} /// <summary>
/// 上传图片
/// </summary>
/// <param name="postfile">客户端上传的文件</param>
/// <param name="uploadpath">保存地址</param>
/// <param name="imgformat">图片格式</param>
/// <returns></returns>
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);
}
} /// <summary>
/// 上传图片,保存为JPEG格式
/// </summary>
/// <param name="postfile">HttpPostedFile</param>
/// <param name="uploadpath">保存文件地址</param>
/// <returns>返回上传后的路径</returns>
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;
}
} /// <summary>
/// 自动生成新的图片名称
/// </summary>
/// <param name="postfile"></param>
/// <param name="uploadpath"></param>
/// <returns></returns>
public static string UploadImage(HttpPostedFile postfile, string uploadpath)
{
return UploadImage(postfile, uploadpath, true);
} #region 水印 #region 上传图片,不缩放,并添加文字水印 /// <summary>
/// 上传图片,不缩放,并添加文字水印
/// </summary>
/// <param name="postedfile">HTTPPOSTEDFILE</param>
/// <param name="uploadpath">保存的全路径,包括文件名</param>
/// <param name="text">水印文字</param>
/// <param name="waterTextFont">文字水印字体</param>
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);
} /// <summary>
/// 上传图片,不缩放,并添加文字水印
/// </summary>
/// <param name="postedfile">HTTPPOSTEDFILE</param>
/// <param name="uploadpath">保存的全路径,包括文件名</param>
/// <param name="text">水印文字</param>
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 上传图片,不缩放,并添加图片水印 /// <summary>
/// 上传图片,不缩放,并添加图片水印
/// </summary>
/// <param name="postedfile">源图</param>
/// <param name="uploadpath">保存的路径,包含上传后的文件名</param>
/// <param name="waterimg">水印图片的虚拟路径</param>
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 上传图片,不缩放,并添加图片水印 /// <summary>
/// 图片等比缩放
/// </summary>
/// <param name="postfile">源图</param>
/// <param name="uploadpath">保存路径及文件名</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
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, , 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 上传任何文件 /// <summary>
/// 上传文件
/// </summary>
/// <param name="postfile">上传的原始文件</param>
/// <param name="uploadpath">保存地址,如:'/upload/images/aaaa.jpg'</param>
/// <returns>返回上传后的文件名</returns>
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) == -) //判断
{
savepath = savepath + filename;
}
postfile.SaveAs(savepath);
return uploadpath + filename;
}
catch (Exception ex)
{
return ex.Message;
}
} #endregion 上传任何文件
}

最新文章

  1. URL和搜索引擎优化
  2. 金融系列4《PUTKEY指令》
  3. Android EditText多行显示及所有属性
  4. 2句代码轻松实现WPF最大化不遮挡任务栏并且具有边框调节效果
  5. 调用父类Controller错误
  6. magento数据查询
  7. 【Android】定位与解决anr错误记录
  8. Spring Boot 参数校验
  9. Windows 下Redis的部署 及key 过期事件
  10. springboot无法加载oracle驱动终极解决
  11. playbook role应用
  12. vue-8-组件
  13. 判断listview滑动方向的代码片段
  14. &lt;构建之法&gt;阅读感想
  15. TextView子类的常用属性
  16. Git-Svn 建立工作目录
  17. ASP.NET MVC 多语言解决方案
  18. 多线程-join()方法
  19. dp4--codeVs1043 方格取数
  20. 分享知识-快乐自己:PageHelper 分页关键基础代码

热门文章

  1. 爬虫框架之Scrapy
  2. string标准C++中的的用法总结(转)
  3. 添加按钮 table增加一行 删减按钮 table去掉一行
  4. laravel 服务提供者
  5. shell 脚本加密
  6. C/C++返回内部静态成员的陷阱(转)
  7. sqoop的基本语法详解及可能遇到的错误
  8. 设置git记住用户和密码
  9. snmp v3的安全配置 snmp认证与加密配置(53)
  10. ASP.NET CORE 配置管理