UploadController.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using AirWheel.Cycling.Domain;
  7. using AirWheel.Cycling.Common;
  8. using AirWheel.Cycling.Service.IService;
  9. using System.Drawing;
  10. using System.Drawing.Drawing2D;
  11. using System.Drawing.Imaging;
  12. using System.IO;
  13. using System.Net;
  14. using System.Configuration;
  15. using System.Collections;
  16. using AirWheel.Cycling.WebPage.Controllers;
  17. namespace AirWheel.Cycling.WebPage.Areas.ComManage.Controllers
  18. {
  19. /// <summary>
  20. /// 文件上传控制器
  21. /// add 作者: 季健国 QQ:181589805 by 2016-10-14
  22. /// </summary>
  23. public class UploadController : BaseController
  24. {
  25. #region 声明容器
  26. /// <summary>
  27. /// 文章管理
  28. /// </summary>
  29. IUploadManage UploadManage { get; set; }
  30. #endregion
  31. #region 公共变量
  32. #endregion
  33. #region 基本视图
  34. /// <summary>
  35. /// 加载首页
  36. /// </summary>
  37. [UserAuthorizeAttribute(ModuleAlias = "Upload", OperaAction = "View")]
  38. public ActionResult Index()
  39. {
  40. ViewBag.Search = base.keywords;
  41. return View(BindList());
  42. }
  43. /// <summary>
  44. /// 加载详情
  45. /// </summary>
  46. [UserAuthorizeAttribute(ModuleAlias = "Upload", OperaAction = "Detail")]
  47. public ActionResult Detail(string id)
  48. {
  49. var entity = this.UploadManage.Get(p => p.ID == id) ?? new COM_UPLOAD();
  50. return View(entity);
  51. }
  52. #endregion
  53. #region 公共方法
  54. /// <summary>
  55. /// 绑定列表数据
  56. /// </summary>
  57. private PageInfo<COM_UPLOAD> BindList()
  58. {
  59. var query = this.UploadManage.LoadAll(null);
  60. if (!string.IsNullOrEmpty(keywords))
  61. {
  62. query = query.Where(p => p.UPOLDNAME.Contains(keywords));
  63. }
  64. //除管理员,其他人默认看自己的文件
  65. if (!this.CurrentUser.IsAdmin)
  66. {
  67. var userid = CurrentUser.Id.ToString();
  68. query = query.Where(p => p.FK_USERID == userid);
  69. }
  70. query = query.OrderByDescending(p => p.UPTIME);
  71. return this.UploadManage.Query(query, base.page, base.pagesize);
  72. }
  73. #endregion
  74. #region 私有方法
  75. /// <summary>
  76. /// 返回上传目录相对路径
  77. /// </summary>
  78. /// <param name="fileName">上传文件名</param>
  79. private string GetUpLoadPath(bool isImg, string ext)
  80. {
  81. string path = ConfigurationManager.AppSettings["uppath"];
  82. if (isImg && IsImage(ext))
  83. {
  84. path += "images/";
  85. }
  86. else
  87. {
  88. path += "files/";
  89. }
  90. #region 用户控制
  91. //判读是否为超级管理员,超级管理员可查看所有的文件
  92. if (!base.CurrentUser.IsAdmin)
  93. {
  94. path += base.CurrentUser.LogName + "/";
  95. }
  96. #endregion
  97. path += DateTime.Now.ToString("yyyyMMdd") + "/";
  98. return path;
  99. }
  100. /// <summary>
  101. /// 是否为图片文件
  102. /// </summary>
  103. /// <param name="_fileExt">文件扩展名,不含“.”</param>
  104. private bool IsImage(string _fileExt)
  105. {
  106. ArrayList al = new ArrayList();
  107. al.Add("bmp");
  108. al.Add("jpeg");
  109. al.Add("jpg");
  110. al.Add("gif");
  111. al.Add("png");
  112. if (al.Contains(_fileExt.ToLower()))
  113. return true;
  114. return false;
  115. }
  116. /// <summary>
  117. /// 是否需要打水印
  118. /// </summary>
  119. /// <param name="_fileExt">文件扩展名,不含“.”</param>
  120. private bool IsWaterMark(string _fileExt)
  121. {
  122. //判断是否开启水印
  123. if (ConfigurationManager.AppSettings["watermarktype"].ToString() != "0")
  124. {
  125. //判断是否可以打水印的图片类型
  126. ArrayList al = new ArrayList();
  127. al.Add("bmp");
  128. al.Add("jpeg");
  129. al.Add("jpg");
  130. al.Add("png");
  131. if (al.Contains(_fileExt.ToLower()))
  132. {
  133. return true;
  134. }
  135. }
  136. return false;
  137. }
  138. /// <summary>
  139. /// 检查是否为合法的上传文件
  140. /// </summary>
  141. private bool CheckFileExt(string _fileExt)
  142. {
  143. //检查危险文件
  144. string[] excExt = { "asp", "aspx", "php", "jsp", "htm", "html" };
  145. for (int i = 0; i < excExt.Length; i++)
  146. {
  147. if (excExt[i].ToLower() == _fileExt.ToLower())
  148. {
  149. return false;
  150. }
  151. }
  152. //检查合法文件
  153. string[] allowExt = ConfigurationManager.AppSettings["attachextension"].ToString().Split(',');
  154. for (int i = 0; i < allowExt.Length; i++)
  155. {
  156. if (allowExt[i].ToLower() == _fileExt.ToLower())
  157. {
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. /// <summary>
  164. /// 检查文件大小是否合法
  165. /// </summary>
  166. /// <param name="_fileExt">文件扩展名,不含“.”</param>
  167. /// <param name="_fileSize">文件大小(B)</param>
  168. private bool CheckFileSize(string _fileExt, int _fileSize)
  169. {
  170. //判断是否为图片文件
  171. if (IsImage(_fileExt))
  172. {
  173. if (_fileSize > int.Parse(ConfigurationManager.AppSettings["attachimgsize"].ToString()) * 1024)
  174. return false;
  175. }
  176. else
  177. {
  178. if (_fileSize > int.Parse(ConfigurationManager.AppSettings["attachfilesize"].ToString()) * 1024)
  179. return false;
  180. }
  181. return true;
  182. }
  183. /// <summary>
  184. /// 是否真实,入侵验证
  185. /// </summary>
  186. /// <param name="_fileBinary">文件post</param>
  187. /// <param name="newfilePath">新文件路径+文件名</param>
  188. /// <returns></returns>
  189. private bool CheckFileTrue(HttpPostedFileBase _fileBinary, string newfilePath)
  190. {
  191. Stream fs = _fileBinary.InputStream;
  192. BinaryReader br = new BinaryReader(fs);
  193. byte but;
  194. string str = "";
  195. try
  196. {
  197. but = br.ReadByte();
  198. str = but.ToString();
  199. but = br.ReadByte();
  200. str += but.ToString();
  201. }
  202. catch
  203. {
  204. return false;
  205. }
  206. /*文件扩展名说明
  207. * 4946/104116 txt
  208. * 7173 gif
  209. * 255216 jpg
  210. * 13780 png
  211. * 6677 bmp
  212. * 239187 txt,aspx,asp,sql
  213. * 208207 xls.doc.ppt
  214. * 6063 xml
  215. * 6033 htm,html
  216. * 4742 js
  217. * 8075 xlsx,zip,pptx,mmap,zip
  218. * 8297 rar
  219. * 01 accdb,mdb
  220. * 7790 exe,dll
  221. * 5666 psd
  222. * 255254 rdp
  223. * 10056 bt种子
  224. * 64101 bat
  225. * 4059 sgf
  226. * 3780 pdf
  227. */
  228. bool bl = false;
  229. string[] binary = { "4946", "104116", "7173", "255216", "13780", "6677", "239187", "208207", "6063", "6033", "4742", "8075", "8297", "01", "5666", "255254", "10056" };
  230. for (int i = 0; i < binary.Length; i++)
  231. {
  232. if (binary[i].ToLower() == str.ToLower())
  233. {
  234. bl = true;
  235. }
  236. }
  237. if (bl)
  238. {
  239. _fileBinary.SaveAs(newfilePath);
  240. }
  241. br.Close();
  242. fs.Close();
  243. return bl;
  244. }
  245. /// <summary>
  246. /// 文件上传方法
  247. /// </summary>
  248. /// <param name="postedFile">文件流</param>
  249. /// <param name="isThumbnail">是否生成缩略图</param>
  250. /// <returns>上传后文件信息</returns>
  251. private JsonHelper FileSaveAs(HttpPostedFileBase postedFile, bool isThumbnail, bool isWater, bool isImg)
  252. {
  253. var jsons = new JsonHelper { Status = "n" };
  254. try
  255. {
  256. string fileExt = Utils.GetFileExt(postedFile.FileName); //文件扩展名,不含“.”
  257. int fileSize = postedFile.ContentLength; //获得文件大小,以字节为单位
  258. string fileName = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(@"\") + 1); //取得原文件名
  259. string newFileName = Utils.GetRamCode() + "." + fileExt; //随机生成新的文件名
  260. string upLoadPath = GetUpLoadPath(isImg, fileExt); //上传目录相对路径
  261. string fullUpLoadPath = Utils.GetMapPath(upLoadPath); //上传目录的物理路径
  262. string newFilePath = upLoadPath + newFileName; //上传后的路径
  263. string newThumbnailFileName = "thumb_" + newFileName; //随机生成缩略图文件名
  264. //检查文件扩展名是否合法
  265. if (!CheckFileExt(fileExt))
  266. {
  267. jsons.Msg = "不允许上传" + fileExt + "类型的文件!";
  268. return jsons;
  269. }
  270. //检查文件大小是否合法
  271. if (!CheckFileSize(fileExt, fileSize))
  272. {
  273. jsons.Msg = "文件超过限制的大小啦!";
  274. return jsons;
  275. }
  276. //检查上传的物理路径是否存在,不存在则创建
  277. if (!Directory.Exists(fullUpLoadPath))
  278. {
  279. Directory.CreateDirectory(fullUpLoadPath);
  280. }
  281. //检查文件是否真实合法
  282. //if (!CheckFileTrue(postedFile, fullUpLoadPath + newFileName))
  283. //{
  284. // jsons.Msg = "不允许上传不可识别的文件!";
  285. // return jsons;
  286. //}
  287. postedFile.SaveAs(fullUpLoadPath + newFileName);
  288. string thumbnail = string.Empty;
  289. //如果是图片,检查是否需要生成缩略图,是则生成
  290. if (IsImage(fileExt) && isThumbnail && ConfigurationManager.AppSettings["thumbnailwidth"].ToString() != "0" && ConfigurationManager.AppSettings["thumbnailheight"].ToString() != "0")
  291. {
  292. Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newThumbnailFileName,
  293. int.Parse(ConfigurationManager.AppSettings["thumbnailwidth"]), int.Parse(ConfigurationManager.AppSettings["thumbnailheight"]), "Cut");
  294. thumbnail = upLoadPath + newThumbnailFileName;
  295. }
  296. //如果是图片,检查是否需要打水印
  297. if (IsWaterMark(fileExt) && isWater)
  298. {
  299. switch (ConfigurationManager.AppSettings["watermarktype"].ToString())
  300. {
  301. case "1":
  302. WaterMark.AddImageSignText(newFilePath, newFilePath,
  303. ConfigurationManager.AppSettings["watermarktext"], int.Parse(ConfigurationManager.AppSettings["watermarkposition"]),
  304. int.Parse(ConfigurationManager.AppSettings["watermarkimgquality"]), ConfigurationManager.AppSettings["watermarkfont"], int.Parse(ConfigurationManager.AppSettings["watermarkfontsize"]));
  305. break;
  306. case "2":
  307. WaterMark.AddImageSignPic(newFilePath, newFilePath,
  308. ConfigurationManager.AppSettings["watermarkpic"], int.Parse(ConfigurationManager.AppSettings["watermarkposition"]),
  309. int.Parse(ConfigurationManager.AppSettings["watermarkimgquality"]), int.Parse(ConfigurationManager.AppSettings["watermarktransparency"]));
  310. break;
  311. }
  312. }
  313. string unit = string.Empty;
  314. //处理完毕,返回JOSN格式的文件信息
  315. jsons.Data = "{\"oldname\": \"" + fileName + "\","; //原始名称
  316. jsons.Data += " \"newname\":\"" + newFileName + "\","; //新名称
  317. jsons.Data += " \"path\": \"" + newFilePath + "\", "; //路径
  318. jsons.Data += " \"thumbpath\":\"" + thumbnail + "\","; //缩略图
  319. jsons.Data += " \"size\": \"" + fileSize + "\","; //大小
  320. jsons.Data += "\"ext\":\"" + fileExt + "\"}";//后缀名
  321. jsons.Status = "y";
  322. return jsons;
  323. }
  324. catch
  325. {
  326. jsons.Msg = "上传过程中发生意外错误!";
  327. return jsons;
  328. }
  329. }
  330. #endregion
  331. #region 其他调用
  332. /// <summary>
  333. /// 上传主调用器
  334. /// </summary>
  335. public ActionResult FileMain()
  336. {
  337. return View();
  338. }
  339. /// <summary>
  340. /// 图片预览视图
  341. /// </summary>
  342. public ActionResult Show()
  343. {
  344. return View();
  345. }
  346. /// <summary>
  347. /// 通过路径获取文件
  348. /// </summary>
  349. /// <returns></returns>
  350. public ActionResult GetFileData()
  351. {
  352. string path = Request.Form["path"];
  353. var jsonM = new JsonHelper() { Status = "y", Msg = "success" };
  354. try
  355. {
  356. jsonM.Data = Utils.DataTableToList<FileModel>(FileHelper.GetFileTable(Server.MapPath(path))).OrderByDescending(p => p.time).ToList();
  357. }
  358. catch (Exception ex)
  359. {
  360. jsonM.Status = "err";
  361. jsonM.Msg = "上传过程中发生错误,消息:" + ex.Message;
  362. }
  363. return Content(JsonConverter.Serialize(jsonM, true));
  364. }
  365. /// <summary>
  366. /// 单个文件上传
  367. /// </summary>
  368. [HttpPost]
  369. public ActionResult SignUpFile()
  370. {
  371. var jsonM = new JsonHelper() { Status = "n", Msg = "success" };
  372. try
  373. {
  374. HttpPostedFileBase upfile = Request.Files["fileUp"]; //取得上传文件
  375. var isImg = Request.QueryString["isImg"];
  376. string delpath = Request.QueryString["delpath"];
  377. if (upfile == null)
  378. {
  379. jsonM.Msg = "请选择要上传文件!";
  380. return Json(jsonM);
  381. }
  382. jsonM = FileSaveAs(upfile, false, false, (isImg.ToLower() == "true" ? true : false));
  383. #region 移除原始文件
  384. if (jsonM.Status == "y" && !string.IsNullOrEmpty(delpath))
  385. {
  386. if (System.IO.File.Exists(Utils.GetMapPath(delpath)))
  387. {
  388. System.IO.File.Delete(Utils.GetMapPath(delpath));
  389. }
  390. }
  391. #endregion
  392. if (jsonM.Status == "y")
  393. {
  394. #region 记录上传数据
  395. string unit = string.Empty;
  396. var jsonValue = JsonConverter.ConvertJson(jsonM.Data.ToString());
  397. var entity = new COM_UPLOAD()
  398. {
  399. ID = Guid.NewGuid().ToString(),
  400. FK_USERID = this.CurrentUser.Id.ToString(),
  401. UPOPEATOR = this.CurrentUser.Name,
  402. UPTIME = DateTime.Now,
  403. UPOLDNAME = jsonValue.oldname,
  404. UPNEWNAME = jsonValue.newname,
  405. UPFILESIZE = FileHelper.GetDiyFileSize(long.Parse(jsonValue.size), out unit),
  406. UPFILEUNIT = unit,
  407. UPFILEPATH = jsonValue.path,
  408. UPFILESUFFIX = jsonValue.ext,
  409. UPFILETHUMBNAIL = jsonValue.thumbpath,
  410. UPFILEIP = Utils.GetIP(),
  411. UPFILEURL = "http://" + Request.Url.AbsoluteUri.Replace("http://", "").Substring(0, Request.Url.AbsoluteUri.Replace("http://", "").IndexOf('/'))
  412. };
  413. this.UploadManage.Save(entity);
  414. #endregion
  415. #region 返回文件信息
  416. jsonM.Data = "{\"oldname\": \"" + jsonValue.oldname + "\","; //原始名称
  417. jsonM.Data += " \"newname\":\"" + jsonValue.newname + "\","; //新名称
  418. jsonM.Data += " \"path\": \"" + jsonValue.path + "\", "; //路径
  419. jsonM.Data += " \"thumbpath\":\"" + jsonValue.thumbpath + "\","; //缩略图
  420. jsonM.Data += " \"size\": \"" + jsonValue.size + "\","; //大小
  421. jsonM.Data += " \"id\": \"" + entity.ID + "\","; //上传文件ID
  422. jsonM.Data += " \"uptime\": \"" + entity.UPTIME + "\","; //上传时间
  423. jsonM.Data += " \"operator\": \"" + entity.UPOPEATOR + "\","; //上传人
  424. jsonM.Data += " \"unitsize\": \"" + entity.UPFILESIZE + unit + "\","; //带单位大小
  425. jsonM.Data += "\"ext\":\"" + jsonValue.ext + "\"}";//后缀名
  426. #endregion
  427. }
  428. }
  429. catch (Exception ex)
  430. {
  431. jsonM.Msg = "上传过程中发生错误,消息:" + ex.Message;
  432. jsonM.Status = "n";
  433. }
  434. return Json(jsonM);
  435. }
  436. /// <summary>
  437. /// 删除文件或文件夹
  438. /// </summary>
  439. /// <returns></returns>
  440. public ActionResult DeleteBy()
  441. {
  442. var jsonM = new JsonHelper() { Status = "y", Msg = "success" };
  443. try
  444. {
  445. var path = Request.QueryString["path"];
  446. var isFile = Request.QueryString["isfile"];
  447. if (isFile == "0")
  448. {
  449. //删除文件夹
  450. FileHelper.ClearDirectory(Server.MapPath(path));
  451. }
  452. else
  453. {
  454. //删除文件
  455. FileHelper.DeleteFile(Server.MapPath(path));
  456. }
  457. }
  458. catch (Exception ex)
  459. {
  460. jsonM.Status = "err";
  461. jsonM.Msg = "删除过程中发生错误,消息:" + ex.Message;
  462. }
  463. return Json(jsonM);
  464. }
  465. #endregion
  466. }
  467. #region 自定义类
  468. /// <summary>
  469. /// 文件模型
  470. /// </summary>
  471. public class FileModel
  472. {
  473. public string name { get; set; }
  474. public string ext { get; set; }
  475. public string size { get; set; }
  476. public DateTime time { get; set; }
  477. }
  478. /// <summary>
  479. /// 缩略图构造类
  480. /// </summary>
  481. public class Thumbnail
  482. {
  483. private Image srcImage;
  484. private string srcFileName;
  485. /// <summary>
  486. /// 创建
  487. /// </summary>
  488. /// <param name="FileName">原始图片路径</param>
  489. public bool SetImage(string FileName)
  490. {
  491. srcFileName = Utils.GetMapPath(FileName);
  492. try
  493. {
  494. srcImage = Image.FromFile(srcFileName);
  495. }
  496. catch
  497. {
  498. return false;
  499. }
  500. return true;
  501. }
  502. /// <summary>
  503. /// 回调
  504. /// </summary>
  505. /// <returns></returns>
  506. public bool ThumbnailCallback()
  507. {
  508. return false;
  509. }
  510. /// <summary>
  511. /// 生成缩略图,返回缩略图的Image对象
  512. /// </summary>
  513. /// <param name="Width">缩略图宽度</param>
  514. /// <param name="Height">缩略图高度</param>
  515. /// <returns>缩略图的Image对象</returns>
  516. public Image GetImage(int Width, int Height)
  517. {
  518. Image img;
  519. Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
  520. img = srcImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
  521. return img;
  522. }
  523. /// <summary>
  524. /// 保存缩略图
  525. /// </summary>
  526. /// <param name="Width"></param>
  527. /// <param name="Height"></param>
  528. public void SaveThumbnailImage(int Width, int Height)
  529. {
  530. switch (Path.GetExtension(srcFileName).ToLower())
  531. {
  532. case ".png":
  533. SaveImage(Width, Height, ImageFormat.Png);
  534. break;
  535. case ".gif":
  536. SaveImage(Width, Height, ImageFormat.Gif);
  537. break;
  538. default:
  539. SaveImage(Width, Height, ImageFormat.Jpeg);
  540. break;
  541. }
  542. }
  543. /// <summary>
  544. /// 生成缩略图并保存
  545. /// </summary>
  546. /// <param name="Width">缩略图的宽度</param>
  547. /// <param name="Height">缩略图的高度</param>
  548. /// <param name="imgformat">保存的图像格式</param>
  549. /// <returns>缩略图的Image对象</returns>
  550. public void SaveImage(int Width, int Height, ImageFormat imgformat)
  551. {
  552. if (imgformat != ImageFormat.Gif && (srcImage.Width > Width) || (srcImage.Height > Height))
  553. {
  554. Image img;
  555. Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
  556. img = srcImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
  557. srcImage.Dispose();
  558. img.Save(srcFileName, imgformat);
  559. img.Dispose();
  560. }
  561. }
  562. #region Helper
  563. /// <summary>
  564. /// 保存图片
  565. /// </summary>
  566. /// <param name="image">Image 对象</param>
  567. /// <param name="savePath">保存路径</param>
  568. /// <param name="ici">指定格式的编解码参数</param>
  569. private static void SaveImage(Image image, string savePath, ImageCodecInfo ici)
  570. {
  571. //设置 原图片 对象的 EncoderParameters 对象
  572. EncoderParameters parameters = new EncoderParameters(1);
  573. parameters.Param[0] = new EncoderParameter(Encoder.Quality, ((long)100));
  574. image.Save(savePath, ici, parameters);
  575. parameters.Dispose();
  576. }
  577. /// <summary>
  578. /// 获取图像编码解码器的所有相关信息
  579. /// </summary>
  580. /// <param name="mimeType">包含编码解码器的多用途网际邮件扩充协议 (MIME) 类型的字符串</param>
  581. /// <returns>返回图像编码解码器的所有相关信息</returns>
  582. private static ImageCodecInfo GetCodecInfo(string mimeType)
  583. {
  584. ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
  585. foreach (ImageCodecInfo ici in CodecInfo)
  586. {
  587. if (ici.MimeType == mimeType)
  588. return ici;
  589. }
  590. return null;
  591. }
  592. /// <summary>
  593. /// 计算新尺寸
  594. /// </summary>
  595. /// <param name="width">原始宽度</param>
  596. /// <param name="height">原始高度</param>
  597. /// <param name="maxWidth">最大新宽度</param>
  598. /// <param name="maxHeight">最大新高度</param>
  599. /// <returns></returns>
  600. private static Size ResizeImage(int width, int height, int maxWidth, int maxHeight)
  601. {
  602. //此次2012-02-05修改过=================
  603. if (maxWidth <= 0)
  604. maxWidth = width;
  605. if (maxHeight <= 0)
  606. maxHeight = height;
  607. //以上2012-02-05修改过=================
  608. decimal MAX_WIDTH = (decimal)maxWidth;
  609. decimal MAX_HEIGHT = (decimal)maxHeight;
  610. decimal ASPECT_RATIO = MAX_WIDTH / MAX_HEIGHT;
  611. int newWidth, newHeight;
  612. decimal originalWidth = (decimal)width;
  613. decimal originalHeight = (decimal)height;
  614. if (originalWidth > MAX_WIDTH || originalHeight > MAX_HEIGHT)
  615. {
  616. decimal factor;
  617. // determine the largest factor
  618. if (originalWidth / originalHeight > ASPECT_RATIO)
  619. {
  620. factor = originalWidth / MAX_WIDTH;
  621. newWidth = Convert.ToInt32(originalWidth / factor);
  622. newHeight = Convert.ToInt32(originalHeight / factor);
  623. }
  624. else
  625. {
  626. factor = originalHeight / MAX_HEIGHT;
  627. newWidth = Convert.ToInt32(originalWidth / factor);
  628. newHeight = Convert.ToInt32(originalHeight / factor);
  629. }
  630. }
  631. else
  632. {
  633. newWidth = width;
  634. newHeight = height;
  635. }
  636. return new Size(newWidth, newHeight);
  637. }
  638. /// <summary>
  639. /// 得到图片格式
  640. /// </summary>
  641. /// <param name="name">文件名称</param>
  642. /// <returns></returns>
  643. public static ImageFormat GetFormat(string name)
  644. {
  645. string ext = name.Substring(name.LastIndexOf(".") + 1);
  646. switch (ext.ToLower())
  647. {
  648. case "jpg":
  649. case "jpeg":
  650. return ImageFormat.Jpeg;
  651. case "bmp":
  652. return ImageFormat.Bmp;
  653. case "png":
  654. return ImageFormat.Png;
  655. case "gif":
  656. return ImageFormat.Gif;
  657. default:
  658. return ImageFormat.Jpeg;
  659. }
  660. }
  661. #endregion
  662. /// <summary>
  663. /// 制作小正方形
  664. /// </summary>
  665. /// <param name="image">图片对象</param>
  666. /// <param name="newFileName">新地址</param>
  667. /// <param name="newSize">长度或宽度</param>
  668. public static void MakeSquareImage(Image image, string newFileName, int newSize)
  669. {
  670. int i = 0;
  671. int width = image.Width;
  672. int height = image.Height;
  673. if (width > height)
  674. i = height;
  675. else
  676. i = width;
  677. Bitmap b = new Bitmap(newSize, newSize);
  678. try
  679. {
  680. Graphics g = Graphics.FromImage(b);
  681. //设置高质量插值法
  682. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  683. //设置高质量,低速度呈现平滑程度
  684. g.SmoothingMode = SmoothingMode.AntiAlias;
  685. g.PixelOffsetMode = PixelOffsetMode.HighQuality;
  686. //清除整个绘图面并以透明背景色填充
  687. g.Clear(Color.Transparent);
  688. if (width < height)
  689. g.DrawImage(image, new Rectangle(0, 0, newSize, newSize), new Rectangle(0, (height - width) / 2, width, width), GraphicsUnit.Pixel);
  690. else
  691. g.DrawImage(image, new Rectangle(0, 0, newSize, newSize), new Rectangle((width - height) / 2, 0, height, height), GraphicsUnit.Pixel);
  692. SaveImage(b, newFileName, GetCodecInfo("image/" + GetFormat(newFileName).ToString().ToLower()));
  693. }
  694. finally
  695. {
  696. image.Dispose();
  697. b.Dispose();
  698. }
  699. }
  700. /// <summary>
  701. /// 制作小正方形
  702. /// </summary>
  703. /// <param name="fileName">图片文件名</param>
  704. /// <param name="newFileName">新地址</param>
  705. /// <param name="newSize">长度或宽度</param>
  706. public static void MakeSquareImage(string fileName, string newFileName, int newSize)
  707. {
  708. MakeSquareImage(Image.FromFile(fileName), newFileName, newSize);
  709. }
  710. /// <summary>
  711. /// 制作远程小正方形
  712. /// </summary>
  713. /// <param name="url">图片url</param>
  714. /// <param name="newFileName">新地址</param>
  715. /// <param name="newSize">长度或宽度</param>
  716. public static void MakeRemoteSquareImage(string url, string newFileName, int newSize)
  717. {
  718. Stream stream = GetRemoteImage(url);
  719. if (stream == null)
  720. return;
  721. Image original = Image.FromStream(stream);
  722. stream.Close();
  723. MakeSquareImage(original, newFileName, newSize);
  724. }
  725. /// <summary>
  726. /// 制作缩略图
  727. /// </summary>
  728. /// <param name="original">图片对象</param>
  729. /// <param name="newFileName">新图路径</param>
  730. /// <param name="maxWidth">最大宽度</param>
  731. /// <param name="maxHeight">最大高度</param>
  732. public static void MakeThumbnailImage(Image original, string newFileName, int maxWidth, int maxHeight)
  733. {
  734. Size _newSize = ResizeImage(original.Width, original.Height, maxWidth, maxHeight);
  735. using (Image displayImage = new Bitmap(original, _newSize))
  736. {
  737. try
  738. {
  739. displayImage.Save(newFileName, original.RawFormat);
  740. }
  741. finally
  742. {
  743. original.Dispose();
  744. }
  745. }
  746. }
  747. /// <summary>
  748. /// 制作缩略图
  749. /// </summary>
  750. /// <param name="fileName">文件名</param>
  751. /// <param name="newFileName">新图路径</param>
  752. /// <param name="maxWidth">最大宽度</param>
  753. /// <param name="maxHeight">最大高度</param>
  754. public static void MakeThumbnailImage(string fileName, string newFileName, int maxWidth, int maxHeight)
  755. {
  756. //2012-02-05修改过,支持替换
  757. byte[] imageBytes = File.ReadAllBytes(fileName);
  758. Image img = Image.FromStream(new System.IO.MemoryStream(imageBytes));
  759. MakeThumbnailImage(img, newFileName, maxWidth, maxHeight);
  760. //原文
  761. //MakeThumbnailImage(Image.FromFile(fileName), newFileName, maxWidth, maxHeight);
  762. }
  763. #region 2012-2-19 新增生成图片缩略图方法
  764. /// <summary>
  765. /// 生成缩略图
  766. /// </summary>
  767. /// <param name="fileName">源图路径(绝对路径)</param>
  768. /// <param name="newFileName">缩略图路径(绝对路径)</param>
  769. /// <param name="width">缩略图宽度</param>
  770. /// <param name="height">缩略图高度</param>
  771. /// <param name="mode">生成缩略图的方式</param>
  772. public static void MakeThumbnailImage(string fileName, string newFileName, int width, int height, string mode)
  773. {
  774. Image originalImage = Image.FromFile(fileName);
  775. int towidth = width;
  776. int toheight = height;
  777. int x = 0;
  778. int y = 0;
  779. int ow = originalImage.Width;
  780. int oh = originalImage.Height;
  781. switch (mode)
  782. {
  783. case "HW"://指定高宽缩放(可能变形)
  784. break;
  785. case "W"://指定宽,高按比例
  786. toheight = originalImage.Height * width / originalImage.Width;
  787. break;
  788. case "H"://指定高,宽按比例
  789. towidth = originalImage.Width * height / originalImage.Height;
  790. break;
  791. case "Cut"://指定高宽裁减(不变形)
  792. if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
  793. {
  794. oh = originalImage.Height;
  795. ow = originalImage.Height * towidth / toheight;
  796. y = 0;
  797. x = (originalImage.Width - ow) / 2;
  798. }
  799. else
  800. {
  801. ow = originalImage.Width;
  802. oh = originalImage.Width * height / towidth;
  803. x = 0;
  804. y = (originalImage.Height - oh) / 2;
  805. }
  806. break;
  807. default:
  808. break;
  809. }
  810. //新建一个bmp图片
  811. Bitmap b = new Bitmap(towidth, toheight);
  812. try
  813. {
  814. //新建一个画板
  815. Graphics g = Graphics.FromImage(b);
  816. //设置高质量插值法
  817. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  818. //设置高质量,低速度呈现平滑程度
  819. g.SmoothingMode = SmoothingMode.AntiAlias;
  820. g.PixelOffsetMode = PixelOffsetMode.HighQuality;
  821. //清空画布并以透明背景色填充
  822. g.Clear(Color.Transparent);
  823. //在指定位置并且按指定大小绘制原图片的指定部分
  824. g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);
  825. SaveImage(b, newFileName, GetCodecInfo("image/" + GetFormat(newFileName).ToString().ToLower()));
  826. }
  827. catch (System.Exception e)
  828. {
  829. throw e;
  830. }
  831. finally
  832. {
  833. originalImage.Dispose();
  834. b.Dispose();
  835. }
  836. }
  837. #endregion
  838. #region 2012-10-30 新增图片裁剪方法
  839. /// <summary>
  840. /// 裁剪图片并保存
  841. /// </summary>
  842. /// <param name="fileName">源图路径(绝对路径)</param>
  843. /// <param name="newFileName">缩略图路径(绝对路径)</param>
  844. /// <param name="maxWidth">缩略图宽度</param>
  845. /// <param name="maxHeight">缩略图高度</param>
  846. /// <param name="cropWidth">裁剪宽度</param>
  847. /// <param name="cropHeight">裁剪高度</param>
  848. /// <param name="X">X轴</param>
  849. /// <param name="Y">Y轴</param>
  850. public static bool MakeThumbnailImage(string fileName, string newFileName, int maxWidth, int maxHeight, int cropWidth, int cropHeight, int X, int Y)
  851. {
  852. byte[] imageBytes = File.ReadAllBytes(fileName);
  853. Image originalImage = Image.FromStream(new System.IO.MemoryStream(imageBytes));
  854. Bitmap b = new Bitmap(cropWidth, cropHeight);
  855. try
  856. {
  857. using (Graphics g = Graphics.FromImage(b))
  858. {
  859. //设置高质量插值法
  860. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  861. //设置高质量,低速度呈现平滑程度
  862. g.SmoothingMode = SmoothingMode.AntiAlias;
  863. g.PixelOffsetMode = PixelOffsetMode.HighQuality;
  864. //清空画布并以透明背景色填充
  865. g.Clear(Color.Transparent);
  866. //在指定位置并且按指定大小绘制原图片的指定部分
  867. g.DrawImage(originalImage, new Rectangle(0, 0, cropWidth, cropWidth), X, Y, cropWidth, cropHeight, GraphicsUnit.Pixel);
  868. Image displayImage = new Bitmap(b, maxWidth, maxHeight);
  869. SaveImage(displayImage, newFileName, GetCodecInfo("image/" + GetFormat(newFileName).ToString().ToLower()));
  870. return true;
  871. }
  872. }
  873. catch (System.Exception e)
  874. {
  875. throw e;
  876. }
  877. finally
  878. {
  879. originalImage.Dispose();
  880. b.Dispose();
  881. }
  882. }
  883. #endregion
  884. /// <summary>
  885. /// 制作远程缩略图
  886. /// </summary>
  887. /// <param name="url">图片URL</param>
  888. /// <param name="newFileName">新图路径</param>
  889. /// <param name="maxWidth">最大宽度</param>
  890. /// <param name="maxHeight">最大高度</param>
  891. public static void MakeRemoteThumbnailImage(string url, string newFileName, int maxWidth, int maxHeight)
  892. {
  893. Stream stream = GetRemoteImage(url);
  894. if (stream == null)
  895. return;
  896. Image original = Image.FromStream(stream);
  897. stream.Close();
  898. MakeThumbnailImage(original, newFileName, maxWidth, maxHeight);
  899. }
  900. /// <summary>
  901. /// 获取图片流
  902. /// </summary>
  903. /// <param name="url">图片URL</param>
  904. /// <returns></returns>
  905. private static Stream GetRemoteImage(string url)
  906. {
  907. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
  908. request.Method = "GET";
  909. request.ContentLength = 0;
  910. request.Timeout = 20000;
  911. HttpWebResponse response = null;
  912. try
  913. {
  914. response = (HttpWebResponse)request.GetResponse();
  915. return response.GetResponseStream();
  916. }
  917. catch
  918. {
  919. return null;
  920. }
  921. }
  922. }
  923. /// <summary>
  924. /// 水印构造类
  925. /// </summary>
  926. public class WaterMark
  927. {
  928. /// <summary>
  929. /// 图片水印
  930. /// </summary>
  931. /// <param name="imgPath">服务器图片相对路径</param>
  932. /// <param name="filename">保存文件名</param>
  933. /// <param name="watermarkFilename">水印文件相对路径</param>
  934. /// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下</param>
  935. /// <param name="quality">附加水印图片质量,0-100</param>
  936. /// <param name="watermarkTransparency">水印的透明度 1--10 10为不透明</param>
  937. public static void AddImageSignPic(string imgPath, string filename, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency)
  938. {
  939. if (!File.Exists(Utils.GetMapPath(imgPath)))
  940. return;
  941. byte[] _ImageBytes = File.ReadAllBytes(Utils.GetMapPath(imgPath));
  942. Image img = Image.FromStream(new System.IO.MemoryStream(_ImageBytes));
  943. filename = Utils.GetMapPath(filename);
  944. if (watermarkFilename.StartsWith("/") == false)
  945. watermarkFilename = "/" + watermarkFilename;
  946. watermarkFilename = Utils.GetMapPath(watermarkFilename);
  947. if (!File.Exists(watermarkFilename))
  948. return;
  949. Graphics g = Graphics.FromImage(img);
  950. //设置高质量插值法
  951. //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  952. //设置高质量,低速度呈现平滑程度
  953. //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  954. Image watermark = new Bitmap(watermarkFilename);
  955. if (watermark.Height >= img.Height || watermark.Width >= img.Width)
  956. return;
  957. ImageAttributes imageAttributes = new ImageAttributes();
  958. ColorMap colorMap = new ColorMap();
  959. colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
  960. colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
  961. ColorMap[] remapTable = { colorMap };
  962. imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
  963. float transparency = 0.5F;
  964. if (watermarkTransparency >= 1 && watermarkTransparency <= 10)
  965. transparency = (watermarkTransparency / 10.0F);
  966. float[][] colorMatrixElements = {
  967. new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
  968. new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
  969. new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
  970. new float[] {0.0f, 0.0f, 0.0f, transparency, 0.0f},
  971. new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
  972. };
  973. ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
  974. imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  975. int xpos = 0;
  976. int ypos = 0;
  977. switch (watermarkStatus)
  978. {
  979. case 1:
  980. xpos = (int)(img.Width * (float).01);
  981. ypos = (int)(img.Height * (float).01);
  982. break;
  983. case 2:
  984. xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
  985. ypos = (int)(img.Height * (float).01);
  986. break;
  987. case 3:
  988. xpos = (int)((img.Width * (float).99) - (watermark.Width));
  989. ypos = (int)(img.Height * (float).01);
  990. break;
  991. case 4:
  992. xpos = (int)(img.Width * (float).01);
  993. ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
  994. break;
  995. case 5:
  996. xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
  997. ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
  998. break;
  999. case 6:
  1000. xpos = (int)((img.Width * (float).99) - (watermark.Width));
  1001. ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
  1002. break;
  1003. case 7:
  1004. xpos = (int)(img.Width * (float).01);
  1005. ypos = (int)((img.Height * (float).99) - watermark.Height);
  1006. break;
  1007. case 8:
  1008. xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
  1009. ypos = (int)((img.Height * (float).99) - watermark.Height);
  1010. break;
  1011. case 9:
  1012. xpos = (int)((img.Width * (float).99) - (watermark.Width));
  1013. ypos = (int)((img.Height * (float).99) - watermark.Height);
  1014. break;
  1015. }
  1016. g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
  1017. ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
  1018. ImageCodecInfo ici = null;
  1019. foreach (ImageCodecInfo codec in codecs)
  1020. {
  1021. if (codec.MimeType.IndexOf("jpeg") > -1)
  1022. ici = codec;
  1023. }
  1024. EncoderParameters encoderParams = new EncoderParameters();
  1025. long[] qualityParam = new long[1];
  1026. if (quality < 0 || quality > 100)
  1027. quality = 80;
  1028. qualityParam[0] = quality;
  1029. EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
  1030. encoderParams.Param[0] = encoderParam;
  1031. if (ici != null)
  1032. img.Save(filename, ici, encoderParams);
  1033. else
  1034. img.Save(filename);
  1035. g.Dispose();
  1036. img.Dispose();
  1037. watermark.Dispose();
  1038. imageAttributes.Dispose();
  1039. }
  1040. /// <summary>
  1041. /// 文字水印
  1042. /// </summary>
  1043. /// <param name="imgPath">服务器图片相对路径</param>
  1044. /// <param name="filename">保存文件名</param>
  1045. /// <param name="watermarkText">水印文字</param>
  1046. /// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下</param>
  1047. /// <param name="quality">附加水印图片质量,0-100</param>
  1048. /// <param name="fontname">字体</param>
  1049. /// <param name="fontsize">字体大小</param>
  1050. public static void AddImageSignText(string imgPath, string filename, string watermarkText, int watermarkStatus, int quality, string fontname, int fontsize)
  1051. {
  1052. byte[] _ImageBytes = File.ReadAllBytes(Utils.GetMapPath(imgPath));
  1053. Image img = Image.FromStream(new System.IO.MemoryStream(_ImageBytes));
  1054. filename = Utils.GetMapPath(filename);
  1055. Graphics g = Graphics.FromImage(img);
  1056. Font drawFont = new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel);
  1057. SizeF crSize;
  1058. crSize = g.MeasureString(watermarkText, drawFont);
  1059. float xpos = 0;
  1060. float ypos = 0;
  1061. switch (watermarkStatus)
  1062. {
  1063. case 1:
  1064. xpos = (float)img.Width * (float).01;
  1065. ypos = (float)img.Height * (float).01;
  1066. break;
  1067. case 2:
  1068. xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
  1069. ypos = (float)img.Height * (float).01;
  1070. break;
  1071. case 3:
  1072. xpos = ((float)img.Width * (float).99) - crSize.Width;
  1073. ypos = (float)img.Height * (float).01;
  1074. break;
  1075. case 4:
  1076. xpos = (float)img.Width * (float).01;
  1077. ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
  1078. break;
  1079. case 5:
  1080. xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
  1081. ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
  1082. break;
  1083. case 6:
  1084. xpos = ((float)img.Width * (float).99) - crSize.Width;
  1085. ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
  1086. break;
  1087. case 7:
  1088. xpos = (float)img.Width * (float).01;
  1089. ypos = ((float)img.Height * (float).99) - crSize.Height;
  1090. break;
  1091. case 8:
  1092. xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
  1093. ypos = ((float)img.Height * (float).99) - crSize.Height;
  1094. break;
  1095. case 9:
  1096. xpos = ((float)img.Width * (float).99) - crSize.Width;
  1097. ypos = ((float)img.Height * (float).99) - crSize.Height;
  1098. break;
  1099. }
  1100. g.DrawString(watermarkText, drawFont, new SolidBrush(Color.White), xpos + 1, ypos + 1);
  1101. g.DrawString(watermarkText, drawFont, new SolidBrush(Color.Black), xpos, ypos);
  1102. ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
  1103. ImageCodecInfo ici = null;
  1104. foreach (ImageCodecInfo codec in codecs)
  1105. {
  1106. if (codec.MimeType.IndexOf("jpeg") > -1)
  1107. ici = codec;
  1108. }
  1109. EncoderParameters encoderParams = new EncoderParameters();
  1110. long[] qualityParam = new long[1];
  1111. if (quality < 0 || quality > 100)
  1112. quality = 80;
  1113. qualityParam[0] = quality;
  1114. EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
  1115. encoderParams.Param[0] = encoderParam;
  1116. if (ici != null)
  1117. img.Save(filename, ici, encoderParams);
  1118. else
  1119. img.Save(filename);
  1120. g.Dispose();
  1121. img.Dispose();
  1122. }
  1123. }
  1124. #endregion
  1125. }