国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ASP.NET 圖片上傳工具類 upload image簡單好用功能齊全

2019-11-17 02:07:47
字體:
來源:轉載
供稿:網友

asp.net 圖片上傳工具類 upload image簡單好用功能齊全

使用方法:

  UploadImage ui = new UploadImage();            /***可選參數***/            ui.SetWordWater = "哈哈";//文字水印            // ui.SetPicWater = Server.MapPath("2.png");//圖片水印(圖片和文字都賦值圖片有效)            ui.SetPositionWater = 4;//水印圖片的位置 0居中、1左上角、2右上角、3左下角、4右下角            ui.SetSmallImgHeight = "110,40,20";//設置縮略圖可以多個            ui.SetSmallImgWidth = "100,40,20";            //保存圖片生成縮略圖            var reponseMessage = ui.FileSaveAs(Request.Files[0], Server.MapPath("~/file/temp"));            //裁剪圖片            var reponseMessage2 = ui.FileCutSaveAs(Request.Files[0], Server.MapPath("~/file/temp2"), 400, 300, UploadImage.CutMode.CutNo);            /***返回信息***/            var isError = reponseMessage.IsError;//是否異常            var webPath = reponseMessage.WebPath;//web路徑            var filePath = reponseMessage.filePath;//物理路徑            var message = reponseMessage.Message;//錯誤信息            var directory = reponseMessage.Directory;//目錄            var smallPath1 = reponseMessage.SmallPath(0);//縮略圖路徑1            var smallPath2 = reponseMessage.SmallPath(1);//縮略圖路徑2            var smallPath3 = reponseMessage.SmallPath(2);//縮略圖路徑3

  

效果:

源碼

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    /// ** 使用說明:http://www.survivalescaperooms.com/sunkaixuan/p/4536626.html    /// </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("http://", "/");                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;
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄陵县| 东乌珠穆沁旗| 金寨县| 句容市| 托克逊县| 商南县| 平乡县| 昭平县| 红河县| 延寿县| 饶平县| 喜德县| 凤山县| 济南市| 长治县| 镇巴县| 宣化县| 辽阳县| 称多县| 连州市| 抚远县| 凤山县| 乐山市| 平安县| 东阳市| 昌邑市| 汶上县| 桃江县| 华池县| 吉木萨尔县| 阿坝县| 慈利县| 高阳县| 丰镇市| 库伦旗| 惠安县| 安阳县| 名山县| 广南县| 安多县| 永善县|