Guest User

Untitled

a guest
Jul 12th, 2020
1,944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 35.59 KB | None | 0 0
  1. using CACodec;
  2. using PXCView36;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Windows.Controls;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16.  
  17.  
  18. public class ReadPagePair
  19. {
  20. public event EventHandler<imageSourceRenderedResultEventArgs> leftImageSourceRendered;
  21. public event EventHandler<imageSourceRenderedResultEventArgs> rightImageSourceRendered;
  22. private event EventHandler<imageSourceRenderedResultEventArgs> resizeImageSourceRendered;
  23. public event EventHandler<imageSourceRenderedResultEventArgs> sendresizeImageSourceRendered;
  24.  
  25. public int leftPageIndex;
  26. public int rightPageIndex;
  27. public ImageStatus imgStatus;
  28.  
  29. public string leftImagePath;
  30. public string rightImagePath;
  31.  
  32. public string leftPageId;
  33. public string rightPageId;
  34.  
  35. public ImageSource leftImageSource;
  36. public ImageSource rightImageSource;
  37. public ImageSource resizedImageSource;
  38.  
  39. public byte[][] decodedPDFPages = new byte[2][];
  40.  
  41. private int PDFdpi = 96;
  42.  
  43. public float PDFScale = 1.0F;
  44. public double baseScale = 1;
  45. public int zoomStep = 0;
  46. public bool isRendering = false;
  47. public List<Thread> zoomThread = new List<Thread>();
  48. private DateTime lastTimeOfZooming;
  49. private bool isResizing = false;
  50.  
  51. private int renderPieces = 9;
  52.  
  53. public ReadPagePair(int leftPageIndex, int rightPageIndex, string leftImgPath, string rightImgPath, string _leftPageId, string _rightPageId, int PDFdpi)
  54. {
  55. this.PDFdpi = PDFdpi;
  56. this.leftPageIndex = leftPageIndex;
  57. this.rightPageIndex = rightPageIndex;
  58. this.imgStatus = ImageStatus.SMALLIMAGE;
  59. this.leftImagePath = leftImgPath;
  60. this.rightImagePath = rightImgPath;
  61.  
  62. this.leftPageId = _leftPageId;
  63. this.rightPageId = _rightPageId;
  64. }
  65.  
  66. public void resizeLargePHEJBitmapImage(CACodecTools caTool, byte[] curKey, Border border, float PDFScale, int zoomStep, DateTime resizeTime, bool isSinglePage)
  67. {
  68. this.lastTimeOfZooming = resizeTime;
  69. this.PDFScale = PDFScale;
  70. this.zoomStep = zoomStep;
  71.  
  72. if (leftImagePath.Length > 0)
  73. {
  74. if (rightImagePath.Length > 0)
  75. {
  76.  
  77. Thread thread = new Thread(() => getPHEJDoubleBitmapImageAsync(caTool, curKey, leftImagePath, rightImagePath, PDFScale, leftPageIndex, border, zoomimageSourceRendered, isSinglePage));
  78. thread.Name = PDFScale.ToString();
  79. zoomThread.Add(thread);
  80. }
  81. else
  82. {
  83.  
  84. Thread thread = new Thread(() => getPHEJSingleBitmapImageAsync(caTool, curKey, leftImagePath, PDFScale, leftPageIndex, border, zoomimageSourceRendered, isSinglePage));
  85. thread.Name = PDFScale.ToString();
  86. zoomThread.Add(thread);
  87. }
  88. }
  89. else if (rightImagePath.Length > 0)
  90. {
  91.  
  92. Thread thread = new Thread(() => getPHEJSingleBitmapImageAsync(caTool, curKey, rightImagePath, PDFScale, leftPageIndex, border, zoomimageSourceRendered, isSinglePage));
  93. thread.Name = PDFScale.ToString();
  94. zoomThread.Add(thread);
  95. }
  96.  
  97.  
  98. if (!zoomThread.Count.Equals(0) && !isResizing)
  99. {
  100. for (int i = zoomThread.Count - 1; i >= 0; i--)
  101. {
  102. if (PDFScale.Equals(((float)Convert.ToDouble(zoomThread[i].Name))))
  103. {
  104. try
  105. {
  106. this.resizeImageSourceRendered += zoomimageSourceRendered;
  107. zoomThread[i].Start();
  108. zoomThread.Clear();
  109. isResizing = true;
  110. break;
  111. }
  112. catch
  113. {
  114.  
  115. continue;
  116. }
  117. }
  118. }
  119. }
  120. }
  121.  
  122. public void createLargeHEJBitmapImage(CACodecTools caTool, byte[] curKey)
  123. {
  124. if (leftImagePath.Length > 0)
  125. {
  126. if (rightImagePath.Length > 0)
  127. {
  128.  
  129. this.leftImageSource = getHEJDoubleBitmapImage(caTool, curKey);
  130. }
  131. else
  132. {
  133.  
  134. this.leftImageSource = getHEJSingleBitmapImage(caTool, curKey);
  135. }
  136. }
  137. else if (rightImagePath.Length > 0)
  138. {
  139.  
  140. this.rightImageSource = getHEJSingleBitmapImage(caTool, curKey);
  141. }
  142. }
  143.  
  144. public void createLargePHEJBitmapImage(CACodecTools caTool, byte[] curKey, Border border, bool isSinglePage)
  145. {
  146. if (leftImagePath.Length > 0)
  147. {
  148. if (rightImagePath.Length > 0)
  149. {
  150.  
  151. this.leftImageSourceRendered += leftimageSourceRendered;
  152. Thread thread = new Thread(() => getPHEJDoubleBitmapImageAsync(caTool, curKey, leftImagePath, rightImagePath, PDFScale, leftPageIndex, border, leftimageSourceRendered, isSinglePage));
  153. thread.Name = PDFScale.ToString();
  154. thread.Start();
  155. isRendering = true;
  156.  
  157. }
  158. else
  159. {
  160.  
  161. this.leftImageSourceRendered += leftimageSourceRendered;
  162. Thread thread = new Thread(() => getPHEJSingleBitmapImageAsync(caTool, curKey, leftImagePath, PDFScale, leftPageIndex, border, leftimageSourceRendered, isSinglePage));
  163. thread.Name = PDFScale.ToString();
  164. thread.Start();
  165. isRendering = true;
  166. }
  167. }
  168. else if (rightImagePath.Length > 0)
  169. {
  170.  
  171. this.rightImageSourceRendered += rightimageSourceRendered;
  172. Thread thread = new Thread(() => getPHEJSingleBitmapImageAsync(caTool, curKey, rightImagePath, PDFScale, leftPageIndex, border, leftimageSourceRendered, isSinglePage));
  173. thread.Name = PDFScale.ToString();
  174. thread.Start();
  175. isRendering = true;
  176. }
  177. }
  178.  
  179. #region PDF Reading Pages
  180.  
  181. public class NativeMethods
  182. {
  183. [DllImport("ole32.dll")]
  184. public static extern void CoTaskMemFree(IntPtr pv);
  185.  
  186. [DllImport("ole32.dll")]
  187. public static extern IntPtr CoTaskMemAlloc(IntPtr cb);
  188.  
  189. [DllImport("libpdf2jpg.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]
  190. public static extern IntPtr pdfLoadFromMemory(int dpi, float scale, IntPtr ibuf, int ilen, IntPtr obptr, IntPtr olptr, int pgs);
  191.  
  192. [DllImport("libpdf2jpg.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]
  193. public static extern int pdfNumberOfPages(IntPtr ibuf, int pgs);
  194.  
  195. [DllImport("libpdf2jpg.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]
  196. public static extern int pdfPageSize(int dpi, float scale, IntPtr ibuf, int ilen, IntPtr pWidth, IntPtr pHeight, int pgs);
  197.  
  198. [DllImport("libpdf2jpg.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]
  199. public static extern IntPtr pdfLoadFromMemoryPartial(int dpi, float scale, IntPtr ibuf, int ilen, IntPtr obptr, IntPtr olptr, int x0, int y0, int x1, int y1,
  200. int pgs);
  201. }
  202.  
  203.  
  204. private Bitmap renPdfToBitmap(CACodecTools caTool, string pageFile, byte[] key, int pg, int dpi, float scal, int decodedPageIndex, Border border, bool isSinglePage)
  205. {
  206. System.Drawing.Color bgColor = System.Drawing.Color.White;
  207. Bitmap bmp = null;
  208. try
  209. {
  210. if (this.decodedPDFPages[decodedPageIndex] == null)
  211. this.decodedPDFPages[decodedPageIndex] = caTool.fileAESDecode(pageFile, key);
  212. }
  213. catch (Exception e)
  214. {
  215.  
  216. this.decodedPDFPages[decodedPageIndex] = null;
  217. throw e;
  218. }
  219.  
  220. try
  221. {
  222. PDFDoc pdfDoc = new PDFDoc();
  223. pdfDoc.Init("PVD20-M4IRG-QYZK9-MNJ2U-DFTK1-MAJ4L", "PDFX3$Henry$300604_Allnuts#");
  224. pdfDoc.OpenFromMemory(this.decodedPDFPages[decodedPageIndex], (uint)decodedPDFPages[decodedPageIndex].Length, 0);
  225. PXCV_Lib36.PXV_CommonRenderParameters commonRenderParam = prepareCommonRenderParameter(pdfDoc, dpi, pg, scal, 0, 0, border, isSinglePage);
  226. pdfDoc.DrawPageToDIBSection(IntPtr.Zero, pg, bgColor, commonRenderParam, out bmp);
  227. pdfDoc.ReleasePageCachedData(pg, (int)PXCV_Lib36.PXCV_ReleaseCachedDataFlags.pxvrcd_ReleaseDocumentImages);
  228. pdfDoc.Delete();
  229. }
  230. catch (Exception e)
  231. {
  232. throw e;
  233. }
  234. //bmp.Save("c:\\Temp\\test.bmp");
  235. return bmp;
  236. }
  237.  
  238.  
  239. private PXCV_Lib36.PXV_CommonRenderParameters prepareCommonRenderParameter(PDFDoc pdfDoc, int dpi, int pageNumber, float zoom, int offsetX, int offsetY, Border border, bool isSinglePage)
  240. {
  241. IntPtr p1 = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PXCV_Helper.RECT)));
  242. IntPtr p2 = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PXCV_Helper.RECT)));
  243. System.Drawing.Point m_Offset = new System.Drawing.Point(offsetX, offsetY);
  244. System.Drawing.Size aPageSize = System.Drawing.Size.Empty;
  245. PXCV_Helper.RECT aWholePage = new PXCV_Helper.RECT();
  246. PXCV_Helper.RECT aDrawRect = new PXCV_Helper.RECT();
  247. PXCV_Lib36.PXV_CommonRenderParameters commonRenderParam = new PXCV_Lib36.PXV_CommonRenderParameters();
  248. PageDimension aPageDim;
  249. pdfDoc.GetPageDimensions(pageNumber, out aPageDim.w, out aPageDim.h);
  250.  
  251.  
  252. double borderHeight = (border.ActualHeight / (double)96) * dpi;
  253. double borderWidth = (border.ActualWidth / (double)96) * dpi;
  254.  
  255. if (zoomStep == 0)
  256. {
  257. //PDF原尺吋
  258. aPageSize.Width = (int)((aPageDim.w / 72.0 * dpi) * zoom);
  259. aPageSize.Height = (int)((aPageDim.h / 72.0 * dpi) * zoom);
  260.  
  261. double borderRatio = borderWidth / borderHeight;
  262. double renderImageRatio = 0;
  263.  
  264. if (isSinglePage)
  265. {
  266. renderImageRatio = (double)aPageSize.Width / (double)aPageSize.Height;
  267. }
  268. else
  269. {
  270. renderImageRatio = (double)(aPageSize.Width * 2) / (double)aPageSize.Height;
  271. }
  272.  
  273. if (aPageSize.Width < borderWidth && aPageSize.Height < borderHeight)
  274. {
  275. double newPageW, newPageH;
  276. if (renderImageRatio > borderRatio)
  277. {
  278. newPageW = borderWidth / 2;
  279. baseScale = newPageW / (double)aPageSize.Width;
  280. newPageH = Math.Round(baseScale * (double)aPageSize.Height, 2);
  281. }
  282. else
  283. {
  284. newPageH = borderHeight;
  285. baseScale = newPageH / (double)aPageSize.Height;
  286. newPageW = Math.Round(baseScale * (double)aPageSize.Width, 2);
  287. }
  288.  
  289. aPageSize.Width = (int)newPageW;
  290. aPageSize.Height = (int)newPageH;
  291. }
  292. else
  293. {
  294. double newPageW, newPageH;
  295. if (renderImageRatio > borderRatio)
  296. {
  297. newPageW = borderWidth / 2;
  298. baseScale = newPageW / (double)aPageSize.Width;
  299. newPageH = Math.Round(baseScale * (double)aPageSize.Height, 2);
  300. }
  301. else
  302. {
  303. newPageH = borderHeight;
  304. baseScale = newPageH / (double)aPageSize.Height;
  305. newPageW = Math.Round(baseScale * (double)aPageSize.Width, 2);
  306. }
  307.  
  308. aPageSize.Width = (int)newPageW;
  309. aPageSize.Height = (int)newPageH;
  310. }
  311. }
  312. else
  313. {
  314.  
  315. aPageSize.Width = (int)((aPageDim.w / 72.0 * dpi) * zoom * baseScale);
  316. aPageSize.Height = (int)((aPageDim.h / 72.0 * dpi) * zoom * baseScale);
  317. }
  318.  
  319. //Region rgn1 = new Region(new System.Drawing.Rectangle(-m_Offset.X, -m_Offset.Y, aPageSize.Width, aPageSize.Height));
  320. //rgn1.Complement(new System.Drawing.Rectangle(0, 0, (int)borderWidth, (int)borderHeight));
  321. //rgn1.Complement(new System.Drawing.Rectangle(0, 0, aPageSize.Width, aPageSize.Height));
  322. aWholePage.left = -m_Offset.X;
  323. aWholePage.top = -m_Offset.Y;
  324. aWholePage.right = aWholePage.left + aPageSize.Width;
  325. aWholePage.bottom = aWholePage.top + aPageSize.Height;
  326.  
  327.  
  328. aDrawRect.left = 0;
  329. aDrawRect.top = 0;
  330. if (zoomStep == 0)
  331. {
  332. if (aPageSize.Width < borderWidth)
  333. {
  334. aDrawRect.right = aPageSize.Width;
  335. }
  336. else
  337. {
  338. aDrawRect.right = (int)borderWidth;
  339. }
  340. if (aPageSize.Height < borderHeight)
  341. {
  342. aDrawRect.bottom = aPageSize.Height;
  343. }
  344. else
  345. {
  346. aDrawRect.bottom = (int)borderHeight;
  347. }
  348. }
  349. else
  350. {
  351. aDrawRect.right = aPageSize.Width;
  352. aDrawRect.bottom = aPageSize.Height;
  353. }
  354.  
  355. //aDrawRect.right = aPageSize.Width;
  356. //aDrawRect.bottom = aPageSize.Height;
  357. Marshal.StructureToPtr(aWholePage, p1, false);
  358. Marshal.StructureToPtr(aDrawRect, p2, false);
  359. commonRenderParam.WholePageRect = p1;
  360. commonRenderParam.DrawRect = p2;
  361. commonRenderParam.RenderTarget = PXCV_Lib36.PXCV_RenderMode.pxvrm_Viewing;
  362. commonRenderParam.Flags = 0;
  363. //System.Drawing.Rectangle rc = new System.Drawing.Rectangle(0, 0, aControlSize.Width, aControlSize.Height);
  364. //System.Drawing.Rectangle rc = new System.Drawing.Rectangle(0, 0, aPageSize.Width, aPageSize.Height);
  365. //rc.Intersect(new System.Drawing.Rectangle(-m_Offset.X, -m_Offset.Y, aPageSize.Width, aPageSize.Height));
  366. //e.DrawRectangle(System.Windows.Media.Brushes.White, null, new Rect(new System.Windows.Size(rc.Width, rc.Height)));
  367. //aGraphics.FillRectangle(System.Drawing.Brushes.White, rc);
  368. //aGraphics.FillRegion(System.Drawing.Brushes.Gray, rgn1);
  369. //rgn1.Dispose();
  370.  
  371.  
  372.  
  373. return commonRenderParam;
  374. }
  375.  
  376. private void getPHEJSingleBitmapImageAsync(CACodecTools caTool, byte[] curKey, string pagePath, float scal, int curPageIndex, Border border, EventHandler<imageSourceRenderedResultEventArgs> ImageSourceRendered, bool isSinglePage)
  377. {
  378. BitmapImage bitmapImage = new BitmapImage();
  379.  
  380. if (pagePath.Contains("tryPageEndLarge"))
  381. return;
  382. Bitmap image = null;
  383. try
  384. {
  385. image = renPdfToBitmap(caTool, pagePath, curKey, 0, PDFdpi, scal, 0, border, isSinglePage);
  386.  
  387. using (MemoryStream memory = new MemoryStream())
  388. {
  389. image.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
  390. //memory.Position = 0;
  391. bitmapImage.BeginInit();
  392. bitmapImage.StreamSource = memory;
  393. bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  394. bitmapImage.EndInit();
  395. bitmapImage.CacheOption = BitmapCacheOption.None;
  396. bitmapImage.StreamSource.Close();
  397. bitmapImage.StreamSource = null;
  398. bitmapImage.Freeze();
  399.  
  400.  
  401. memory.Dispose();
  402. memory.Close();
  403. image.Dispose();
  404. image = null;
  405. }
  406. }
  407. catch (Exception ex)
  408. {
  409.  
  410. Debug.WriteLine("Error :" + ex.Message + " @ getPHEJDoubleBitmapImageAsync");
  411. bitmapImage = null;
  412. image = null;
  413. }
  414.  
  415. EventHandler<imageSourceRenderedResultEventArgs> imageRenderResult = ImageSourceRendered;
  416.  
  417. if (imageRenderResult != null)
  418. {
  419. imageRenderResult(this, new imageSourceRenderedResultEventArgs(bitmapImage, curPageIndex, scal));
  420. }
  421.  
  422. }
  423.  
  424. void zoomimageSourceRendered(object sender, imageSourceRenderedResultEventArgs e)
  425. {
  426. this.resizeImageSourceRendered -= zoomimageSourceRendered;
  427.  
  428. isResizing = false;
  429.  
  430. if (leftPageIndex.Equals(e.renderPageIndex))
  431. {
  432. if (PDFScale.Equals(e.sourceScale))
  433. {
  434. EventHandler<imageSourceRenderedResultEventArgs> SendResizeImage = sendresizeImageSourceRendered;
  435. if (SendResizeImage != null)
  436. {
  437. SendResizeImage(sender, e);
  438. }
  439. isRendering = false;
  440. zoomThread.Clear();
  441. zoomStep = 0;
  442. PDFScale = 1F;
  443. }
  444. else
  445. {
  446. for (int i = zoomThread.Count - 1; i >= 0; i--)
  447. {
  448. if (PDFScale.Equals(((float)Convert.ToDouble(zoomThread[i].Name))))
  449. {
  450. try
  451. {
  452. zoomThread[i].Start();
  453. this.resizeImageSourceRendered += zoomimageSourceRendered;
  454. isResizing = true;
  455. break;
  456. }
  457. catch
  458. {
  459.  
  460. continue;
  461. }
  462. }
  463. }
  464. }
  465. }
  466. else
  467. {
  468. isResizing = false;
  469. zoomThread.Clear();
  470. }
  471. }
  472.  
  473. void leftimageSourceRendered(object sender, imageSourceRenderedResultEventArgs e)
  474. {
  475. this.leftImageSourceRendered -= leftimageSourceRendered;
  476. if (e.imgSource != null)
  477. {
  478. this.leftImageSource = e.imgSource;
  479. isRendering = false;
  480. }
  481. }
  482.  
  483. void rightimageSourceRendered(object sender, imageSourceRenderedResultEventArgs e)
  484. {
  485. this.rightImageSourceRendered -= rightimageSourceRendered;
  486. if (e.imgSource != null)
  487. {
  488. this.rightImageSource = e.imgSource;
  489. isRendering = false;
  490. }
  491. }
  492.  
  493. private void getPHEJDoubleBitmapImageAsync(CACodecTools caTool, byte[] curKey, string leftPagePath, string rightPagePath, float scal, int curPageIndex, Border border, EventHandler<imageSourceRenderedResultEventArgs> ImageSourceRendered, bool isSinglePage)
  494. {
  495. BitmapImage bitmapImage = new BitmapImage();
  496. Bitmap image1 = null;
  497. Bitmap image2 = null;
  498. Bitmap bitmap = null;
  499. bool hasTryPage = false;
  500.  
  501. EventHandler<imageSourceRenderedResultEventArgs> imageRenderResult = ImageSourceRendered;
  502.  
  503. try
  504. {
  505.  
  506. if (leftPagePath.Contains("tryPageEndLarge"))
  507. {
  508. if (leftPagePath.Contains("tryPageEndLarge.pdf"))
  509. leftPagePath = leftPagePath.Replace("tryPageEndLarge.pdf", "tryPageEnd.jpg");
  510. else if (leftPagePath.Contains("tryPageEndLargeNTPC.pdf"))
  511. leftPagePath = leftPagePath.Replace("tryPageEndLargeNTPC.pdf", "tryPageEndNTPC.jpg");
  512.  
  513. hasTryPage = true;
  514. BitmapImage img1 = new BitmapImage(new Uri(leftPagePath, UriKind.RelativeOrAbsolute));
  515.  
  516. using (MemoryStream outStream = new MemoryStream())
  517. {
  518. BitmapEncoder enc = new BmpBitmapEncoder();
  519. enc.Frames.Add(BitmapFrame.Create(img1));
  520. enc.Save(outStream);
  521. image1 = new System.Drawing.Bitmap(outStream);
  522. }
  523. }
  524. else
  525. {
  526. image1 = renPdfToBitmap(caTool, leftPagePath, curKey, 0, PDFdpi, scal, 0, border, isSinglePage);
  527. }
  528. }
  529. catch(Exception ex)
  530. {
  531.  
  532. Debug.WriteLine("Error :" + ex.Message + " @ getPHEJDoubleBitmapImageAsync");
  533. image1 = null;
  534. image2 = null;
  535. bitmap = null;
  536. bitmapImage = null;
  537.  
  538. if (imageRenderResult != null)
  539. {
  540. imageRenderResult(this, new imageSourceRenderedResultEventArgs(bitmapImage, curPageIndex, scal));
  541. }
  542. return;
  543. }
  544.  
  545. try
  546. {
  547. if (rightPagePath.Contains("tryPageEndLarge"))
  548. {
  549. if (rightPagePath.Contains("tryPageEndLarge.pdf"))
  550. rightPagePath = rightPagePath.Replace("tryPageEndLarge.pdf", "tryPageEnd.jpg");
  551. else if (rightPagePath.Contains("tryPageEndLargeNTPC.pdf"))
  552. rightPagePath = rightPagePath.Replace("tryPageEndLargeNTPC.pdf", "tryPageEndNTPC.jpg");
  553.  
  554. hasTryPage = true;
  555. BitmapImage img2 = new BitmapImage(new Uri(rightPagePath, UriKind.RelativeOrAbsolute));
  556. using (MemoryStream outStream = new MemoryStream())
  557. {
  558. BitmapEncoder enc = new BmpBitmapEncoder();
  559. enc.Frames.Add(BitmapFrame.Create(img2));
  560. enc.Save(outStream);
  561. image2 = new System.Drawing.Bitmap(outStream);
  562. }
  563. }
  564. else
  565. {
  566. image2 = renPdfToBitmap(caTool, rightPagePath, curKey, 0, PDFdpi, scal, 1, border, isSinglePage);
  567. }
  568.  
  569. }
  570. catch (Exception ex)
  571. {
  572.  
  573. Debug.WriteLine("Error :" + ex.Message + " @ getPHEJDoubleBitmapImageAsync");
  574. image1 = null;
  575. image2 = null;
  576. bitmap = null;
  577. bitmapImage = null;
  578.  
  579. if (imageRenderResult != null)
  580. {
  581. imageRenderResult(this, new imageSourceRenderedResultEventArgs(bitmapImage, curPageIndex, scal));
  582. }
  583. return;
  584. }
  585.  
  586. try
  587. {
  588. int mergeWidth = Convert.ToInt32(image1.Width + image2.Width);
  589. int mergeHeight = Convert.ToInt32(Math.Max(image1.Height, image2.Height));
  590.  
  591.  
  592. if (hasTryPage)
  593. {
  594. mergeWidth = Math.Min(image1.Width, image2.Width) * 2;
  595.  
  596. mergeHeight = Math.Min(image1.Height, image2.Height);
  597.  
  598. if (image1.Width > image2.Width)
  599. {
  600. image1 = ResizeImage(image1, new Size(image2.Width, image2.Height));
  601. mergeWidth = image2.Width * 2;
  602. }
  603. else
  604. {
  605. image2 = ResizeImage(image2, new Size(image1.Width, image1.Height));
  606. mergeWidth = image1.Width * 2;
  607. }
  608. }
  609.  
  610. bitmap = new Bitmap(mergeWidth, mergeHeight);
  611. using (Graphics g = Graphics.FromImage(bitmap))
  612. {
  613. g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
  614. g.DrawImage(image1, 0, 0, image1.Width, image1.Height);
  615. g.DrawImage(image2, image1.Width, 0, image2.Width, image2.Height);
  616. g.Dispose();
  617. }
  618.  
  619. using (MemoryStream memory = new MemoryStream())
  620. {
  621. bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
  622. //memory.Position = 0;
  623. bitmapImage.BeginInit();
  624. bitmapImage.StreamSource = memory;
  625. bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  626. bitmapImage.EndInit();
  627. bitmapImage.CacheOption = BitmapCacheOption.None;
  628. bitmapImage.StreamSource.Close();
  629. bitmapImage.StreamSource = null;
  630. bitmapImage.Freeze();
  631.  
  632.  
  633. memory.Dispose();
  634. memory.Close();
  635. bitmap.Dispose();
  636. bitmap = null;
  637. }
  638.  
  639. image1 = null;
  640. image2 = null;
  641. }
  642. catch
  643. {
  644.  
  645. image1 = null;
  646. image2 = null;
  647. bitmap = null;
  648. bitmapImage = null;
  649.  
  650. if (imageRenderResult != null)
  651. {
  652. imageRenderResult(this, new imageSourceRenderedResultEventArgs(bitmapImage, curPageIndex, scal));
  653. }
  654. return;
  655. }
  656.  
  657. if (imageRenderResult != null)
  658. {
  659. imageRenderResult(this, new imageSourceRenderedResultEventArgs(bitmapImage, curPageIndex, scal));
  660. }
  661. }
  662.  
  663. public static Bitmap ResizeImage(Bitmap imgToResize, Size size)
  664. {
  665. try
  666. {
  667. Bitmap b = new Bitmap(size.Width, size.Height);
  668. using (Graphics g = Graphics.FromImage((System.Drawing.Image)b))
  669. {
  670. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  671. g.DrawImage(imgToResize, 0, 0, size.Width, size.Height);
  672. }
  673. return b;
  674. }
  675. catch { }
  676. return null;
  677. }
  678.  
  679. #endregion
  680.  
  681. #region HEJ Reading Page
  682.  
  683. private BitmapImage getHEJSingleBitmapImage(CACodecTools caTool, byte[] curKey)
  684. {
  685. BitmapImage bigBitmapImage = new BitmapImage();
  686. try
  687. {
  688. using (MemoryStream bMapLast = caTool.fileAESDecode(leftImagePath, curKey, false))
  689. {
  690.  
  691. bigBitmapImage.BeginInit();
  692. bigBitmapImage.StreamSource = bMapLast;
  693. bigBitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  694. bigBitmapImage.EndInit();
  695. bigBitmapImage.CacheOption = BitmapCacheOption.None;
  696. bigBitmapImage.StreamSource.Close();
  697. bigBitmapImage.StreamSource = null;
  698. bigBitmapImage.Freeze();
  699.  
  700. bMapLast.Dispose();
  701. bMapLast.Close();
  702. }
  703. }
  704. catch (Exception e)
  705. {
  706.  
  707. throw e;
  708. }
  709. return bigBitmapImage;
  710. }
  711.  
  712. private BitmapImage getHEJDoubleBitmapImage(CACodecTools caTool, byte[] curKey)
  713. {
  714. BitmapImage bitmapImage = new BitmapImage();
  715. try
  716. {
  717. using (MemoryStream bMapLeft = caTool.fileAESDecode(leftImagePath, curKey, false))
  718. {
  719. using (MemoryStream bMapRight = caTool.fileAESDecode(rightImagePath, curKey, false))
  720. {
  721. //雙頁
  722. System.Drawing.Bitmap image1 = new Bitmap(bMapLeft);
  723. System.Drawing.Bitmap image2 = new Bitmap(bMapRight);
  724.  
  725. int mergeWidth = Convert.ToInt32(image1.Width + image2.Width);
  726. int mergeHeight = Convert.ToInt32(Math.Max(image1.Height, image2.Height));
  727.  
  728. Bitmap bitmap = new Bitmap(mergeWidth, mergeHeight);
  729. using (Graphics g = Graphics.FromImage(bitmap))
  730. {
  731. g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
  732. g.DrawImage(image1, 0, 0, image1.Width, image1.Height);
  733. g.DrawImage(image2, image1.Width, 0, image2.Width, image2.Height);
  734. g.Dispose();
  735. }
  736.  
  737. using (MemoryStream memory = new MemoryStream())
  738. {
  739. bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
  740. //memory.Position = 0;
  741. bitmapImage.BeginInit();
  742. bitmapImage.StreamSource = memory;
  743. bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  744. bitmapImage.EndInit();
  745. bitmapImage.CacheOption = BitmapCacheOption.None;
  746. bitmapImage.StreamSource.Close();
  747. bitmapImage.StreamSource = null;
  748. bitmapImage.Freeze();
  749. memory.Dispose();
  750. memory.Close();
  751. bitmap.Dispose();
  752. bitmap = null;
  753. }
  754.  
  755. bMapLeft.Dispose();
  756. bMapLeft.Close();
  757. bMapRight.Dispose();
  758. bMapRight.Close();
  759. image1 = null;
  760. image2 = null;
  761. }
  762. }
  763. }
  764. catch (Exception e)
  765. {
  766.  
  767. throw e;
  768. }
  769. return bitmapImage;
  770. }
  771.  
  772. #endregion
  773.  
  774. private byte[] getByteArrayFromImage(BitmapImage imageC)
  775. {
  776. byte[] data;
  777. JpegBitmapEncoder encoder = new JpegBitmapEncoder();
  778. if (imageC.UriSource != null)
  779. {
  780. encoder.Frames.Add(BitmapFrame.Create(imageC.UriSource));
  781. }
  782. else
  783. {
  784. encoder.Frames.Add(BitmapFrame.Create(imageC));
  785. }
  786.  
  787. using (MemoryStream ms = new MemoryStream())
  788. {
  789. encoder.Save(ms);
  790. data = ms.ToArray();
  791. ms.Close();
  792. ms.Dispose();
  793. encoder = null;
  794. imageC = null;
  795. return data;
  796. }
  797. }
  798. }
  799.  
  800. public enum ImageStatus
  801. {
  802. UNKNOWN = 0,
  803. SMALLIMAGE = 1,
  804. LARGEIMAGE = 2,
  805. GENERATING = 3
  806. }
  807.  
  808. public enum MediaCanvasOpenedBy
  809. {
  810. None = 0,
  811. SearchButton = 1,
  812. MediaButton = 2,
  813. CategoryButton = 3,
  814. NoteButton = 4,
  815. ShareButton = 5,
  816. SettingButton = 6,
  817. PenMemo = 7
  818. }
  819.  
  820. public enum SharedPlatform
  821. {
  822. None = 0,
  823. Facebook = 1,
  824. Plurk = 2,
  825. Mail = 3,
  826. Google = 4,
  827. Twitter = 5
  828. }
  829.  
  830. public enum PageMode
  831. {
  832. None = 0,
  833. SinglePage = 1,
  834. DoublePage = 2
  835. }
  836.  
  837. //PDF頁面大小
  838. public struct PageDimension
  839. {
  840. public double w;
  841. public double h;
  842. }
  843.  
  844. public class SearchRecord
  845. {
  846. public int targetPage { get; set; }
  847. public string showedPage { get; set; }
  848. public string targetLine { get; set; }
  849. public string imagePath { get; set; }
  850.  
  851. public SearchRecord(string _showedPage, string _targetLine, int _targetPage)
  852. {
  853. targetPage = _targetPage;
  854. targetLine = _targetLine;
  855. showedPage = _showedPage;
  856. }
  857.  
  858. }
  859.  
  860. public class ThumbnailImageAndPage : INotifyPropertyChanged
  861. {
  862. public string pageIndex { get; set; }
  863. public string rightImagePath { get; set; }
  864. public string leftImagePath { get; set; }
  865. public bool _isDownloaded;
  866.  
  867. public event PropertyChangedEventHandler PropertyChanged;
  868.  
  869. public bool isDownloaded
  870. {
  871. get
  872. {
  873. return _isDownloaded;
  874. }
  875. set
  876. {
  877. _isDownloaded = value;
  878. if (PropertyChanged != null)
  879. {
  880. PropertyChanged(this, new PropertyChangedEventArgs("isDownloaded"));
  881. }
  882. }
  883. }
  884.  
  885. public ThumbnailImageAndPage(string _pageIndex, string _rightImagePath, string _leftImagePath, bool downloadStatus)
  886. {
  887. pageIndex = _pageIndex;
  888. rightImagePath = _rightImagePath;
  889. leftImagePath = _leftImagePath;
  890. _isDownloaded = downloadStatus;
  891. }
  892. }
  893.  
  894. public class ShareButton
  895. {
  896. public string imagePath { get; set; }
  897. public string textShown { get; set; }
  898. public bool isShareButtonEnabled { get; set; }
  899. public SharedPlatform sharePlatForm { get; set; }
  900.  
  901. public ShareButton(string _imagePath, string _textShown, SharedPlatform _sharePlatForm, bool _isShareButtonEnabled)
  902. {
  903. imagePath = _imagePath;
  904. textShown = _textShown;
  905. sharePlatForm = _sharePlatForm;
  906. isShareButtonEnabled = _isShareButtonEnabled;
  907. }
  908. }
  909.  
  910.  
  911. public class imageSourceRenderedResultEventArgs : EventArgs
  912. {
  913. public BitmapImage imgSource;
  914. public int renderPageIndex;
  915. public float sourceScale;
  916. public imageSourceRenderedResultEventArgs(BitmapImage imgSource, int renderPageIndex, float sourceScale)
  917. {
  918. this.imgSource = imgSource;
  919. this.renderPageIndex = renderPageIndex;
  920. this.sourceScale = sourceScale;
  921. }
  922. }
  923. }
Add Comment
Please, Sign In to add comment