SnapshotMenu.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using ComPDFKit.Controls.Common;
  2. using ComPDFKit.Controls.Helper;
  3. using ComPDFKit.Controls.PDFControl;
  4. using ComPDFKit.Import;
  5. using ComPDFKit.PDFDocument;
  6. using ComPDFKit.PDFPage;
  7. using ComPDFKit.Tool;
  8. using ComPDFKitViewer;
  9. using Microsoft.Win32;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. namespace ComPDFKit.Controls.Snapshot
  18. {
  19. public partial class SnapshotMenu : UserControl
  20. {
  21. public SnapshotEditToolArgs SnapToolArgs { get; set; }
  22. public PDFViewControl PdfViewer { get; set; }
  23. public event EventHandler<KeyValuePair<string,object>> SnapToolEvent;
  24. public SnapshotMenu()
  25. {
  26. InitializeComponent();
  27. SetLangText();
  28. }
  29. private void SetLangText()
  30. {
  31. ToolTipService.SetToolTip(ReSnapshotBtn, LanguageHelper.CompressManager.GetString("ContentSelection_CancelDo"));
  32. ToolTipService.SetToolTip(SnapshotPrintBtn, LanguageHelper.CompressManager.GetString("Main_Print"));
  33. ToolTipService.SetToolTip(SnapshotSaveBtn, LanguageHelper.CompressManager.GetString("ContentSelection_Output"));
  34. ToolTipService.SetToolTip(SnapshotCloseBtn, LanguageHelper.CompressManager.GetString("ContentSelection_Exit"));
  35. ToolTipService.SetToolTip(SnapshotCopyBtn, LanguageHelper.CompressManager.GetString("Main_Copy"));
  36. ToolTipService.SetToolTip(SnapshotCorpBtn, LanguageHelper.CompressManager.GetString("Main_Crop"));
  37. ToolTipService.SetToolTip(SnapshotCorpBtn2, LanguageHelper.CompressManager.GetString("Main_Crop"));
  38. }
  39. private void Button_ReSnapshot(object sender, RoutedEventArgs e)
  40. {
  41. if(SnapToolArgs!=null)
  42. {
  43. SnapToolArgs.ReSnapshot();
  44. }
  45. }
  46. private void Button_SnapshotClose(object sender, RoutedEventArgs e)
  47. {
  48. if(SnapToolEvent!=null)
  49. {
  50. KeyValuePair<string, object> param = new KeyValuePair<string, object>("CloseSnap",null);
  51. SnapToolEvent.Invoke(this, param);
  52. }
  53. }
  54. private void Button_SnapshotCopy(object sender, RoutedEventArgs e)
  55. {
  56. if (SnapToolArgs != null)
  57. {
  58. SnapToolArgs.SaveSnapshotToClipboard();
  59. if (SnapToolEvent != null)
  60. {
  61. KeyValuePair<string, object> param = new KeyValuePair<string, object>("CloseSnap", null);
  62. SnapToolEvent.Invoke(this, param);
  63. }
  64. }
  65. }
  66. private int CropPageUI { get; set; }
  67. private void Button_SnapshotCorp(object sender, RoutedEventArgs e)
  68. {
  69. if (MessageBoxEx.Show(LanguageHelper.CompressManager.GetString("Corp_Customize_PRM") + " ", LanguageHelper.CompressManager.GetString("Corp_Customize"), System.Windows.Forms.MessageBoxButtons.OKCancel, System.Windows.Forms.MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.OK)
  70. {
  71. if (SnapToolArgs != null && PdfViewer != null)
  72. {
  73. List<int> cropPageList = new List<int>();
  74. Rect rect = SnapToolArgs.GetSnapshotPDFRect(out int pageIndexex);
  75. cropPageList.Add(pageIndexex);
  76. PdfViewer?.CropPage(CPDFDisplayBox.CropBox, rect, cropPageList);
  77. PdfViewer.FocusPDFViewTool.GetCPDFViewer().GoToPage(pageIndexex);
  78. KeyValuePair<string, object> param = new KeyValuePair<string, object>("Save", null);
  79. SnapToolEvent.Invoke(this, param);
  80. if (SnapToolEvent != null)
  81. {
  82. param = new KeyValuePair<string, object>("CloseSnap", null);
  83. SnapToolEvent.Invoke(this, param);
  84. }
  85. }
  86. }
  87. }
  88. private void Button_SnapshotSave(object sender, RoutedEventArgs e)
  89. {
  90. if (SnapToolArgs != null && PdfViewer != null && PdfViewer.PDFToolManager != null)
  91. {
  92. SaveFileDialog dlg = new SaveFileDialog();
  93. dlg.Filter = "jpg|*.jpg|png|*.png";
  94. dlg.FileName = PdfViewer.GetCPDFViewer().GetDocument().FileName;
  95. if(dlg.FileName==null ||dlg.FileName.Trim().Length==0)
  96. {
  97. dlg.FileName = "Blank" + DateTime.Now.ToString("yyyyMMddHHmmss");
  98. }
  99. if (dlg.ShowDialog() == true)
  100. {
  101. string fileName = dlg.FileName;
  102. WriteableBitmap saveBitmap= SnapToolArgs.GetSnapshotImage();
  103. if(saveBitmap!=null)
  104. {
  105. if (dlg.SafeFileName.ToLower().EndsWith(".jpg"))
  106. {
  107. Stream saveStream = dlg.OpenFile();
  108. JpegBitmapEncoder jpgEncoder = new JpegBitmapEncoder();
  109. BitmapFrame frame = BitmapFrame.Create(saveBitmap);
  110. jpgEncoder.Frames.Add(frame);
  111. jpgEncoder.Save(saveStream);
  112. saveStream.Dispose();
  113. }
  114. else if (dlg.SafeFileName.ToLower().EndsWith(".png"))
  115. {
  116. Stream saveStream = dlg.OpenFile();
  117. PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
  118. BitmapFrame frame = BitmapFrame.Create(saveBitmap);
  119. pngEncoder.Frames.Add(frame);
  120. pngEncoder.Save(saveStream);
  121. saveStream.Dispose();
  122. }
  123. CommonHelper.ExplorerFile(dlg.FileName);
  124. }
  125. if (SnapToolEvent != null)
  126. {
  127. KeyValuePair<string, object> param = new KeyValuePair<string, object>("CloseSnap", null);
  128. SnapToolEvent.Invoke(this, param);
  129. }
  130. }
  131. }
  132. }
  133. private void Button_PrintSnapshot(object sender, RoutedEventArgs e)
  134. {
  135. if (SnapToolArgs != null)
  136. {
  137. try
  138. {
  139. WriteableBitmap saveBitmap = SnapToolArgs.GetSnapshotImage();
  140. if (saveBitmap != null)
  141. {
  142. PrintDialog printDlg = new PrintDialog();
  143. if (printDlg.ShowDialog() == true)
  144. {
  145. DrawingVisual visualItem = new DrawingVisual();
  146. DrawingContext drawContext = visualItem.RenderOpen();
  147. drawContext.DrawImage(saveBitmap, new Rect(0, 0, saveBitmap.Width, saveBitmap.Height));
  148. drawContext.Close();
  149. printDlg.PrintVisual(visualItem, "Snapshot");
  150. }
  151. }
  152. if (SnapToolEvent != null)
  153. {
  154. KeyValuePair<string, object> param = new KeyValuePair<string, object>("CloseSnap", null);
  155. SnapToolEvent.Invoke(this, param);
  156. }
  157. }
  158. catch (Exception ex)
  159. {
  160. }
  161. }
  162. }
  163. }
  164. public class SnapshotToolArgs
  165. {
  166. public Color LineColor;
  167. public Color BgColor;
  168. public double LineWidth;
  169. public double Transparency;
  170. public DashStyle LineDash;
  171. public bool HasBgColor;
  172. public SnapshotToolArgs()
  173. {
  174. LineDash = new DashStyle();
  175. HasBgColor = true;
  176. LineColor = Color.FromRgb(49, 106, 197);
  177. BgColor = Color.FromRgb(153, 193, 218);
  178. LineWidth = 1;
  179. Transparency = 0.75;
  180. }
  181. }
  182. public class SnapshotEditToolArgs : SnapshotToolArgs
  183. {
  184. public Color HighLightColor;
  185. public Color ControlPointColor;
  186. public UserControl ToolPanel;
  187. public PDFViewControl Viewer { get; set; }
  188. public SnapshotEditToolArgs() : base()
  189. {
  190. HighLightColor = Colors.Transparent;
  191. ControlPointColor = Color.FromRgb(71, 126, 222);
  192. }
  193. public void ReSnapshot()
  194. {
  195. Viewer.FocusPDFViewTool.CleanPageSelectedRect();
  196. }
  197. public void SaveSnapshotToClipboard()
  198. {
  199. WriteableBitmap snapImage = GetSnapshotImage();
  200. if (snapImage != null)
  201. {
  202. try
  203. {
  204. Clipboard.SetImage(snapImage);
  205. }
  206. catch (Exception ex)
  207. {
  208. }
  209. }
  210. }
  211. public WriteableBitmap GetSnapshotImage()
  212. {
  213. if (Viewer != null && Viewer.SnapshotData != null)
  214. {
  215. CPDFDocument pdfDoc = Viewer.GetCPDFViewer().GetDocument();
  216. PageSelectedData snapData = Viewer.SnapshotData;
  217. CPDFViewer pdfViewer = Viewer.GetCPDFViewer();
  218. if (snapData.SelectRect.IsEmpty == false)
  219. {
  220. try
  221. {
  222. CPDFPage pdfPage = pdfDoc.PageAtIndex(snapData.PageIndex);
  223. double zoom = pdfViewer.GetZoom();
  224. Rect snapRect = snapData.SelectRect;
  225. DrawMode drawMode = pdfViewer.GetDrawModes();
  226. uint bgColor = 0xFFFFFFFF;
  227. int flag = 1;
  228. switch (drawMode)
  229. {
  230. case DrawMode.Soft:
  231. bgColor = 0xFFFFEFB2;
  232. break;
  233. case DrawMode.Green:
  234. bgColor = 0xFFCBE9CE;
  235. break;
  236. case DrawMode.Dark:
  237. bgColor = 0xFF000000;
  238. flag = flag | 0x08 | 0x10000;
  239. break;
  240. case DrawMode.Normal:
  241. bgColor = 0xFFFFFFFF;
  242. break;
  243. case DrawMode.Custom:
  244. bgColor = pdfViewer.GetPDFBackground();
  245. break;
  246. default:
  247. break;
  248. }
  249. byte[] imageData = new byte[(int)snapRect.Width * (int)snapRect.Height * 4];
  250. pdfPage.RenderPageBitmapWithMatrix((float)(zoom * 96D / 72D), new CRect(
  251. (float)snapRect.Left,
  252. (float)snapRect.Bottom,
  253. (float)snapRect.Right,
  254. (float)snapRect.Top),
  255. bgColor,
  256. imageData,
  257. flag,
  258. true);
  259. WriteableBitmap snapBitmap = new WriteableBitmap((int)snapRect.Width, (int)snapRect.Height, 96, 96, PixelFormats.Bgra32, null);
  260. snapBitmap.WritePixels(new Int32Rect(0, 0, (int)snapRect.Width, (int)snapRect.Height), imageData, snapBitmap.BackBufferStride, 0);
  261. return snapBitmap;
  262. }
  263. catch (Exception ex)
  264. {
  265. }
  266. }
  267. }
  268. return null;
  269. }
  270. public Rect GetSnapshotPDFRect(out int pageIndex)
  271. {
  272. pageIndex = -1;
  273. if (Viewer != null && Viewer.SnapshotData != null)
  274. {
  275. PageSelectedData snapData = Viewer.SnapshotData;
  276. pageIndex = snapData.PageIndex;
  277. return snapData.RawRect;
  278. }
  279. return Rect.Empty;
  280. }
  281. }
  282. }