Brak opisu

ImageUpload.cs 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. using System;
  2. using System.IO;
  3. using System.Web;
  4. using System.Web.UI.HtmlControls;
  5. using System.Drawing;
  6. using System.Net;
  7. using System.Text.RegularExpressions;
  8. using System.IO.Compression;
  9. namespace CallCenter.Utility
  10. {
  11. /// <summary>
  12. /// 文件类型
  13. /// </summary>
  14. public enum FileExtension
  15. {
  16. JPG = 255216,
  17. GIF = 7173,
  18. BMP = 6677,
  19. PNG = 13780,
  20. RAR = 8297,
  21. jpg = 255216,
  22. exe = 7790,
  23. xml = 6063,
  24. html = 6033,
  25. aspx = 239187,
  26. cs = 117115,
  27. js = 119105,
  28. txt = 210187,
  29. sql = 255254
  30. }
  31. /// <summary>
  32. /// 图片检测类
  33. /// </summary>
  34. public static class FileValidation
  35. {
  36. #region 上传图片检测类
  37. /// <summary>
  38. /// 是否允许
  39. /// </summary>
  40. public static bool IsAllowedExtension(HttpPostedFile oFile, FileExtension[] fileEx)
  41. {
  42. int fileLen = oFile.ContentLength;
  43. byte[] imgArray = new byte[fileLen];
  44. oFile.InputStream.Read(imgArray, 0, fileLen);
  45. MemoryStream ms = new MemoryStream(imgArray);
  46. System.IO.BinaryReader br = new System.IO.BinaryReader(ms);
  47. string fileclass = "";
  48. byte buffer;
  49. try
  50. {
  51. buffer = br.ReadByte();
  52. fileclass = buffer.ToString();
  53. buffer = br.ReadByte();
  54. fileclass += buffer.ToString();
  55. }
  56. catch { }
  57. br.Close();
  58. ms.Close();
  59. foreach (FileExtension fe in fileEx)
  60. {
  61. if (Int32.Parse(fileclass) == (int)fe) return true;
  62. }
  63. return false;
  64. }
  65. /// <summary>
  66. /// 上传前的图片是否可靠
  67. /// </summary>
  68. public static bool IsSecureUploadPhoto(HttpPostedFile oFile)
  69. {
  70. bool isPhoto = false;
  71. string fileExtension = System.IO.Path.GetExtension(oFile.FileName).ToLower();
  72. string[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg", ".bmp" };
  73. for (int i = 0; i < allowedExtensions.Length; i++)
  74. {
  75. if (fileExtension == allowedExtensions[i])
  76. {
  77. isPhoto = true;
  78. break;
  79. }
  80. }
  81. if (!isPhoto)
  82. {
  83. return true;
  84. }
  85. FileExtension[] fe = { FileExtension.BMP, FileExtension.GIF, FileExtension.JPG, FileExtension.PNG };
  86. if (IsAllowedExtension(oFile, fe))
  87. return true;
  88. else
  89. return false;
  90. }
  91. /// <summary>
  92. /// 上传后的图片是否安全
  93. /// </summary>
  94. /// <param name="photoFile">物理地址</param>
  95. public static bool IsSecureUpfilePhoto(string photoFile)
  96. {
  97. bool isPhoto = false;
  98. string Img = "Yes";
  99. string fileExtension = System.IO.Path.GetExtension(photoFile).ToLower();
  100. string[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg", ".bmp" };
  101. for (int i = 0; i < allowedExtensions.Length; i++)
  102. {
  103. if (fileExtension == allowedExtensions[i])
  104. {
  105. isPhoto = true;
  106. break;
  107. }
  108. }
  109. if (!isPhoto)
  110. {
  111. return true;
  112. }
  113. StreamReader sr = new StreamReader(photoFile, System.Text.Encoding.Default);
  114. string strContent = sr.ReadToEnd();
  115. sr.Close();
  116. string str = "request|<script|.getfolder|.createfolder|.deletefolder|.createdirectory|.deletedirectory|.saveas|wscript.shell|script.encode|server.|.createobject|execute|activexobject|language=";
  117. foreach (string s in str.Split('|'))
  118. {
  119. if (strContent.ToLower().IndexOf(s) != -1)
  120. {
  121. File.Delete(photoFile);
  122. Img = "No";
  123. break;
  124. }
  125. }
  126. return (Img == "Yes");
  127. }
  128. #endregion
  129. }
  130. /// <summary>
  131. /// 图片上传类
  132. /// </summary>
  133. //----------------调用-------------------
  134. //imageUpload iu = new imageUpload();
  135. //iu.AddText = "";
  136. //iu.CopyIamgePath = "";
  137. //iu.DrawString_x = ;
  138. //iu.DrawString_y = ;
  139. //iu.DrawStyle = ;
  140. //iu.Font = "";
  141. //iu.FontSize = ;
  142. //iu.FormFile = File1;
  143. //iu.IsCreateImg =;
  144. //iu.IsDraw = ;
  145. //iu.OutFileName = "";
  146. //iu.OutThumbFileName = "";
  147. //iu.SavePath = @"~/image/";
  148. //iu.SaveType = ;
  149. //iu.sHeight = ;
  150. //iu.sWidth = ;
  151. //iu.Upload();
  152. //--------------------------------------
  153. public class ImageUpload
  154. {
  155. #region 私有成员
  156. private int _Error = 0;//返回上传状态。
  157. private int _MaxSize = 20 * 1024 * 1024;//最大单个上传文件 (默认)
  158. private string _FileType = "jpg;gif;bmp;png";//所支持的上传类型用"/"隔开
  159. private string _SavePath = System.Web.HttpContext.Current.Server.MapPath(".") + "\\";//保存文件的实际路径
  160. private int _SaveType = 0;//上传文件的类型,0代表自动生成文件名
  161. private HtmlInputFile _FormFile;//上传控件。
  162. private string _DataUrl;//base64编码的文本
  163. private string _InFileName = "";//非自动生成文件名设置。
  164. private string _OutFileName = "";//输出文件名。
  165. private bool _IsCreateImg = true;//是否生成缩略图。
  166. private bool _Iss = false;//是否有缩略图生成.
  167. private int _Height = 0;//获取上传图片的高度
  168. private int _Width = 0;//获取上传图片的宽度
  169. private int _sHeight = 120;//设置生成缩略图的高度
  170. private int _sWidth = 120;//设置生成缩略图的宽度
  171. private bool _IsDraw = false;//设置是否加水印
  172. private int _DrawStyle = 0;//设置加水印的方式0:文字水印模式,1:图片水印模式,2:不加
  173. private int _DrawString_x = 10;//绘制文本的X坐标(左上角)
  174. private int _DrawString_y = 10;//绘制文本的Y坐标(左上角)
  175. private string _AddText = "GlobalNatureCrafts";//设置水印内容
  176. private string _Font = "宋体";//设置水印字体
  177. private int _FontSize = 12;//设置水印字大小
  178. private int _FileSize = 0;//获取已经上传文件的大小
  179. private string _CopyIamgePath = System.Web.HttpContext.Current.Server.MapPath(".") + "/images/5dm_new.jpg";//图片水印模式下的覆盖图片的实际地址
  180. #endregion
  181. #region 公有属性
  182. /// <summary>
  183. /// Error返回值
  184. /// 1、没有上传的文件
  185. /// 2、类型不允许
  186. /// 3、大小超限
  187. /// 4、未知错误
  188. /// 0、上传成功。
  189. /// </summary>
  190. public int Error
  191. {
  192. get { return _Error; }
  193. }
  194. /// <summary>
  195. /// 最大单个上传文件
  196. /// </summary>
  197. public int MaxSize
  198. {
  199. set { _MaxSize = value; }
  200. }
  201. /// <summary>
  202. /// 所支持的上传类型用";"隔开
  203. /// </summary>
  204. public string FileType
  205. {
  206. set { _FileType = value; }
  207. }
  208. /// <summary>
  209. /// 保存文件的实际路径
  210. /// </summary>
  211. public string SavePath
  212. {
  213. set { _SavePath = System.Web.HttpContext.Current.Server.MapPath(value); }
  214. get { return _SavePath; }
  215. }
  216. /// <summary>
  217. /// 上传文件的类型,0代表自动生成文件名
  218. /// </summary>
  219. public int SaveType
  220. {
  221. set { _SaveType = value; }
  222. }
  223. /// <summary>
  224. /// 上传控件
  225. /// </summary>
  226. public HtmlInputFile FormFile
  227. {
  228. set { _FormFile = value; }
  229. }
  230. /// <summary>
  231. /// base64编码的文本
  232. /// </summary>
  233. public string DataUrl
  234. {
  235. set { _DataUrl = value; }
  236. }
  237. /// <summary>
  238. /// 非自动生成文件名设置。
  239. /// </summary>
  240. public string InFileName
  241. {
  242. set { _InFileName = value; }
  243. }
  244. /// <summary>
  245. /// 输出文件名
  246. /// </summary>
  247. public string OutFileName
  248. {
  249. get { return _OutFileName; }
  250. set { _OutFileName = value; }
  251. }
  252. /// <summary>
  253. /// 输出的缩略图文件名
  254. /// </summary>
  255. public string OutThumbFileName
  256. {
  257. get;
  258. set;
  259. }
  260. /// <summary>
  261. /// 是否有缩略图生成.
  262. /// </summary>
  263. public bool Iss
  264. {
  265. get { return _Iss; }
  266. }
  267. /// <summary>
  268. /// 获取上传图片的宽度
  269. /// </summary>
  270. public int Width
  271. {
  272. get { return _Width; }
  273. }
  274. /// <summary>
  275. /// 获取上传图片的高度
  276. /// </summary>
  277. public int Height
  278. {
  279. get { return _Height; }
  280. }
  281. /// <summary>
  282. /// 设置缩略图的宽度
  283. /// </summary>
  284. public int sWidth
  285. {
  286. get { return _sWidth; }
  287. set { _sWidth = value; }
  288. }
  289. /// <summary>
  290. /// 设置缩略图的高度
  291. /// </summary>
  292. public int sHeight
  293. {
  294. get { return _sHeight; }
  295. set { _sHeight = value; }
  296. }
  297. /// <summary>
  298. /// 是否生成缩略图
  299. /// </summary>
  300. public bool IsCreateImg
  301. {
  302. get { return _IsCreateImg; }
  303. set { _IsCreateImg = value; }
  304. }
  305. /// <summary>
  306. /// 是否加水印
  307. /// </summary>
  308. public bool IsDraw
  309. {
  310. get { return _IsDraw; }
  311. set { _IsDraw = value; }
  312. }
  313. /// <summary>
  314. /// 设置加水印的方式
  315. /// 0:文字水印模式
  316. /// 1:图片水印模式
  317. /// 2:不加
  318. /// </summary>
  319. public int DrawStyle
  320. {
  321. get { return _DrawStyle; }
  322. set { _DrawStyle = value; }
  323. }
  324. /// <summary>
  325. /// 绘制文本的X坐标(左上角)
  326. /// </summary>
  327. public int DrawString_x
  328. {
  329. get { return _DrawString_x; }
  330. set { _DrawString_x = value; }
  331. }
  332. /// <summary>
  333. /// 绘制文本的Y坐标(左上角)
  334. /// </summary>
  335. public int DrawString_y
  336. {
  337. get { return _DrawString_y; }
  338. set { _DrawString_y = value; }
  339. }
  340. /// <summary>
  341. /// 设置文字水印内容
  342. /// </summary>
  343. public string AddText
  344. {
  345. get { return _AddText; }
  346. set { _AddText = value; }
  347. }
  348. /// <summary>
  349. /// 设置文字水印字体
  350. /// </summary>
  351. public string Font
  352. {
  353. get { return _Font; }
  354. set { _Font = value; }
  355. }
  356. /// <summary>
  357. /// 设置文字水印字的大小
  358. /// </summary>
  359. public int FontSize
  360. {
  361. get { return _FontSize; }
  362. set { _FontSize = value; }
  363. }
  364. /// <summary>
  365. /// 文件大小
  366. /// </summary>
  367. public int FileSize
  368. {
  369. get { return _FileSize; }
  370. set { _FileSize = value; }
  371. }
  372. /// <summary>
  373. /// 图片水印模式下的覆盖图片的实际地址
  374. /// </summary>
  375. public string CopyIamgePath
  376. {
  377. set { _CopyIamgePath = System.Web.HttpContext.Current.Server.MapPath(value); }
  378. }
  379. public HttpPostedFile PostFile { get; set; }
  380. #endregion
  381. #region 私有方法
  382. /// <summary>
  383. /// 获取文件的后缀名
  384. /// </summary>
  385. private string GetExt(string path)
  386. {
  387. return Path.GetExtension(path);
  388. }
  389. /// <summary>
  390. /// 获取输出文件的文件名
  391. /// </summary>
  392. private string FileName(string Ext)
  393. {
  394. if (_SaveType == 0 || _InFileName.Trim() == "")
  395. return "(" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ")" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + Ext;
  396. else
  397. return "(" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ")" + _InFileName;
  398. }
  399. /// <summary>
  400. /// 检查上传的文件的类型,是否允许上传。
  401. /// </summary>
  402. private bool IsUpload(string Ext)
  403. {
  404. Ext = Ext.Replace(".", "");
  405. bool b = false;
  406. string[] arrFileType = _FileType.Split(';');
  407. foreach (string str in arrFileType)
  408. {
  409. if (str.ToLower() == Ext.ToLower())
  410. {
  411. b = true;
  412. break;
  413. }
  414. }
  415. return b;
  416. }
  417. /// <summary>
  418. /// 检查上传的文件的类型,是否允许上传。
  419. /// </summary>
  420. private bool IsUploadMp3(string Ext)
  421. {
  422. Ext = Ext.Replace(".", "");
  423. bool b = false;
  424. string[] arrFileType = ("mp3").Split(';');
  425. foreach (string str in arrFileType)
  426. {
  427. if (str.ToLower() == Ext.ToLower())
  428. {
  429. b = true;
  430. break;
  431. }
  432. }
  433. return b;
  434. }
  435. private bool IsUploadMp4(string Ext)
  436. {
  437. Ext = Ext.Replace(".", "");
  438. bool b = false;
  439. string[] arrFileType = ("mp4").Split(';');
  440. foreach (string str in arrFileType)
  441. {
  442. if (str.ToLower() == Ext.ToLower())
  443. {
  444. b = true;
  445. break;
  446. }
  447. }
  448. return b;
  449. }
  450. #endregion
  451. #region 上传图片
  452. public void Upload()
  453. {
  454. HttpPostedFile hpFile = PostFile;
  455. if (hpFile == null || hpFile.FileName.Trim() == "")
  456. {
  457. _Error = 1;
  458. return;
  459. }
  460. string Ext = GetExt(hpFile.FileName);
  461. if (!IsUpload(Ext))
  462. {
  463. _Error = 2;
  464. return;
  465. }
  466. int iLen = hpFile.ContentLength;
  467. if (iLen > _MaxSize)
  468. {
  469. _Error = 3;
  470. return;
  471. }
  472. try
  473. {
  474. if (!Directory.Exists(_SavePath)) Directory.CreateDirectory(_SavePath);
  475. byte[] bData = new byte[iLen];
  476. hpFile.InputStream.Read(bData, 0, iLen);
  477. string FName;
  478. FName = FileName(Ext);
  479. string TempFile = "";
  480. if (_IsDraw)
  481. {
  482. TempFile = FName.Split('.').GetValue(0).ToString() + "_temp." + FName.Split('.').GetValue(1).ToString();
  483. }
  484. else
  485. {
  486. TempFile = FName;
  487. }
  488. FileStream newFile = new FileStream(_SavePath + TempFile, FileMode.Create);
  489. newFile.Write(bData, 0, bData.Length);
  490. newFile.Flush();
  491. int _FileSizeTemp = hpFile.ContentLength;
  492. string ImageFilePath = _SavePath + FName;
  493. if (_IsDraw)
  494. {
  495. if (_DrawStyle == 0)
  496. {
  497. System.Drawing.Image Img1 = System.Drawing.Image.FromStream(newFile);
  498. Graphics g = Graphics.FromImage(Img1);
  499. g.DrawImage(Img1, 100, 100, Img1.Width, Img1.Height);
  500. Font f = new Font(_Font, _FontSize);
  501. Brush b = new SolidBrush(Color.Red);
  502. string addtext = _AddText;
  503. g.DrawString(addtext, f, b, _DrawString_x, _DrawString_y);
  504. g.Dispose();
  505. Img1.Save(ImageFilePath);
  506. Img1.Dispose();
  507. }
  508. else
  509. {
  510. System.Drawing.Image image = System.Drawing.Image.FromStream(newFile);
  511. System.Drawing.Image copyImage = System.Drawing.Image.FromFile(_CopyIamgePath);
  512. Graphics g = Graphics.FromImage(image);
  513. g.DrawImage(copyImage, new Rectangle(image.Width - copyImage.Width - 5, image.Height - copyImage.Height - 5, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
  514. g.Dispose();
  515. image.Save(ImageFilePath);
  516. image.Dispose();
  517. }
  518. }
  519. //获取图片的高度和宽度
  520. System.Drawing.Image Img = System.Drawing.Image.FromStream(newFile);
  521. _Width = Img.Width;
  522. _Height = Img.Height;
  523. //生成缩略图部分
  524. if (_IsCreateImg)
  525. {
  526. #region 缩略图大小只设置了最大范围,并不是实际大小
  527. float realbili = (float)_Width / (float)_Height;
  528. float wishbili = (float)_sWidth / (float)_sHeight;
  529. //实际图比缩略图最大尺寸更宽矮,以宽为准
  530. if (realbili > wishbili)
  531. {
  532. _sHeight = (int)((float)_sWidth / realbili);
  533. }
  534. //实际图比缩略图最大尺寸更高长,以高为准
  535. else
  536. {
  537. _sWidth = (int)((float)_sHeight * realbili);
  538. }
  539. #endregion
  540. this.OutThumbFileName = FName.Split('.').GetValue(0).ToString() + "_s." + FName.Split('.').GetValue(1).ToString();
  541. string ImgFilePath = _SavePath + this.OutThumbFileName;
  542. System.Drawing.Image newImg = Img.GetThumbnailImage(_sWidth, _sHeight, null, System.IntPtr.Zero);
  543. newImg.Save(ImgFilePath);
  544. newImg.Dispose();
  545. _Iss = true;
  546. }
  547. if (_IsDraw)
  548. {
  549. if (File.Exists(_SavePath + FName.Split('.').GetValue(0).ToString() + "_temp." + FName.Split('.').GetValue(1).ToString()))
  550. {
  551. newFile.Dispose();
  552. File.Delete(_SavePath + FName.Split('.').GetValue(0).ToString() + "_temp." + FName.Split('.').GetValue(1).ToString());
  553. }
  554. }
  555. newFile.Close();
  556. newFile.Dispose();
  557. _OutFileName = FName;
  558. _FileSize = _FileSizeTemp;
  559. _Error = 0;
  560. return;
  561. }
  562. catch
  563. {
  564. _Error = 4;
  565. return;
  566. }
  567. }
  568. public void Upload64()
  569. {
  570. int delLength = _DataUrl.IndexOf(',') + 1;
  571. string str = _DataUrl.Substring(delLength, _DataUrl.Length - delLength);
  572. byte[] bData = Convert.FromBase64String(str);
  573. int iLen = bData.Length;
  574. if (iLen > _MaxSize)
  575. {
  576. _Error = 3;
  577. return;
  578. }
  579. try
  580. {
  581. if (!Directory.Exists(_SavePath)) Directory.CreateDirectory(_SavePath);
  582. string FName;
  583. FName = FileName(".jpg");
  584. string TempFile = "";
  585. if (_IsDraw)
  586. {
  587. TempFile = FName.Split('.').GetValue(0).ToString() + "_temp." + FName.Split('.').GetValue(1).ToString();
  588. }
  589. else
  590. {
  591. TempFile = FName;
  592. }
  593. FileStream newFile = new FileStream(_SavePath + TempFile, FileMode.Create);
  594. newFile.Write(bData, 0, bData.Length);
  595. newFile.Flush();
  596. int _FileSizeTemp = iLen;
  597. string ImageFilePath = _SavePath + FName;
  598. if (_IsDraw)
  599. {
  600. if (_DrawStyle == 0)
  601. {
  602. System.Drawing.Image Img1 = System.Drawing.Image.FromStream(newFile);
  603. Graphics g = Graphics.FromImage(Img1);
  604. g.DrawImage(Img1, 100, 100, Img1.Width, Img1.Height);
  605. Font f = new Font(_Font, _FontSize);
  606. Brush b = new SolidBrush(Color.Red);
  607. string addtext = _AddText;
  608. g.DrawString(addtext, f, b, _DrawString_x, _DrawString_y);
  609. g.Dispose();
  610. Img1.Save(ImageFilePath);
  611. Img1.Dispose();
  612. }
  613. else
  614. {
  615. System.Drawing.Image image = System.Drawing.Image.FromStream(newFile);
  616. System.Drawing.Image copyImage = System.Drawing.Image.FromFile(_CopyIamgePath);
  617. Graphics g = Graphics.FromImage(image);
  618. g.DrawImage(copyImage, new Rectangle(image.Width - copyImage.Width - 5, image.Height - copyImage.Height - 5, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
  619. g.Dispose();
  620. image.Save(ImageFilePath);
  621. image.Dispose();
  622. }
  623. }
  624. //获取图片的高度和宽度
  625. System.Drawing.Image Img = System.Drawing.Image.FromStream(newFile);
  626. _Width = Img.Width;
  627. _Height = Img.Height;
  628. //生成缩略图部分
  629. if (_IsCreateImg)
  630. {
  631. #region 缩略图大小只设置了最大范围,并不是实际大小
  632. float realbili = (float)_Width / (float)_Height;
  633. float wishbili = (float)_sWidth / (float)_sHeight;
  634. //实际图比缩略图最大尺寸更宽矮,以宽为准
  635. if (realbili > wishbili)
  636. {
  637. _sHeight = (int)((float)_sWidth / realbili);
  638. }
  639. //实际图比缩略图最大尺寸更高长,以高为准
  640. else
  641. {
  642. _sWidth = (int)((float)_sHeight * realbili);
  643. }
  644. #endregion
  645. this.OutThumbFileName = FName.Split('.').GetValue(0).ToString() + "_s." + FName.Split('.').GetValue(1).ToString();
  646. string ImgFilePath = _SavePath + this.OutThumbFileName;
  647. System.Drawing.Image newImg = Img.GetThumbnailImage(_sWidth, _sHeight, null, System.IntPtr.Zero);
  648. newImg.Save(ImgFilePath);
  649. newImg.Dispose();
  650. _Iss = true;
  651. }
  652. if (_IsDraw)
  653. {
  654. if (File.Exists(_SavePath + FName.Split('.').GetValue(0).ToString() + "_temp." + FName.Split('.').GetValue(1).ToString()))
  655. {
  656. newFile.Dispose();
  657. File.Delete(_SavePath + FName.Split('.').GetValue(0).ToString() + "_temp." + FName.Split('.').GetValue(1).ToString());
  658. }
  659. }
  660. newFile.Close();
  661. newFile.Dispose();
  662. _OutFileName = FName;
  663. _FileSize = _FileSizeTemp;
  664. _Error = 0;
  665. return;
  666. }
  667. catch
  668. {
  669. _Error = 4;
  670. return;
  671. }
  672. }
  673. /// <summary>
  674. /// 文件转Base64
  675. /// </summary>
  676. /// <param name="ImageFileName">图片的完整路径</param>
  677. /// <returns></returns>
  678. public string ImgToBase64(string ImageFileName)
  679. {
  680. try
  681. {
  682. WebClient mywebclient = new WebClient();
  683. byte[] Bytes = mywebclient.DownloadData(ImageFileName);
  684. return Convert.ToBase64String(Bytes);
  685. }
  686. catch (Exception ex)
  687. {
  688. return null;
  689. }
  690. }
  691. public bool IsImg(string ext)
  692. {
  693. return IsUpload(ext);
  694. }
  695. public bool IsMp3(string ext)
  696. {
  697. return IsUploadMp3(ext);
  698. }
  699. public bool IsMp4(string ext)
  700. {
  701. return IsUploadMp4(ext);
  702. }
  703. #endregion
  704. #region
  705. public string CompressString(string str)
  706. {
  707. string compressString = "";
  708. byte[] compressBeforeByte = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(str);
  709. byte[] compressAfterByte = Compress(compressBeforeByte);
  710. compressString = Convert.ToBase64String(compressAfterByte);
  711. return compressString;
  712. }
  713. /// <summary>
  714. /// 压缩
  715. /// </summary>
  716. /// <param name="data"></param>
  717. /// <returns></returns>
  718. public byte[] Compress(byte[] data)
  719. {
  720. try
  721. {
  722. MemoryStream ms = new MemoryStream();
  723. GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);
  724. zip.Write(data,0, data.Length);
  725. zip.Close();
  726. byte[] buffer = new byte[ms.Length];
  727. ms.Position =0 ;
  728. ms.Read(buffer,0 , buffer.Length);
  729. ms.Close();
  730. return buffer;
  731. }
  732. catch (Exception e)
  733. {
  734. throw new Exception(e.Message);
  735. }
  736. }
  737. #endregion
  738. #region url下载文件并保存本地
  739. public string downloadurl(string url,string filename)
  740. {
  741. try
  742. {
  743. WebClient mywebclient = new WebClient();
  744. byte[] Bytes = mywebclient.DownloadData(url);
  745. if (!Directory.Exists(_SavePath)) Directory.CreateDirectory(_SavePath);
  746. using (var fs = new FileStream(_SavePath+ filename, FileMode.Create))
  747. {
  748. fs.Write(Bytes, 0, Bytes.Length);
  749. fs.Flush();
  750. return "";
  751. }
  752. }
  753. catch (Exception ex)
  754. {
  755. return ex.Message;
  756. }
  757. }
  758. #endregion
  759. #region base64转音视频文件
  760. /// <summary>
  761. /// Base64转文件
  762. /// </summary>
  763. /// <param name="url"></param>
  764. /// <returns></returns>
  765. public bool Base64ToMP(string strbase,string filepath)//path中需要包含文件名
  766. {
  767. try
  768. {
  769. string outPath = filepath;
  770. var contents = Convert.FromBase64String(strbase);
  771. using (var fs = new FileStream(outPath, FileMode.Create, FileAccess.Write))
  772. {
  773. fs.Write(contents, 0, contents.Length);
  774. fs.Flush();
  775. return true;
  776. }
  777. }
  778. catch(Exception ex)
  779. {
  780. return false;
  781. }
  782. }
  783. #endregion
  784. }
  785. }