uploadFile.aspx.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. using BizCom;
  2. using ICSharpCode.SharpZipLib.Zip;
  3. using Microsoft.Win32;
  4. using SevenZip;
  5. using System;
  6. using System.Configuration;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Text;
  10. using System.Web;
  11. using Utils;
  12. using SiteCore;
  13. using Aspose.Imaging.ImageOptions;
  14. using Aspose.Imaging;
  15. using Microsoft.WindowsAPICodePack.Shell;
  16. using System.Drawing.Imaging;
  17. using CorelDRAW;
  18. using System.Security.Principal;
  19. using System.Threading.Tasks;
  20. using System.Threading;
  21. using System.Runtime.InteropServices;
  22. using System.Text.RegularExpressions;
  23. using System.Data;
  24. public partial class uploadFile : System.Web.UI.Page
  25. {
  26. public static string upPath = ConfigurationManager.AppSettings["upPath"];
  27. public static string curPath = ConfigurationManager.AppSettings["curPath"];
  28. static CdrConvert cdrConvert = new CdrConvert();
  29. private void conSuc(string msg)
  30. {
  31. Response.Write("{\"res\":\"1\",\"msg\":\"" + msg + "!\"}");
  32. //Response.End();
  33. }
  34. private void conErc(string msg)
  35. {
  36. Response.Write("{\"res\":\"0\",\"msg\":\"" + msg + "!\"}");
  37. //Response.End();
  38. }
  39. public string GetMD5HashFromFile(HttpPostedFile file)
  40. {
  41. try
  42. {
  43. System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  44. byte[] retVal = md5.ComputeHash(file.InputStream);
  45. StringBuilder sb = new StringBuilder();
  46. for (int i = 0; i < retVal.Length; i++)
  47. {
  48. sb.Append(retVal[i].ToString("x2"));
  49. }
  50. return sb.ToString();
  51. }
  52. catch (Exception ex)
  53. {
  54. XLog.SaveLog(0, "上传MD5错误!" + ex.Message);
  55. }
  56. return "";
  57. }
  58. private static byte[] GetBlobByHttpPostedFile(HttpPostedFile httpPostedFile)
  59. {
  60. var contentLength = httpPostedFile.ContentLength;
  61. var result = new byte[contentLength];
  62. var inputStream = httpPostedFile.InputStream;
  63. inputStream.Read(result, 0, contentLength);
  64. return result;
  65. }
  66. protected void Page_Load(object sender, EventArgs e)
  67. {
  68. //if (CurrentUser == null)
  69. //{
  70. // Response.Write(err);
  71. // return;
  72. //}
  73. //CeErpTradeCell.GetByCtid("1717009344450030601");
  74. if (Request.Files.Count < 1)
  75. {
  76. conErc("空文件!");
  77. return;
  78. }
  79. int userId = 0;
  80. int orgid = 0;
  81. if (Request["userid"] != null)
  82. {
  83. userId = Convert.ToInt32(Request["userid"]);
  84. }
  85. if (Request["orgid"] != null)
  86. {
  87. orgid = Convert.ToInt32(Request["orgid"]);
  88. }
  89. HttpPostedFile postFile = Request.Files[0];
  90. if (postFile != null)
  91. {
  92. string file_name = postFile.FileName;
  93. string namePattern = @"《(.*?)》";
  94. Regex nameReg = new Regex(namePattern, RegexOptions.IgnoreCase | RegexOptions.Multiline, TimeSpan.FromSeconds(2));//2秒后超时
  95. MatchCollection nameMatches = nameReg.Matches(file_name);//设定要查找的字符串
  96. if (nameMatches.Count > 0)
  97. {
  98. foreach (Match match in nameMatches)
  99. {
  100. file_name = file_name.Replace(match.Value, "");
  101. }
  102. }
  103. file_name = file_name.Replace("(", "(");
  104. file_name = file_name.Replace(")", ")");
  105. string ctid = MidStrEx(file_name, "(", ")").Trim();
  106. if (string.IsNullOrEmpty(ctid))
  107. {
  108. conErc("上传的文件名格式不正确");
  109. return;
  110. }
  111. string memoCtid = ctid;
  112. if (ctid.IndexOf("C") == -1)
  113. {
  114. if (file_name.IndexOf("[") != -1 && file_name.IndexOf("C") != -1)
  115. {
  116. if (ctid.IndexOf("S_") != -1) // (S_S_1962772776865084101)[C1] 对应ctid是 S_S_C1_1962772776865084101
  117. {
  118. int lastIndex = ctid.LastIndexOf("S_"); //最后一个S_的位置
  119. string sPre = ctid.Substring(0, lastIndex + 2); //S_S_
  120. string initTid = ctid.Substring(lastIndex + 2, ctid.Length - lastIndex - 2); //1962772776865084101
  121. string pre_ctid = MidStrEx(file_name, "[", "]"); //C1
  122. if (pre_ctid.IndexOf("+") != -1)
  123. {
  124. pre_ctid = "C" + pre_ctid.Split('+')[1];
  125. }
  126. memoCtid = sPre + pre_ctid + "_" + initTid; //S_S_ + C1 + _ +1962772776865084101
  127. }
  128. else
  129. {
  130. string pre_ctid = MidStrEx(file_name, "[", "]");
  131. if (pre_ctid.IndexOf("+") != -1)
  132. {
  133. pre_ctid = "C" + pre_ctid.Split('+')[1];
  134. }
  135. memoCtid = pre_ctid + "_" + ctid;
  136. }
  137. }
  138. }
  139. CeErpTradeCell entity = null;
  140. if (ctid != "") entity = CeErpTradeCell.GetByCode(ctid);
  141. if (entity == null) entity = CeErpTradeCell.GetByCtid(ctid);
  142. if (entity == null) entity = CeErpTradeCell.GetByCtid(memoCtid);
  143. try
  144. {
  145. if (entity != null)
  146. {
  147. string pname = Path.GetFileNameWithoutExtension(file_name);
  148. if (entity.IsRefund == 2)
  149. {
  150. StringBuilder sql = new StringBuilder();
  151. sql.AppendFormat("select refund_status from CE_ErpTradeOrder where tid='{0}'", entity.tid);
  152. DataTable dt = CeErpTradeCell.ExecuteDataset(sql.ToString()).Tables[0];
  153. bool isAll = true;
  154. foreach (DataRow dr in dt.Rows)
  155. {
  156. if ("NO_REFUND".Equals(dr["refund_status"]))
  157. {
  158. isAll = false;
  159. break;
  160. }
  161. }
  162. if (isAll)
  163. {
  164. conErc("此单退款,不允许上传");
  165. return;
  166. }
  167. }
  168. if (entity.seller_memo != pname)
  169. {
  170. conErc("上传的文件名与备注不符合!");
  171. return;
  172. }
  173. if (entity.OrderState == -1)
  174. {
  175. conErc("还未审核不允许上传");
  176. return;
  177. }
  178. if (entity.OrderState < (int)OrderState.设计中 && orgid != 10 && orgid != 4)
  179. {
  180. conErc("还未开始设计不允许上传");
  181. return;
  182. }
  183. //if (pname.IndexOf("现货") != -1)
  184. //{
  185. // conErc("设计款的单文件名不能有现货字眼");
  186. // return;
  187. //}
  188. //更新状态
  189. if (entity.OrderState >= (int)OrderState.下单完成)
  190. {
  191. conErc("已经下单无法上传!");
  192. return;
  193. }
  194. if (entity.IsVerifyToSupplier)
  195. {
  196. conErc("订单已到车间无法上传!");
  197. return;
  198. }
  199. string extend = Path.GetExtension(file_name).ToLower();
  200. if (!(extend == ".cdr" || extend == ".zip" || extend == ".rar" || extend == ".pdf"))
  201. {
  202. conErc("只允许上传zip和cdr文件!");
  203. return;
  204. }
  205. if (entity.OrderState != 5)
  206. {
  207. entity.FinishDesignTime = DateTime.Now;
  208. if (entity.isDianziOrder == 1 || entity.ProductId == 57 || entity.ProductId == 28)
  209. {
  210. entity.OrderState = 6;
  211. entity.SupplierId = 35;
  212. entity.FinishPlaceTime = DateTime.Now;
  213. bool sendResult = commonHelper.SetOrderDummyDelivery(entity.tid);
  214. if (sendResult)
  215. {
  216. entity.OrderState = 7;
  217. }
  218. }
  219. else
  220. entity.OrderState = 5; //设计完成
  221. //if (entity.IsReturn == 2) //2是下单人打回给设计的,重新上传的话,需要清除打回
  222. //{
  223. entity.IsXianHuo = 0;
  224. entity.IsVerifyToSupplier = false;
  225. //}
  226. }
  227. entity.IsReturn = 0;
  228. if (entity.MemoOpt == 1 || entity.MemoOpt == 2)
  229. {
  230. entity.MemoOpt = 0;
  231. }
  232. if (entity.payment < 500)
  233. {
  234. if (entity.seller_memo.Contains("插卡") && entity.ProductCount != null && !entity.seller_memo.Contains("S_"))
  235. {
  236. int num = 0;
  237. try
  238. {
  239. string text = entity.ProductCount;
  240. string pattern = @"(\d+)\s*张";
  241. MatchCollection matches = Regex.Matches(text, pattern);
  242. foreach (Match match in matches)
  243. {
  244. if (match.Success)
  245. {
  246. num = Convert.ToInt32(match.Groups[1].Value);
  247. break;
  248. }
  249. }
  250. }
  251. catch (Exception ex)
  252. {
  253. }
  254. if (num >= 100)
  255. {
  256. entity.IsVerifyToSupplier = true;
  257. entity.SupplierId = 3;
  258. entity.FinishPlaceTime = DateTime.Now;
  259. }
  260. }
  261. if ((((entity.seller_memo.Contains("条幅彩色") && !entity.seller_memo.Contains("辽宁") && !entity.seller_memo.Contains("山东")) || entity.seller_memo.Contains("贡锻布") || entity.seller_memo.Contains("贡缎布") || entity.seller_memo.Contains("旗帜布")) && !entity.seller_memo.Contains("双喷")) || (entity.seller_memo.Contains("帆布") && !entity.seller_memo.Contains("帆布袋")))
  262. {
  263. entity.IsVerifyToSupplier = true;
  264. entity.SupplierId = 98;
  265. entity.FinishPlaceTime = DateTime.Now;
  266. }
  267. }
  268. if(entity.ProductId == 2690)
  269. {
  270. entity.IsVerifyToSupplier = true;
  271. entity.SupplierId = 97;
  272. entity.FinishPlaceTime = DateTime.Now;
  273. }
  274. if(entity.ProductId == 2701)
  275. {
  276. entity.IsVerifyToSupplier = true;
  277. entity.SupplierId = 98;
  278. entity.FinishPlaceTime = DateTime.Now;
  279. }
  280. string dPath = entity.FinishDesignTime.GetValueOrDefault().ToString("yyyyMMdd");
  281. //XLog.SaveLog(5, dPath);
  282. string sPath = Path.Combine(upPath, dPath);
  283. string cpath = Path.Combine(upPath, entity.FinishDesignTime.GetValueOrDefault().ToString("yyyyMM"));
  284. //XLog.SaveLog(5, sPath);
  285. //XLog.SaveLog(5, Directory.Exists(sPath).ToString());
  286. if (!Directory.Exists(sPath)) Directory.CreateDirectory(sPath);
  287. if (!Directory.Exists(cpath)) Directory.CreateDirectory(cpath);
  288. string saveFile = Path.Combine(sPath, file_name);
  289. string f_ext = Path.GetExtension(saveFile);
  290. string[] extArr = new string[] { ".cdr", ".zip", ".rar", ".pdf" };
  291. string _file = "";
  292. foreach (string ext in extArr)
  293. {
  294. if (f_ext != ext)
  295. {
  296. _file = saveFile.Replace(f_ext, ext);
  297. if (File.Exists(_file)) File.Delete(_file);
  298. }
  299. else
  300. {
  301. if (File.Exists(saveFile)) File.Delete(saveFile);
  302. }
  303. }
  304. //上传文件
  305. postFile.SaveAs(saveFile);
  306. entity.FileMd5 = GetMD5HashFromFile(postFile);
  307. //XLog.SaveLog(0, saveFile);
  308. entity.UpdateTime = DateTime.Now;
  309. entity.Update();
  310. //CeErpTradeCell.UpdateRelationOrder(entity);
  311. CeErpTradeCell.UpdateRelationOrder(entity.ctid);
  312. CeErpTradeLog.AddLog(entity.ctid, entity.OrderState, userId, "上传设计文件-" + saveFile);
  313. if (ctid.IndexOf("S_") >= -1)
  314. {
  315. StringBuilder sql = new StringBuilder();
  316. sql.AppendFormat("select * from Ce_ErpTradeCellExtend where ctid='{0}'", entity.ctid);
  317. DataTable cellEx = CeErpTradeCell.ExecuteDataset(sql.ToString()).Tables[0];
  318. if (cellEx.Rows.Count > 0)
  319. {
  320. string txtReprintTime = cellEx.Rows[0]["ReprintTime"].ToString();
  321. if (!string.IsNullOrEmpty(txtReprintTime) && !"null".Equals(txtReprintTime))
  322. {
  323. DateTime reprint = DateTime.Parse(txtReprintTime);
  324. if (DateTime.Compare(DateTime.Now.AddHours(-24), reprint) >= 0)
  325. {
  326. string parentId = entity.ctid.Substring(2);
  327. string update_sql = string.Format("update Ce_ErpTradeAfterSaleExtend set ReprintOut = ReprintOut+1 where tid= '{0}' ;", parentId);
  328. CeErpTradeLog.ExecuteNonQuery(update_sql);
  329. }
  330. }
  331. }
  332. }
  333. if (Path.GetExtension(saveFile).IndexOf("cdr", StringComparison.OrdinalIgnoreCase) != -1)
  334. {
  335. //string sql = string.Format("insert into s_cdrtopng(name,addtime)values('{0}',getdate()) ;", saveFile);
  336. //CeErpTradeLog.ExecuteNonQuery(sql);
  337. }
  338. else if (Path.GetExtension(saveFile).IndexOf("zip", StringComparison.OrdinalIgnoreCase) != -1 || Path.GetExtension(saveFile).IndexOf("rar", StringComparison.OrdinalIgnoreCase) != -1)
  339. {
  340. try
  341. {
  342. DecompressZIPandRAR(saveFile, sPath, file_name);
  343. }
  344. catch (Exception ex)
  345. {
  346. CeErpTradeLog.AddLog(entity.ctid, entity.OrderState, entity.DesignUserId, "解压失败!");
  347. XLog.SaveLog(0, "上传解压发生错误!" + ex.Message);
  348. }
  349. //Decompress(saveFile,sPath);
  350. }
  351. //new Thread(new ThreadStart(delegate ()
  352. //{
  353. //System.Threading.Thread.Sleep(2000);
  354. if (Path.GetExtension(saveFile).IndexOf("cdr", StringComparison.OrdinalIgnoreCase) != -1)
  355. {
  356. var identity = WindowsIdentity.GetCurrent();
  357. var principal = new WindowsPrincipal(identity);
  358. writeLog("上传完成!" + saveFile);
  359. Task.Run(async () =>
  360. {
  361. string filePath = Path.GetFullPath(saveFile);
  362. string targPath = Path.Combine(cpath, file_name);
  363. using (identity.Impersonate())
  364. {
  365. try
  366. {
  367. if (File.Exists(filePath))
  368. {
  369. cdrConvert.CdrConvertPng(filePath, targPath);
  370. }
  371. }
  372. catch (Exception ex)
  373. {
  374. XLog.SaveLog(0, filePath + ",转成图片出错:" + ex.Message);
  375. }
  376. }
  377. });
  378. }
  379. //})).Start();
  380. conSuc("上传成功!");
  381. return;
  382. }
  383. else
  384. {
  385. conErc("找不到对应的订单");
  386. return;
  387. }
  388. }
  389. catch (Exception ex)
  390. {
  391. conErc("上传发生错误!" + CommonHelper.FormatTextArea(ex.Message));
  392. CeErpTradeLog.AddLog(entity.ctid, entity.OrderState, entity.DesignUserId, "上传失败!");
  393. XLog.SaveLog(0, "上传发生错误!" + ex.Message);
  394. return;
  395. }
  396. finally
  397. {
  398. if (postFile != null)
  399. {
  400. postFile.InputStream.Close();
  401. }
  402. }
  403. }
  404. conErc("空文件!");
  405. }
  406. class CdrConvert
  407. {
  408. private object lockObject = new object();
  409. public void CdrConvertPng(string filePath, string targPath)
  410. {
  411. lock (lockObject)
  412. {
  413. writeLog("开始截图!" + filePath);
  414. targPath = targPath.Replace(".cdr", ".png");
  415. try
  416. {
  417. Application cdr = new Application();
  418. cdr.OpenDocument(filePath, 1);
  419. cdr.ActiveDocument.ExportBitmap(
  420. targPath,
  421. cdrFilter.cdrPNG,
  422. cdrExportRange.cdrCurrentPage,
  423. cdrImageType.cdrRGBColorImage,
  424. 0, 0, 72, 72,
  425. cdrAntiAliasingType.cdrNoAntiAliasing,
  426. false,
  427. true,
  428. true,
  429. false,
  430. cdrCompressionType.cdrCompressionNone,
  431. null).Finish();
  432. cdr.ActiveDocument.Close();
  433. cdr.Quit();
  434. cdr = null;
  435. }
  436. catch (Exception ex)
  437. {
  438. XLog.SaveLog(0, targPath + ",转成图片出错:" + ex.Message);
  439. }
  440. finally
  441. {
  442. KillProcessByName("CorelDRW");
  443. }
  444. }
  445. }
  446. void KillProcessByName(string processName)
  447. {
  448. Process[] processes = Process.GetProcessesByName(processName);
  449. foreach (Process process in processes)
  450. {
  451. try
  452. {
  453. process.Kill();
  454. process.WaitForExit(); // 等待进程退出
  455. }
  456. catch (Exception ex)
  457. {
  458. }
  459. }
  460. }
  461. }
  462. private void DecompressZIPandRAR(string zipFile, string targetPath, string filename)
  463. {
  464. string notExtension = filename.Substring(0, filename.Length - 4);
  465. SevenZipExtractor.SetLibraryPath(Server.MapPath("bin\\7z.dll"));
  466. LibraryFeature lf = SevenZipExtractor.CurrentLibraryFeatures;
  467. using (SevenZipExtractor szExtra = new SevenZipExtractor(zipFile))
  468. {
  469. //szExtra.ExtractArchive("d:\\temp");
  470. foreach (string afn in szExtra.ArchiveFileNames)
  471. {
  472. if (afn.IndexOf(notExtension, StringComparison.OrdinalIgnoreCase) != -1)
  473. {
  474. szExtra.ExtractFiles(targetPath, afn);
  475. break;
  476. }
  477. }
  478. }
  479. }
  480. private void Decompress(string GzipFile, string targetPath)
  481. {
  482. //string directoryName = Path.GetDirectoryName(targetPath + "\\") + "\\";
  483. string directoryName = targetPath + "\\";
  484. if (!Directory.Exists(directoryName)) Directory.CreateDirectory(directoryName);//生成解压目录
  485. //helper.writeLog(GzipFile);
  486. //helper.writeLog(directoryName);
  487. string CurrentDirectory = directoryName;
  488. byte[] data = new byte[2048];
  489. int size = 2048;
  490. ZipEntry theEntry = null;
  491. Stream _stream = File.OpenRead(GzipFile);
  492. if (_stream.Length == 0) { _stream.Close(); return; }
  493. try
  494. {
  495. using (ZipInputStream s = new ZipInputStream(_stream))
  496. {
  497. while ((theEntry = s.GetNextEntry()) != null)
  498. {
  499. if (theEntry.IsDirectory)
  500. {// 该结点是目录
  501. if (!Directory.Exists(CurrentDirectory + theEntry.Name)) Directory.CreateDirectory(CurrentDirectory + theEntry.Name);
  502. }
  503. else
  504. {
  505. if (theEntry.Name != String.Empty && (theEntry.Name.IndexOf(".png", StringComparison.OrdinalIgnoreCase) != -1 || theEntry.Name.IndexOf(".jpg", StringComparison.OrdinalIgnoreCase) != -1))
  506. {
  507. //解压文件到指定的目录
  508. using (FileStream streamWriter = File.Create(CurrentDirectory + theEntry.Name))
  509. {
  510. while (true)
  511. {
  512. size = s.Read(data, 0, data.Length);
  513. if (size <= 0) break;
  514. streamWriter.Write(data, 0, size);
  515. }
  516. streamWriter.Close();
  517. }
  518. break;
  519. }
  520. }
  521. }
  522. s.Close();
  523. }
  524. }
  525. catch (Exception ex)
  526. {
  527. XLog.SaveLog(0, "解压uncau:" + ex.Message);
  528. }
  529. }
  530. public string unCompressRAR(string unRarPatch, string rarPatch, string rarName)
  531. {
  532. string the_rar;
  533. RegistryKey the_Reg;
  534. object the_Obj;
  535. string the_Info;
  536. try
  537. {
  538. the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
  539. the_Obj = the_Reg.GetValue("");
  540. the_rar = the_Obj.ToString();
  541. the_Reg.Close();
  542. if (Directory.Exists(unRarPatch) == false)
  543. {
  544. Directory.CreateDirectory(unRarPatch);
  545. }
  546. the_Info = "x " + rarName + " " + unRarPatch + " -y";
  547. ProcessStartInfo the_StartInfo = new ProcessStartInfo();
  548. the_StartInfo.FileName = the_rar;
  549. the_StartInfo.Arguments = the_Info;
  550. the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  551. the_StartInfo.WorkingDirectory = rarPatch;//获取压缩包路径
  552. Process the_Process = new Process();
  553. the_Process.StartInfo = the_StartInfo;
  554. the_Process.Start();
  555. the_Process.WaitForExit();
  556. the_Process.Close();
  557. }
  558. catch (Exception ex)
  559. {
  560. throw ex;
  561. }
  562. return unRarPatch;
  563. }
  564. private void ShellCdrConvertPng(string path, string targPath)
  565. {
  566. ShellFile shellFile = ShellFile.FromFilePath(path);
  567. System.Drawing.Bitmap shellThumb = shellFile.Thumbnail.ExtraLargeBitmap;
  568. //在画板的指定位置画图
  569. targPath = targPath.Replace(".cdr", ".png");
  570. shellThumb.Save(targPath, ImageFormat.Png);
  571. shellThumb.Dispose();
  572. shellFile.Dispose();
  573. }
  574. private void CdrConvertPng(string path, string targPath)
  575. {
  576. using (Aspose.Imaging.FileFormats.Cdr.CdrImage image = (Aspose.Imaging.FileFormats.Cdr.CdrImage)Aspose.Imaging.Image.Load(path))
  577. {
  578. PngOptions options = new Aspose.Imaging.ImageOptions.PngOptions();
  579. options.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
  580. // Set rasterization options for fileformat
  581. options.VectorRasterizationOptions = (Aspose.Imaging.ImageOptions.VectorRasterizationOptions)
  582. image.GetDefaultOptions(new object[] { Aspose.Imaging.Color.White, image.Width, image.Height });
  583. options.VectorRasterizationOptions.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
  584. options.VectorRasterizationOptions.SmoothingMode = Aspose.Imaging.SmoothingMode.None;
  585. targPath = targPath.Replace(".cdr", ".png");
  586. image.Save(targPath, options);
  587. options.Dispose();
  588. }
  589. }
  590. private string CdrExPng(string path, string targPath)
  591. {
  592. try
  593. {
  594. targPath = targPath.Replace(".cdr", ".png");
  595. Application cdr = new Application();
  596. cdr.OpenDocument(path, 1);
  597. cdr.ActiveDocument.ExportBitmap(
  598. targPath,
  599. cdrFilter.cdrPNG,
  600. cdrExportRange.cdrCurrentPage,
  601. cdrImageType.cdrRGBColorImage,
  602. 0, 0, 72, 72,
  603. cdrAntiAliasingType.cdrNoAntiAliasing,
  604. false,
  605. true,
  606. true,
  607. false,
  608. cdrCompressionType.cdrCompressionNone,
  609. null).Finish();
  610. cdr.ActiveDocument.Close();
  611. cdr.Quit();
  612. cdr = null;
  613. GC.Collect();
  614. }
  615. catch (Exception ex)
  616. {
  617. XLog.SaveLog(0, path + ",转成图片出错:" + ex.Message);
  618. }
  619. finally
  620. {
  621. }
  622. return "111";
  623. }
  624. private static object cdrpngobj = new object();
  625. private void CdrExportPng(string path, string cdrFile)
  626. {
  627. lock (cdrpngobj)
  628. {
  629. string fname = curPath + "\\" + Path.GetFileNameWithoutExtension(cdrFile) + ".png";
  630. string new_fname = path + "\\" + Path.GetFileNameWithoutExtension(cdrFile) + ".png";
  631. CorelDRAW.Application cdr = new CorelDRAW.Application();
  632. cdr.OpenDocument(cdrFile, 1);
  633. cdr.ActiveDocument.ExportBitmap(
  634. fname,
  635. CorelDRAW.cdrFilter.cdrPNG,
  636. CorelDRAW.cdrExportRange.cdrCurrentPage,
  637. CorelDRAW.cdrImageType.cdrRGBColorImage,
  638. 0, 0, 72, 72,
  639. CorelDRAW.cdrAntiAliasingType.cdrNoAntiAliasing,
  640. false,
  641. true,
  642. true,
  643. false,
  644. CorelDRAW.cdrCompressionType.cdrCompressionNone,
  645. null).Finish();
  646. cdr.ActiveDocument.Close();
  647. cdr.Quit();
  648. if (File.Exists(fname))
  649. {
  650. File.Copy(fname, new_fname);
  651. File.Delete(fname);
  652. }
  653. }
  654. }
  655. public static string MidStrEx(string sourse, string startstr, string endstr)
  656. {
  657. string result = string.Empty;
  658. int startindex, endindex;
  659. try
  660. {
  661. startindex = sourse.IndexOf(startstr);
  662. if (startindex == -1)
  663. return result;
  664. string tmpstr = sourse.Substring(startindex + startstr.Length);
  665. endindex = tmpstr.IndexOf(endstr);
  666. if (endindex == -1)
  667. return result;
  668. result = tmpstr.Remove(endindex);
  669. }
  670. catch (Exception ex)
  671. {
  672. Console.WriteLine("MidStrEx Err:" + ex.Message);
  673. }
  674. return result;
  675. }
  676. private static string logPath = ConfigurationManager.AppSettings["curPath"] + "\\log";
  677. private static object logFlag = new object();
  678. public static void writeLog(string log)
  679. {
  680. lock (logFlag)
  681. {
  682. using (FileStream fileStream = new FileStream(logPath + "\\" + DateTime.Now.ToString("yy-MM-dd") + ".log", FileMode.Append, FileAccess.Write))
  683. {
  684. using (StreamWriter sw = new StreamWriter(fileStream, Encoding.Default))
  685. {
  686. sw.Write(log + " ------时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
  687. sw.Flush();
  688. }
  689. }
  690. }
  691. }
  692. }