ImageEditPropertyViewModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.PdfViewer;
  3. using Microsoft.Win32;
  4. using PDF_Master.Helper;
  5. using PDF_Master.Model;
  6. using Prism.Commands;
  7. using Prism.Mvvm;
  8. using Prism.Regions;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Input;
  17. namespace PDF_Master.ViewModels.PropertyPanel.PDFEdit
  18. {
  19. public class ImageEditPropertyViewModel : PDFEditVM, INavigationAware
  20. {
  21. #region 快捷键
  22. private void ShortCut_KeyDown(object sender, KeyEventArgs e)
  23. {
  24. if (e.Key == Key.Escape)
  25. {
  26. if (PDFViewer != null)
  27. {
  28. if (PDFViewer.ToolManager != null && IsCrop == true)
  29. {
  30. CancelCropImg();
  31. }
  32. }
  33. }
  34. if(e.Key==Key.Enter)
  35. {
  36. CropImg();
  37. }
  38. }
  39. #endregion
  40. #region 属性和变量
  41. //防止自动保存属性值
  42. private bool isCanSave = false;
  43. public event EventHandler ClearCheckedAglin;
  44. #region 是否为多选内容
  45. private bool _isMultiSelectImage = false;
  46. public bool IsMultiSelectImage { get { return _isMultiSelectImage; } set { SetProperty(ref _isMultiSelectImage, value); } }
  47. #endregion
  48. private string _opacity1= "Opacity";
  49. public string Opacity
  50. {
  51. get { return _opacity1; }
  52. set { SetProperty(ref _opacity1, value); }
  53. }
  54. //不透明度
  55. private double _opacity;
  56. public double OpacityUI
  57. {
  58. get { return _opacity; }
  59. set { SetProperty(ref _opacity, value); }
  60. }
  61. private double _transpent;
  62. public double Transpent
  63. {
  64. get { return _transpent; }
  65. set
  66. {
  67. SetProperty(ref _transpent, value);
  68. }
  69. }
  70. //是否为图片裁剪状态
  71. private bool _isCrop = false;
  72. public bool IsCrop { get { return _isCrop; } set { SetProperty(ref _isCrop, value); } }
  73. //选中的图像
  74. private System.Windows.Media.Imaging.BitmapSource _currentImg;
  75. public System.Windows.Media.Imaging.BitmapSource CurrentImg { get { return _currentImg; } set { SetProperty(ref _currentImg, value); } }
  76. #endregion
  77. #region Command
  78. //替换
  79. public DelegateCommand ReplaceImgCommand { get; set; }
  80. //导出
  81. public DelegateCommand ExportImgCommand { get; set; }
  82. //裁剪
  83. public DelegateCommand CropImgCommand { get; set; }
  84. //对齐
  85. public DelegateCommand<object> ImgAlignCheckedCommand { get; set; }
  86. //逆时针旋转
  87. public DelegateCommand AntiClockwiseCommand { get; set; }
  88. //顺时针旋转
  89. public DelegateCommand ClockwiseCommand { get; set; }
  90. //左右翻转
  91. public DelegateCommand FlipleftrightCommand { get; set; }
  92. //上下翻转
  93. public DelegateCommand UpsidedownCommand { get; set; }
  94. //裁剪状态
  95. public DelegateCommand CropModeCommand { get; set; }
  96. //取消裁剪状态
  97. public DelegateCommand CancelCropCommand { get; set; }
  98. //还原裁剪状态
  99. public DelegateCommand RestoreCropCommand { get; set; }
  100. //添加文本
  101. public DelegateCommand AddTextCommand { get; set; }
  102. //添加图片
  103. public DelegateCommand AddImgCommand { get; set; }
  104. //透明度条
  105. public DelegateCommand TranspentslidCommand { get; set; }
  106. public DelegateCommand EditImgModeCommand { get; set; }
  107. #endregion
  108. public ImageEditPropertyViewModel()
  109. {
  110. InitCommand();
  111. }
  112. private void InitCommand()
  113. {
  114. AddTextCommand = new DelegateCommand(AddText);
  115. AddImgCommand = new DelegateCommand(AddImg);
  116. ReplaceImgCommand = new DelegateCommand(ReplaceImg);
  117. ExportImgCommand = new DelegateCommand(ExportImg);
  118. CropImgCommand = new DelegateCommand(CropImg);
  119. ImgAlignCheckedCommand = new DelegateCommand<object>(ImgAlignChecked);
  120. TranspentslidCommand = new DelegateCommand(Transpentslid);
  121. AntiClockwiseCommand = new DelegateCommand(AntiClockwise);
  122. ClockwiseCommand = new DelegateCommand(Clockwise);
  123. FlipleftrightCommand = new DelegateCommand(Flipleftright);
  124. UpsidedownCommand = new DelegateCommand(Upsidedown);
  125. CropModeCommand = new DelegateCommand(CropMode);
  126. CancelCropCommand = new DelegateCommand(CancelCropImg);
  127. RestoreCropCommand = new DelegateCommand(RestoreCropImg);
  128. EditImgModeCommand = new DelegateCommand(EditImgMode);
  129. }
  130. #region Command实现
  131. //不透明度滑动左键松开
  132. private void Transpentslid()
  133. {
  134. if (TextEditEvent != null && isCanSave)
  135. {
  136. if (IsMultiSelectImage)
  137. {
  138. foreach (var item in TextEditEventList)
  139. {
  140. item.Transparency = (int)(_transpent * 2.55);
  141. item.UpdatePDFEditByEventArgs();
  142. }
  143. }
  144. else
  145. {
  146. TextEditEvent.Transparency = (int)(_transpent * 2.55);
  147. TextEditEvent.UpdatePDFEditByEventArgs();
  148. OpacityUI = _transpent / 100.0;
  149. }
  150. }
  151. }
  152. //点击编辑按钮,暂时保留
  153. private void EditImgMode()
  154. {
  155. }
  156. //添加文本模式
  157. private void AddText()
  158. {
  159. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  160. }
  161. //添加图片
  162. private void AddImg()
  163. {
  164. OpenFileDialog openFileDialog = new OpenFileDialog();
  165. openFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;";
  166. openFileDialog.Multiselect = true;
  167. if ((bool)openFileDialog.ShowDialog())
  168. {
  169. if (string.IsNullOrEmpty(openFileDialog.FileName) == false)
  170. {
  171. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  172. PDFViewer.AddPDFEditImage(openFileDialog.FileName, 500, 500);
  173. }
  174. }
  175. }
  176. //进入裁剪模式
  177. private void CropMode()
  178. {
  179. IsCrop = true;
  180. if (TextEditEvent != null)
  181. {
  182. TextEditEvent.ClipImage = true;
  183. TextEditEvent.UpdatePDFEditByEventArgs();
  184. }
  185. }
  186. //取消裁剪
  187. private void CancelCropImg()
  188. {
  189. if (TextEditEvent != null)
  190. {
  191. TextEditEvent.ClipImage = false;
  192. TextEditEvent.CancelClip();
  193. TextEditEvent.UpdatePDFEditByEventArgs();
  194. IsCrop = false;
  195. }
  196. }
  197. //还原裁剪
  198. private void RestoreCropImg()
  199. {
  200. if (TextEditEvent != null)
  201. {
  202. TextEditEvent.RestoreClip();
  203. }
  204. }
  205. //完成裁剪
  206. private void CropImg()
  207. {
  208. if (TextEditEvent != null)
  209. {
  210. TextEditEvent.SaveClip();
  211. TextEditEvent.ClipImage = false;
  212. TextEditEvent.UpdatePDFEditByEventArgs();
  213. IsCrop = false;
  214. GetImagePreView();
  215. }
  216. }
  217. //导出图片
  218. private void ExportImg()
  219. {
  220. if (PDFViewer == null || TextEditEvent == null || TextEditEvent.EditType != ComPDFKit.PDFPage.CPDFEditType.EditImage) return;
  221. System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog();
  222. folder.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
  223. if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  224. {
  225. if (string.IsNullOrEmpty(folder.SelectedPath))
  226. return;
  227. var keyValueList = PDFViewer.GetSelectedImages();
  228. int i = 0;
  229. foreach (var bitmap in keyValueList)
  230. {
  231. foreach (var bitmapItem in bitmap.Value)
  232. {
  233. bitmapItem.Save(folder.SelectedPath + "\\" + i + ".png", System.Drawing.Imaging.ImageFormat.Png);
  234. i++;
  235. }
  236. }
  237. var strFilePath = folder.SelectedPath + "\\0.png";
  238. CommonHelper.ShowFileBrowser(strFilePath);
  239. }
  240. }
  241. //替换裁剪
  242. private void ReplaceImg()
  243. {
  244. OpenFileDialog openFileDialog = new OpenFileDialog();
  245. openFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;";
  246. openFileDialog.Multiselect = true;
  247. if ((bool)openFileDialog.ShowDialog())
  248. {
  249. if (string.IsNullOrEmpty(openFileDialog.FileName) == false)
  250. {
  251. TextEditEvent.ReplaceImagePath = openFileDialog.FileName;
  252. TextEditEvent.UpdatePDFEditByEventArgs();
  253. ReplaceimgTask();
  254. }
  255. }
  256. }
  257. //此处只有异步 ReplaceimgTask()才有作用
  258. async Task ReplaceimgTask()
  259. {
  260. await Task.Delay(100);
  261. GetImagePreView();
  262. }
  263. //顺时针旋转
  264. private void Clockwise()
  265. {
  266. ImgRoateAngle(90);
  267. }
  268. //逆时针旋转
  269. private void AntiClockwise()
  270. {
  271. ImgRoateAngle(-90);
  272. }
  273. //左右翻转
  274. private void Flipleftright()
  275. {
  276. TextEditEvent.HorizontalMirror = true;
  277. TextEditEvent.UpdatePDFEditByEventArgs();
  278. }
  279. //上下翻转
  280. private void Upsidedown()
  281. {
  282. TextEditEvent.VerticalMirror = true;
  283. TextEditEvent.UpdatePDFEditByEventArgs();
  284. }
  285. //旋转逻辑
  286. private void ImgRoateAngle(int angle)
  287. {
  288. if (IsMultiSelectImage)
  289. {
  290. foreach (var item in TextEditEventList)
  291. {
  292. item.Rotate = item.Rotate + angle;
  293. item.UpdatePDFEditByEventArgs();
  294. }
  295. }
  296. else
  297. {
  298. TextEditEvent.Rotate = TextEditEvent.Rotate + angle;
  299. TextEditEvent.UpdatePDFEditByEventArgs();
  300. GetImagePreView();
  301. }
  302. }
  303. #endregion
  304. #region 布局处理
  305. private void ImgAlignChecked(object obj)
  306. {
  307. if (obj != null)
  308. {
  309. switch ((string)obj)
  310. {
  311. case "AlignLeft":
  312. PDFViewer.SetPDFEditAligment(AlignModes.AlignLeft);
  313. break;
  314. case "AlignHorizonCenter":
  315. PDFViewer.SetPDFEditAligment(AlignModes.AlignHorizonCenter);
  316. break;
  317. case "AlignRight":
  318. PDFViewer.SetPDFEditAligment(AlignModes.AlignRight);
  319. break;
  320. case "DistributeHorizontal":
  321. PDFViewer.SetPDFEditAligment(AlignModes.DistributeHorizontal);
  322. break;
  323. case "AlignTop":
  324. PDFViewer.SetPDFEditAligment(AlignModes.AlignTop);
  325. break;
  326. case "AlignVerticalCenter":
  327. PDFViewer.SetPDFEditAligment(AlignModes.AlignVerticalCenter);
  328. break;
  329. case "AlignBottom":
  330. PDFViewer.SetPDFEditAligment(AlignModes.AlignBottom);
  331. break;
  332. case "DistributeVertical":
  333. PDFViewer.SetPDFEditAligment(AlignModes.DistributeVertical);
  334. break;
  335. }
  336. }
  337. }
  338. #endregion
  339. protected List<PDFEditEvent> TextEditEventList;
  340. public void OnNavigatedTo(NavigationContext navigationContext)
  341. {
  342. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  343. navigationContext.Parameters.TryGetValue<List<PDFEditEvent>>(ParameterNames.AnnotEvent, out TextEditEventList);
  344. if (PDFViewer != null)
  345. {
  346. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  347. PDFViewer.PDFEditActiveHandler += PDFViewer_PDFEditActiveHandler;
  348. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  349. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  350. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  351. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  352. LoadedPDFEdit();
  353. isCanSave = true;
  354. }
  355. }
  356. //获取图片参数
  357. private void LoadedPDFEdit()
  358. {
  359. if (TextEditEventList != null && TextEditEventList.Count > 0)
  360. {
  361. TextEditEvent = TextEditEventList[0];
  362. if (TextEditEventList.Count > 1)
  363. {
  364. IsMultiSelectImage = true;
  365. }
  366. else
  367. {
  368. GetImagePreView();
  369. }
  370. if (TextEditEventList.Count == 2)
  371. {
  372. IsLayoutAlign = true;
  373. IsLayoutAvgAlign = false;
  374. }
  375. else if (TextEditEventList.Count > 2)
  376. {
  377. IsLayoutAlign = true;
  378. IsLayoutAvgAlign = true;
  379. }
  380. else
  381. {
  382. IsLayoutAlign = false;
  383. IsLayoutAvgAlign = false;
  384. }
  385. var tranUI = (TextEditEvent.Transparency / (255.0 * 255.0)) * 100;
  386. var temp = Math.Round((double)tranUI, 0);
  387. Transpent = temp;
  388. OpacityUI = temp / 100.0;
  389. }
  390. }
  391. #region 右键菜单
  392. //点击空白处时
  393. private ContextMenu EmptyStateMenu(object sender)
  394. {
  395. var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu;
  396. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  397. //粘贴
  398. customMenu.SetMenuBinding(0, ApplicationCommands.Paste);
  399. //添加文本
  400. customMenu.SetMenuBinding(1, AddTextCommand);
  401. //添加图像
  402. customMenu.SetMenuBinding(2, AddImgCommand);
  403. return popMenu;
  404. }
  405. //选中图像时
  406. private ContextMenu SelectImgPDFEdit(object sender)
  407. {
  408. var popMenu = App.Current.FindResource("SelectImgMenu") as ContextMenu;
  409. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  410. //复制
  411. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  412. //剪切
  413. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  414. //粘贴
  415. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  416. //删除
  417. customMenu.SetMenuBinding(3, ApplicationCommands.Delete);
  418. //裁剪
  419. customMenu.SetMenuBinding(4, CropModeCommand);
  420. //替换
  421. customMenu.SetMenuBinding(5, ReplaceImgCommand);
  422. //导出
  423. customMenu.SetMenuBinding(6, ExportImgCommand);
  424. return popMenu;
  425. }
  426. //选中裁剪图像时
  427. private ContextMenu CropImgPDFEdit(object sender)
  428. {
  429. var popMenu = App.Current.FindResource("CropImgMenu") as ContextMenu;
  430. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  431. //确认裁剪
  432. customMenu.SetMenuBinding(0, CropImgCommand);
  433. //取消裁剪
  434. customMenu.SetMenuBinding(1, CancelCropCommand);
  435. //还原裁剪
  436. customMenu.SetMenuBinding(2, RestoreCropCommand);
  437. return popMenu;
  438. }
  439. //多选图片右键
  440. private ContextMenu SelectMoreImage(object sender)
  441. {
  442. var popMenu = App.Current.FindResource("SelectMoreImageMenu") as ContextMenu;
  443. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  444. //复制
  445. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  446. //剪切
  447. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  448. //粘贴
  449. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  450. //删除
  451. customMenu.SetMenuBinding(3, ApplicationCommands.Delete);
  452. //导出
  453. customMenu.SetMenuBinding(4, ExportImgCommand);
  454. return popMenu;
  455. }
  456. #endregion
  457. //左键点击逻辑
  458. private void PDFViewer_PDFEditActiveHandler(object sender, List<PDFEditEvent> e)
  459. {
  460. //退出编辑模式
  461. IsCrop = false;
  462. }
  463. //右键逻辑
  464. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  465. {
  466. if (e == null)
  467. return;
  468. switch (e.CommandType)
  469. {
  470. case CommandType.Context:
  471. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  472. {
  473. if (IsCrop == true)
  474. {
  475. e.PopupMenu =CropImgPDFEdit(sender);
  476. }
  477. else
  478. {
  479. e.PopupMenu = SelectImgPDFEdit(sender);
  480. }
  481. }
  482. else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)
  483. {
  484. e.PopupMenu = EmptyStateMenu(sender);
  485. }
  486. break;
  487. default:
  488. e.DoCommand();
  489. break;
  490. }
  491. if (e.PopupMenu != null)
  492. {
  493. e.Handle = true;
  494. }
  495. }
  496. //属性面板图像更新
  497. private void GetImagePreView()
  498. {
  499. try
  500. {
  501. var list = PDFViewer.GetSelectedImages();
  502. if (list != null && list.Count > 0)
  503. {
  504. System.Drawing.Bitmap bitmap = null;
  505. foreach (var item in list)
  506. {
  507. if (item.Value.Count > 0)
  508. {
  509. bitmap = item.Value[0];
  510. break;
  511. }
  512. }
  513. if (bitmap != null)
  514. {
  515. IntPtr ip = bitmap.GetHbitmap();
  516. System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,
  517. System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
  518. CurrentImg = bitmapSource;
  519. }
  520. }
  521. }
  522. catch
  523. {
  524. }
  525. }
  526. public bool IsNavigationTarget(NavigationContext navigationContext) { return true; }
  527. public void OnNavigatedFrom(NavigationContext navigationContext)
  528. {
  529. isCanSave = false;
  530. IsMultiSelectImage = false;
  531. TextEditEvent = null;
  532. ClearCheckedAglin?.Invoke(null, null);
  533. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  534. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  535. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  536. }
  537. }
  538. }