Guest User

Untitled

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