| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web;
- using System.Web.UI.HtmlControls;
- namespace RMYY_CallCenter_Api.Utility
- {
- public class ImageHelper
- {
- #region 私有成员
- private int _Error = 0;//返回上传状态。
- private int _MaxSize = 20 * 1024 * 1024;//最大单个上传文件 (默认)
- private string _FileType = "jpg;gif;bmp;png";//所支持的上传类型用"/"隔开
- private string _SavePath = System.Web.HttpContext.Current.Server.MapPath(".") + "\\";//保存文件的实际路径
- private int _SaveType = 0;//上传文件的类型,0代表自动生成文件名
- private HtmlInputFile _FormFile;//上传控件。
- private string _DataUrl;//base64编码的文本
- private string _InFileName = "";//非自动生成文件名设置。
- private string _OutFileName = "";//输出文件名。
- private bool _IsCreateImg = true;//是否生成缩略图。
- private bool _Iss = false;//是否有缩略图生成.
- private int _Height = 0;//获取上传图片的高度
- private int _Width = 0;//获取上传图片的宽度
- private int _sHeight = 120;//设置生成缩略图的高度
- private int _sWidth = 120;//设置生成缩略图的宽度
- private int _DrawString_x = 10;//绘制文本的X坐标(左上角)
- private int _DrawString_y = 10;//绘制文本的Y坐标(左上角)
- private int _FileSize = 0;//获取已经上传文件的大小
- #endregion
- #region 公有属性
- /// <summary>
- /// Error返回值
- /// 1、没有上传的文件
- /// 2、类型不允许
- /// 3、大小超限
- /// 4、未知错误
- /// 0、上传成功。
- /// </summary>
- public int Error
- {
- get { return _Error; }
- }
- /// <summary>
- /// 最大单个上传文件
- /// </summary>
- public int MaxSize
- {
- set { _MaxSize = value; }
- }
- /// <summary>
- /// 所支持的上传类型用";"隔开
- /// </summary>
- public string FileType
- {
- set { _FileType = value; }
- }
- /// <summary>
- /// 保存文件的实际路径
- /// </summary>
- public string SavePath
- {
- set { _SavePath = System.Web.HttpContext.Current.Server.MapPath(value); }
- get { return _SavePath; }
- }
- /// <summary>
- /// 上传文件的类型,0代表自动生成文件名
- /// </summary>
- public int SaveType
- {
- set { _SaveType = value; }
- }
- /// <summary>
- /// base64编码的文本
- /// </summary>
- public string DataUrl
- {
- set { _DataUrl = value; }
- }
- /// <summary>
- /// 非自动生成文件名设置。
- /// </summary>
- public string InFileName
- {
- set { _InFileName = value; }
- }
- /// <summary>
- /// 输出文件名
- /// </summary>
- public string OutFileName
- {
- get { return _OutFileName; }
- set { _OutFileName = value; }
- }
- /// <summary>
- /// 输出的缩略图文件名
- /// </summary>
- public string OutThumbFileName
- {
- get;
- set;
- }
- /// <summary>
- /// 是否有缩略图生成.
- /// </summary>
- public bool Iss
- {
- get { return _Iss; }
- }
- /// <summary>
- /// 获取上传图片的宽度
- /// </summary>
- public int Width
- {
- get { return _Width; }
- }
- /// <summary>
- /// 获取上传图片的高度
- /// </summary>
- public int Height
- {
- get { return _Height; }
- }
- /// <summary>
- /// 设置缩略图的宽度
- /// </summary>
- public int sWidth
- {
- get { return _sWidth; }
- set { _sWidth = value; }
- }
- /// <summary>
- /// 设置缩略图的高度
- /// </summary>
- public int sHeight
- {
- get { return _sHeight; }
- set { _sHeight = value; }
- }
- /// <summary>
- /// 是否生成缩略图
- /// </summary>
- public bool IsCreateImg
- {
- get { return _IsCreateImg; }
- set { _IsCreateImg = value; }
- }
- /// <summary>
- /// 绘制文本的X坐标(左上角)
- /// </summary>
- public int DrawString_x
- {
- get { return _DrawString_x; }
- set { _DrawString_x = value; }
- }
- /// <summary>
- /// 绘制文本的Y坐标(左上角)
- /// </summary>
- public int DrawString_y
- {
- get { return _DrawString_y; }
- set { _DrawString_y = value; }
- }
- /// <summary>
- /// 文件大小
- /// </summary>
- public int FileSize
- {
- get { return _FileSize; }
- set { _FileSize = value; }
- }
- public HttpPostedFileBase PostFile { get; set; }
- #endregion
- #region 私有方法
- /// <summary>
- /// 获取文件的后缀名
- /// </summary>
- private string GetExt(string path)
- {
- return Path.GetExtension(path);
- }
- /// <summary>
- /// 获取输出文件的文件名
- /// </summary>
- private string FileName(string Ext)
- {
- if (_SaveType == 0 || _InFileName.Trim() == "")
- return "(" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ")" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + Ext;
- else
- return "(" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ")" + _InFileName;
- }
- /// <summary>
- /// 检查上传的文件的类型,是否允许上传。
- /// </summary>
- private bool IsUpload(string Ext)
- {
- Ext = Ext.Replace(".", "");
- bool b = false;
- string[] arrFileType = _FileType.Split(';');
- foreach (string str in arrFileType)
- {
- if (str.ToLower() == Ext.ToLower())
- {
- b = true;
- break;
- }
- }
- return b;
- }
- #endregion
- #region 上传图片
- public void Upload()
- {
- HttpPostedFileBase hpFile = PostFile;
- if (hpFile == null || hpFile.FileName.Trim() == "")
- {
- _Error = 1;
- return;
- }
- string Ext = GetExt(hpFile.FileName);
- if (!IsUpload(Ext))
- {
- _Error = 2;
- return;
- }
- int iLen = hpFile.ContentLength;
- if (iLen > _MaxSize)
- {
- _Error = 3;
- return;
- }
- try
- {
- if (!Directory.Exists(_SavePath)) Directory.CreateDirectory(_SavePath);
- byte[] bData = new byte[iLen];
- hpFile.InputStream.Read(bData, 0, iLen);
- string FName;
- FName = FileName(Ext);
- string TempFile = FName;
- FileStream newFile = new FileStream(_SavePath + TempFile, FileMode.Create);
- newFile.Write(bData, 0, bData.Length);
- newFile.Flush();
- int _FileSizeTemp = hpFile.ContentLength;
- string ImageFilePath = _SavePath + FName;
- //获取图片的高度和宽度
- System.Drawing.Image Img = System.Drawing.Image.FromStream(newFile);
- _Width = Img.Width;
- _Height = Img.Height;
- //生成缩略图部分
- if (_IsCreateImg)
- {
- #region 缩略图大小只设置了最大范围,并不是实际大小
- float realbili = (float)_Width / (float)_Height;
- float wishbili = (float)_sWidth / (float)_sHeight;
- //实际图比缩略图最大尺寸更宽矮,以宽为准
- if (realbili > wishbili)
- {
- _sHeight = (int)((float)_sWidth / realbili);
- }
- //实际图比缩略图最大尺寸更高长,以高为准
- else
- {
- _sWidth = (int)((float)_sHeight * realbili);
- }
- #endregion
- this.OutThumbFileName = FName.Split('.').GetValue(0).ToString() + "_s." + FName.Split('.').GetValue(1).ToString();
- string ImgFilePath = _SavePath + this.OutThumbFileName;
- System.Drawing.Image newImg = Img.GetThumbnailImage(_sWidth, _sHeight, null, System.IntPtr.Zero);
- newImg.Save(ImgFilePath);
- newImg.Dispose();
- _Iss = true;
- }
- newFile.Close();
- newFile.Dispose();
- _OutFileName = FName;
- _FileSize = _FileSizeTemp;
- _Error = 0;
- return;
- }
- catch
- {
- _Error = 4;
- return;
- }
- }
- public void Upload64()
- {
- int delLength = _DataUrl.IndexOf(',') + 1;
- string str = _DataUrl.Substring(delLength, _DataUrl.Length - delLength);
- str=str.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+");
- if (str.Length % 4 > 0)
- {
- str = str.PadRight(str.Length + 4 - str.Length % 4, '=');
- }
- byte[] bData = Convert.FromBase64String(str);
- int iLen = bData.Length;
- if (iLen > _MaxSize)
- {
- _Error = 3;
- return;
- }
- try
- {
- if (!Directory.Exists(_SavePath)) Directory.CreateDirectory(_SavePath);
- string FName;
- FName = FileName(".jpg");
- string TempFile = FName;
- FileStream newFile = new FileStream(_SavePath + TempFile, FileMode.Create);
- newFile.Write(bData, 0, bData.Length);
- newFile.Flush();
- int _FileSizeTemp = iLen;
- string ImageFilePath = _SavePath + FName;
- //获取图片的高度和宽度
- System.Drawing.Image Img = System.Drawing.Image.FromStream(newFile);
- _Width = Img.Width;
- _Height = Img.Height;
- //生成缩略图部分
- if (_IsCreateImg)
- {
- #region 缩略图大小只设置了最大范围,并不是实际大小
- float realbili = (float)_Width / (float)_Height;
- float wishbili = (float)_sWidth / (float)_sHeight;
- //实际图比缩略图最大尺寸更宽矮,以宽为准
- if (realbili > wishbili)
- {
- _sHeight = (int)((float)_sWidth / realbili);
- }
- //实际图比缩略图最大尺寸更高长,以高为准
- else
- {
- _sWidth = (int)((float)_sHeight * realbili);
- }
- #endregion
- this.OutThumbFileName = FName.Split('.').GetValue(0).ToString() + "_s." + FName.Split('.').GetValue(1).ToString();
- string ImgFilePath = _SavePath + this.OutThumbFileName;
- System.Drawing.Image newImg = Img.GetThumbnailImage(_sWidth, _sHeight, null, System.IntPtr.Zero);
- newImg.Save(ImgFilePath);
- newImg.Dispose();
- _Iss = true;
- }
- newFile.Close();
- newFile.Dispose();
- _OutFileName = FName;
- _FileSize = _FileSizeTemp;
- _Error = 0;
- return;
- }
- catch
- {
- _Error = 4;
- return;
- }
- }
- #endregion
- }
- }
|