人民医院API

ImageHelper.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Web;
  9. using System.Web.UI.HtmlControls;
  10. namespace RMYY_CallCenter_Api.Utility
  11. {
  12. public class ImageHelper
  13. {
  14. #region 私有成员
  15. private int _Error = 0;//返回上传状态。
  16. private int _MaxSize = 20 * 1024 * 1024;//最大单个上传文件 (默认)
  17. private string _FileType = "jpg;gif;bmp;png";//所支持的上传类型用"/"隔开
  18. private string _SavePath = System.Web.HttpContext.Current.Server.MapPath(".") + "\\";//保存文件的实际路径
  19. private int _SaveType = 0;//上传文件的类型,0代表自动生成文件名
  20. private HtmlInputFile _FormFile;//上传控件。
  21. private string _DataUrl;//base64编码的文本
  22. private string _InFileName = "";//非自动生成文件名设置。
  23. private string _OutFileName = "";//输出文件名。
  24. private bool _IsCreateImg = true;//是否生成缩略图。
  25. private bool _Iss = false;//是否有缩略图生成.
  26. private int _Height = 0;//获取上传图片的高度
  27. private int _Width = 0;//获取上传图片的宽度
  28. private int _sHeight = 120;//设置生成缩略图的高度
  29. private int _sWidth = 120;//设置生成缩略图的宽度
  30. private int _DrawString_x = 10;//绘制文本的X坐标(左上角)
  31. private int _DrawString_y = 10;//绘制文本的Y坐标(左上角)
  32. private int _FileSize = 0;//获取已经上传文件的大小
  33. #endregion
  34. #region 公有属性
  35. /// <summary>
  36. /// Error返回值
  37. /// 1、没有上传的文件
  38. /// 2、类型不允许
  39. /// 3、大小超限
  40. /// 4、未知错误
  41. /// 0、上传成功。
  42. /// </summary>
  43. public int Error
  44. {
  45. get { return _Error; }
  46. }
  47. /// <summary>
  48. /// 最大单个上传文件
  49. /// </summary>
  50. public int MaxSize
  51. {
  52. set { _MaxSize = value; }
  53. }
  54. /// <summary>
  55. /// 所支持的上传类型用";"隔开
  56. /// </summary>
  57. public string FileType
  58. {
  59. set { _FileType = value; }
  60. }
  61. /// <summary>
  62. /// 保存文件的实际路径
  63. /// </summary>
  64. public string SavePath
  65. {
  66. set { _SavePath = System.Web.HttpContext.Current.Server.MapPath(value); }
  67. get { return _SavePath; }
  68. }
  69. /// <summary>
  70. /// 上传文件的类型,0代表自动生成文件名
  71. /// </summary>
  72. public int SaveType
  73. {
  74. set { _SaveType = value; }
  75. }
  76. /// <summary>
  77. /// base64编码的文本
  78. /// </summary>
  79. public string DataUrl
  80. {
  81. set { _DataUrl = value; }
  82. }
  83. /// <summary>
  84. /// 非自动生成文件名设置。
  85. /// </summary>
  86. public string InFileName
  87. {
  88. set { _InFileName = value; }
  89. }
  90. /// <summary>
  91. /// 输出文件名
  92. /// </summary>
  93. public string OutFileName
  94. {
  95. get { return _OutFileName; }
  96. set { _OutFileName = value; }
  97. }
  98. /// <summary>
  99. /// 输出的缩略图文件名
  100. /// </summary>
  101. public string OutThumbFileName
  102. {
  103. get;
  104. set;
  105. }
  106. /// <summary>
  107. /// 是否有缩略图生成.
  108. /// </summary>
  109. public bool Iss
  110. {
  111. get { return _Iss; }
  112. }
  113. /// <summary>
  114. /// 获取上传图片的宽度
  115. /// </summary>
  116. public int Width
  117. {
  118. get { return _Width; }
  119. }
  120. /// <summary>
  121. /// 获取上传图片的高度
  122. /// </summary>
  123. public int Height
  124. {
  125. get { return _Height; }
  126. }
  127. /// <summary>
  128. /// 设置缩略图的宽度
  129. /// </summary>
  130. public int sWidth
  131. {
  132. get { return _sWidth; }
  133. set { _sWidth = value; }
  134. }
  135. /// <summary>
  136. /// 设置缩略图的高度
  137. /// </summary>
  138. public int sHeight
  139. {
  140. get { return _sHeight; }
  141. set { _sHeight = value; }
  142. }
  143. /// <summary>
  144. /// 是否生成缩略图
  145. /// </summary>
  146. public bool IsCreateImg
  147. {
  148. get { return _IsCreateImg; }
  149. set { _IsCreateImg = value; }
  150. }
  151. /// <summary>
  152. /// 绘制文本的X坐标(左上角)
  153. /// </summary>
  154. public int DrawString_x
  155. {
  156. get { return _DrawString_x; }
  157. set { _DrawString_x = value; }
  158. }
  159. /// <summary>
  160. /// 绘制文本的Y坐标(左上角)
  161. /// </summary>
  162. public int DrawString_y
  163. {
  164. get { return _DrawString_y; }
  165. set { _DrawString_y = value; }
  166. }
  167. /// <summary>
  168. /// 文件大小
  169. /// </summary>
  170. public int FileSize
  171. {
  172. get { return _FileSize; }
  173. set { _FileSize = value; }
  174. }
  175. public HttpPostedFileBase PostFile { get; set; }
  176. #endregion
  177. #region 私有方法
  178. /// <summary>
  179. /// 获取文件的后缀名
  180. /// </summary>
  181. private string GetExt(string path)
  182. {
  183. return Path.GetExtension(path);
  184. }
  185. /// <summary>
  186. /// 获取输出文件的文件名
  187. /// </summary>
  188. private string FileName(string Ext)
  189. {
  190. if (_SaveType == 0 || _InFileName.Trim() == "")
  191. return "(" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ")" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + Ext;
  192. else
  193. return "(" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ")" + _InFileName;
  194. }
  195. /// <summary>
  196. /// 检查上传的文件的类型,是否允许上传。
  197. /// </summary>
  198. private bool IsUpload(string Ext)
  199. {
  200. Ext = Ext.Replace(".", "");
  201. bool b = false;
  202. string[] arrFileType = _FileType.Split(';');
  203. foreach (string str in arrFileType)
  204. {
  205. if (str.ToLower() == Ext.ToLower())
  206. {
  207. b = true;
  208. break;
  209. }
  210. }
  211. return b;
  212. }
  213. #endregion
  214. #region 上传图片
  215. public void Upload()
  216. {
  217. HttpPostedFileBase hpFile = PostFile;
  218. if (hpFile == null || hpFile.FileName.Trim() == "")
  219. {
  220. _Error = 1;
  221. return;
  222. }
  223. string Ext = GetExt(hpFile.FileName);
  224. if (!IsUpload(Ext))
  225. {
  226. _Error = 2;
  227. return;
  228. }
  229. int iLen = hpFile.ContentLength;
  230. if (iLen > _MaxSize)
  231. {
  232. _Error = 3;
  233. return;
  234. }
  235. try
  236. {
  237. if (!Directory.Exists(_SavePath)) Directory.CreateDirectory(_SavePath);
  238. byte[] bData = new byte[iLen];
  239. hpFile.InputStream.Read(bData, 0, iLen);
  240. string FName;
  241. FName = FileName(Ext);
  242. string TempFile = FName;
  243. FileStream newFile = new FileStream(_SavePath + TempFile, FileMode.Create);
  244. newFile.Write(bData, 0, bData.Length);
  245. newFile.Flush();
  246. int _FileSizeTemp = hpFile.ContentLength;
  247. string ImageFilePath = _SavePath + FName;
  248. //获取图片的高度和宽度
  249. System.Drawing.Image Img = System.Drawing.Image.FromStream(newFile);
  250. _Width = Img.Width;
  251. _Height = Img.Height;
  252. //生成缩略图部分
  253. if (_IsCreateImg)
  254. {
  255. #region 缩略图大小只设置了最大范围,并不是实际大小
  256. float realbili = (float)_Width / (float)_Height;
  257. float wishbili = (float)_sWidth / (float)_sHeight;
  258. //实际图比缩略图最大尺寸更宽矮,以宽为准
  259. if (realbili > wishbili)
  260. {
  261. _sHeight = (int)((float)_sWidth / realbili);
  262. }
  263. //实际图比缩略图最大尺寸更高长,以高为准
  264. else
  265. {
  266. _sWidth = (int)((float)_sHeight * realbili);
  267. }
  268. #endregion
  269. this.OutThumbFileName = FName.Split('.').GetValue(0).ToString() + "_s." + FName.Split('.').GetValue(1).ToString();
  270. string ImgFilePath = _SavePath + this.OutThumbFileName;
  271. System.Drawing.Image newImg = Img.GetThumbnailImage(_sWidth, _sHeight, null, System.IntPtr.Zero);
  272. newImg.Save(ImgFilePath);
  273. newImg.Dispose();
  274. _Iss = true;
  275. }
  276. newFile.Close();
  277. newFile.Dispose();
  278. _OutFileName = FName;
  279. _FileSize = _FileSizeTemp;
  280. _Error = 0;
  281. return;
  282. }
  283. catch
  284. {
  285. _Error = 4;
  286. return;
  287. }
  288. }
  289. public void Upload64()
  290. {
  291. int delLength = _DataUrl.IndexOf(',') + 1;
  292. string str = _DataUrl.Substring(delLength, _DataUrl.Length - delLength);
  293. str=str.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+");
  294. if (str.Length % 4 > 0)
  295. {
  296. str = str.PadRight(str.Length + 4 - str.Length % 4, '=');
  297. }
  298. byte[] bData = Convert.FromBase64String(str);
  299. int iLen = bData.Length;
  300. if (iLen > _MaxSize)
  301. {
  302. _Error = 3;
  303. return;
  304. }
  305. try
  306. {
  307. if (!Directory.Exists(_SavePath)) Directory.CreateDirectory(_SavePath);
  308. string FName;
  309. FName = FileName(".jpg");
  310. string TempFile = FName;
  311. FileStream newFile = new FileStream(_SavePath + TempFile, FileMode.Create);
  312. newFile.Write(bData, 0, bData.Length);
  313. newFile.Flush();
  314. int _FileSizeTemp = iLen;
  315. string ImageFilePath = _SavePath + FName;
  316. //获取图片的高度和宽度
  317. System.Drawing.Image Img = System.Drawing.Image.FromStream(newFile);
  318. _Width = Img.Width;
  319. _Height = Img.Height;
  320. //生成缩略图部分
  321. if (_IsCreateImg)
  322. {
  323. #region 缩略图大小只设置了最大范围,并不是实际大小
  324. float realbili = (float)_Width / (float)_Height;
  325. float wishbili = (float)_sWidth / (float)_sHeight;
  326. //实际图比缩略图最大尺寸更宽矮,以宽为准
  327. if (realbili > wishbili)
  328. {
  329. _sHeight = (int)((float)_sWidth / realbili);
  330. }
  331. //实际图比缩略图最大尺寸更高长,以高为准
  332. else
  333. {
  334. _sWidth = (int)((float)_sHeight * realbili);
  335. }
  336. #endregion
  337. this.OutThumbFileName = FName.Split('.').GetValue(0).ToString() + "_s." + FName.Split('.').GetValue(1).ToString();
  338. string ImgFilePath = _SavePath + this.OutThumbFileName;
  339. System.Drawing.Image newImg = Img.GetThumbnailImage(_sWidth, _sHeight, null, System.IntPtr.Zero);
  340. newImg.Save(ImgFilePath);
  341. newImg.Dispose();
  342. _Iss = true;
  343. }
  344. newFile.Close();
  345. newFile.Dispose();
  346. _OutFileName = FName;
  347. _FileSize = _FileSizeTemp;
  348. _Error = 0;
  349. return;
  350. }
  351. catch
  352. {
  353. _Error = 4;
  354. return;
  355. }
  356. }
  357. #endregion
  358. }
  359. }