一款非常不错的asp.net图片处理类,自己用的时候需要做相应的修改(水印、剪裁、缩略图)

介绍了一款适用于ASP.NET的图片处理类库,包括如何进行水印添加、图片剪裁以及生成缩略图的操作。
摘要由CSDN通过智能技术生成

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Collections;
using System.Net;
using System.Text.RegularExpressions;
using System.Configuration;
 
namespace SyntacticSugar
{
  /// <summary>
  /// ** 描述:图片上传类、支持水印、缩略图
  /// ** 创始时间:2015-5-28
  /// ** 修改时间:-
  /// ** 修改人:sunkaixuan
  /// </summary>
  public class UploadImage
  {
 
    #region 属性
    /// <summary>
    /// 允许图片格式
    /// </summary>
    public string SetAllowFormat { get; set; }
    /// <summary>
    /// 允许上传图片大小
    /// </summary>
    public double SetAllowSize { get; set; }
    /// <summary>
    /// 文字水印字符
    /// </summary>
    public string SetWordWater { get; set; }
    /// <summary>
    /// 图片水印
    /// </summary>
    public string SetPicWater { get; set; }
    /// <summary>
    /// 水印图片的位置 0居中、1左上角、2右上角、3左下角、4右下角
    /// </summary>
    public int SetPositionWater { get; set; }
    /// <summary>
    /// 缩略图宽度多个逗号格开(例如:200,100)
    /// </summary>
    public string SetSmallImgWidth { get; set; }
    /// <summary>
    /// 缩略图高度多个逗号格开(例如:200,100)
    /// </summary>
    public string SetSmallImgHeight { get; set; }
    /// <summary>
    /// 是否限制最大宽度,默认为true
    /// </summary>
    public bool SetLimitWidth { get; set; }
    /// <summary>
    /// 最大宽度尺寸,默认为600
    /// </summary>
    public int SetMaxWidth { get; set; }
    /// <summary>
    /// 是否剪裁图片,默认true
    /// </summary>
    public bool SetCutImage { get; set; }
    /// <summary>
    /// 限制图片最小宽度,0表示不限制
    /// </summary>
    public int SetMinWidth { get; set; }
 
    #endregion
 
    public UploadImage()
    {
      SetAllowFormat = ".jpeg|.jpg|.bmp|.gif|.png";  //允许图片格式
      SetAllowSize = 1;    //允许上传图片大小,默认为1MB
      SetPositionWater = 4;
      SetCutImage = true;
    }
 
 
 
    #region main method
 
 
    /// <summary>
    /// 裁剪图片
    /// </summary>
    /// <param name="PostedFile">HttpPostedFile控件</param>
    /// <param name="SavePath">保存路径【sys.config配置路径】</param>
    /// <param name="oImgWidth">图片宽度</param>
    /// <param name="oImgHeight">图片高度</param>
    /// <param name="cMode">剪切类型</param>
    /// <param name="ext">返回格式</param>
    /// <returns>返回上传信息</returns>
    public ResponseMessage FileCutSaveAs(System.Web.HttpPostedFile PostedFile, string SavePath, int oImgWidth, int oImgHeight, CutMode cMode)
    {
      ResponseMessage rm = new ResponseMessage();
      try
      {
        //获取上传文件的扩展名
        string sEx = System.IO.Path.GetExtension(PostedFile.FileName);
        if (!CheckValidExt(SetAllowFormat, sEx))
        {
          TryError(rm, 2);
          return rm;
        }
 
        //获取上传文件的大小
        double PostFileSize = PostedFile.ContentLength / 1024.0 / 1024.0;
 
        if (PostFileSize > SetAllowSize)
        {
          TryError(rm, 3);
          return rm; //超过文件上传大小
        }
        if (!System.IO.Directory.Exists(SavePath))
        {
          System.IO.Directory.CreateDirectory(SavePath);
        }
        //重命名名称
        string NewfileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString("000");
        string fName = "s" + NewfileName + sEx;
        string fullPath = Path.Combine(SavePath, fName);
        PostedFile.SaveAs(fullPath);
 
        //重命名名称
        string sNewfileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString("000");
        string sFName = sNewfileName + sEx;
        rm.IsError = false;
        rm.FileName = sFName;
        string sFullPath = Path.Combine(SavePath, sFName);
        rm.filePath = sFullPath;
        rm.WebPath = "/"+sFullPath.Replace(HttpContext.Current.Server.MapPath("~/"), "").Replace("\\", "/");
        CreateSmallPhoto(fullPath, oImgWidth, oImgHeight, sFullPath, SetPicWater, SetWordWater, cMode);
        if (File.Exists(fullPath))
        {
          File.Delete(fullPath);
        }
        //压缩
        if (PostFileSize > 100)
        {
          CompressPhoto(sFullPath, 100);
        }
      }
      catch (Exception ex)
      {
        TryError(rm, ex.Message);
      }
      return rm;
    }
 
 
 
    /// <summary>
    /// 通用图片上传类
    /// </summary>
    /// <param name="PostedFile">HttpPostedFile控件</param>
    /// <param name="SavePath">保存路径【sys.config配置路径】</param>
    /// <param name="finame">返回文件名</param>
    /// <param name="fisize">返回文件大小</param>
    /// <returns>返回上传信息</returns>
    public ResponseMessage FileSaveAs(System.Web.HttpPostedFile PostedFile, string SavePath)
    {
      ResponseMessage rm = new ResponseMessage();
      try
      {
        if (string.IsNullOrEmpty(PostedFile.FileName))
        {
          TryError(rm, 4);
          return rm;
        }
 
        Random rd = new Random();
        int rdInt = rd.Next(1000, 9999);
        //重命名名称
        string NewfileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + rdInt;
 
        //获取上传文件的扩展名
        string sEx = System.IO.Path.GetExtension(PostedFile.FileName);
        if (!CheckValidExt(SetAllowFormat, sEx))
        {
          TryError(rm, 2);
          return rm;
        }
 
        //获取上传文件的大小
        double PostFileSize = PostedFile.ContentLength / 1024.0 / 1024.0;
 
        if (PostFileSize > SetAllowSize)
        {
          TryError(rm, 3);
          return rm;
        }
 
 
        if (!System.IO.Directory.Exists(SavePath))
        {
          System.IO.Directory.CreateDirectory(SavePath);
        }
 
        rm.FileName = NewfileName + sEx;
        string fullPath = SavePath.Trim('\\') + "\\" &
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ice_baili

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值