PDFImageEditControl.xaml.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using ComPDFKitViewer.PdfViewer;
  2. using ComPDFKitViewer;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media.Imaging;
  8. using ComPDFKit.PDFPage;
  9. using System.Windows.Controls.Primitives;
  10. using Microsoft.Win32;
  11. using System.Diagnostics;
  12. using System.Drawing;
  13. using System.IO;
  14. namespace Compdfkit_Tools.Edit
  15. {
  16. public partial class PDFImageEditControl : UserControl
  17. {
  18. public CPDFViewer PDFView { get; private set; }
  19. public PDFEditEvent EditEvent { get; set; }
  20. public PDFImageEditControl()
  21. {
  22. InitializeComponent();
  23. Loaded += PDFImageEditControl_Loaded;
  24. Unloaded += PDFImageEditControl_Unloaded;
  25. }
  26. private void PDFImageEditControl_Unloaded(object sender, RoutedEventArgs e)
  27. {
  28. RotateUI.RotationChanged -= RotateUI_RotationChanged;
  29. FlipUI.FlipChanged -= FlipUI_FlipChanged;
  30. }
  31. public void InitWithPDFViewer(CPDFViewer newPDFView)
  32. {
  33. PDFView = newPDFView;
  34. }
  35. public void SetPDFImageEditData(PDFEditEvent newEvent)
  36. {
  37. EditEvent = null;
  38. if (newEvent != null && newEvent.EditType == CPDFEditType.EditImage)
  39. {
  40. SetImageTransparency(newEvent.Transparency);
  41. }
  42. EditEvent = newEvent;
  43. SetImageThumb();
  44. }
  45. private void PDFImageEditControl_Loaded(object sender, RoutedEventArgs e)
  46. {
  47. RotateUI.RotationChanged += RotateUI_RotationChanged;
  48. FlipUI.FlipChanged += FlipUI_FlipChanged;
  49. }
  50. private void FlipUI_FlipChanged(object sender, bool e)
  51. {
  52. if (EditEvent != null)
  53. {
  54. if(e)
  55. {
  56. EditEvent.VerticalMirror = true;
  57. }
  58. else
  59. {
  60. EditEvent.HorizontalMirror = true;
  61. }
  62. EditEvent.UpdatePDFEditByEventArgs();
  63. SetImageThumb();
  64. }
  65. }
  66. private void RotateUI_RotationChanged(object sender, double e)
  67. {
  68. if (EditEvent != null)
  69. {
  70. EditEvent.Rotate = (int)e;
  71. EditEvent.UpdatePDFEditByEventArgs();
  72. SetImageThumb();
  73. }
  74. }
  75. private void Slider_DragStarted(object sender, DragStartedEventArgs e)
  76. {
  77. Slider slider = sender as Slider;
  78. if (slider != null)
  79. {
  80. slider.Tag = "false";
  81. }
  82. }
  83. private void SliderOpacity_DragCompleted(object sender, DragCompletedEventArgs e)
  84. {
  85. Slider slider = sender as Slider;
  86. if (slider != null)
  87. {
  88. slider.Tag = "true";
  89. }
  90. if (EditEvent != null)
  91. {
  92. EditEvent.Transparency = (int)(ImasgeOpacitySlider.Value * 255);
  93. EditEvent.UpdatePDFEditByEventArgs();
  94. SetImageThumb();
  95. }
  96. }
  97. private void SliderOpacity_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  98. {
  99. Slider slider = sender as Slider;
  100. if(OpacityTextBox != null)
  101. {
  102. OpacityTextBox.Text = string.Format("{0}%", (int)(ImasgeOpacitySlider.Value * 100));
  103. }
  104. if (slider != null && slider.Tag != null && slider.Tag.ToString() == "false")
  105. {
  106. return;
  107. }
  108. if (EditEvent != null)
  109. {
  110. EditEvent.Transparency = (int)(ImasgeOpacitySlider.Value * 255);
  111. EditEvent.UpdatePDFEditByEventArgs();
  112. SetImageThumb();
  113. }
  114. }
  115. private void ImageReplaceBtn_Click(object sender, RoutedEventArgs e)
  116. {
  117. if (EditEvent != null)
  118. {
  119. OpenFileDialog openFileDialog = new OpenFileDialog();
  120. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  121. if (openFileDialog.ShowDialog() == true)
  122. {
  123. EditEvent.ReplaceImagePath = openFileDialog.FileName;
  124. EditEvent.UpdatePDFEditByEventArgs();
  125. EditEvent = null;
  126. }
  127. }
  128. }
  129. private void ImageExportBtn_Click(object sender, RoutedEventArgs e)
  130. {
  131. if (PDFView != null)
  132. {
  133. Dictionary<int, List<Bitmap>> imageDict = PDFView.GetSelectedImages();
  134. if (imageDict != null && imageDict.Count > 0)
  135. {
  136. System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
  137. if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  138. {
  139. string choosePath = folderBrowser.SelectedPath;
  140. string openPath = choosePath;
  141. try
  142. {
  143. foreach (int pageIndex in imageDict.Keys)
  144. {
  145. List<Bitmap> imageList = imageDict[pageIndex];
  146. foreach (Bitmap image in imageList)
  147. {
  148. string savePath = System.IO.Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  149. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  150. openPath = savePath;
  151. }
  152. }
  153. Process.Start("explorer", "/select,\"" + openPath + "\"");
  154. }
  155. catch (Exception ex)
  156. {
  157. }
  158. }
  159. }
  160. }
  161. }
  162. private void ImageClipBtn_Click(object sender, RoutedEventArgs e)
  163. {
  164. if (EditEvent != null)
  165. {
  166. EditEvent.ClipImage = true;
  167. EditEvent.UpdatePDFEditByEventArgs();
  168. }
  169. }
  170. private void OpacityComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  171. {
  172. ComboBoxItem selectItem = OpacityComboBox.SelectedItem as ComboBoxItem;
  173. if (selectItem != null && selectItem.Content != null)
  174. {
  175. if (double.TryParse(selectItem.Content.ToString().TrimEnd('%'), out double newOpacity))
  176. {
  177. OpacityTextBox.Text = selectItem.Content.ToString();
  178. ImasgeOpacitySlider.Value = newOpacity / 100.0;
  179. }
  180. }
  181. }
  182. public void SetImageThumb()
  183. {
  184. if (PDFView != null && EditEvent!=null)
  185. {
  186. try
  187. {
  188. Dictionary<int, List<Bitmap>> imageDict = PDFView.GetSelectedImages();
  189. foreach (int pageIndex in imageDict.Keys)
  190. {
  191. List<Bitmap> imageList = imageDict[pageIndex];
  192. if (imageList.Count > 0)
  193. {
  194. Bitmap bitmapImage = imageList[0];
  195. MemoryStream memoryStream = new MemoryStream();
  196. bitmapImage.Save(memoryStream, bitmapImage.RawFormat);
  197. BitmapImage imageShow = new BitmapImage();
  198. imageShow.BeginInit();
  199. imageShow.StreamSource = memoryStream;
  200. imageShow.EndInit();
  201. ImageThumbUI.Source = imageShow;
  202. break;
  203. }
  204. }
  205. }
  206. catch (Exception ex)
  207. {
  208. }
  209. }
  210. }
  211. public void SetImageTransparency(double transparency)
  212. {
  213. ImasgeOpacitySlider.Value = transparency / 255D;
  214. OpacityTextBox.Text = string.Format("{0}%", (int)(Math.Ceiling(ImasgeOpacitySlider.Value * 100)));
  215. }
  216. }
  217. }