SignatureCreateDialogViewModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. using ComPDFKitViewer.AnnotEvent;
  2. using ComPDFKitViewer.PdfViewer;
  3. using Microsoft.Win32;
  4. using PDF_Office.Model;
  5. using Prism.Commands;
  6. using Prism.Mvvm;
  7. using Prism.Regions;
  8. using Prism.Services.Dialogs;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Collections.ObjectModel;
  12. using System.Drawing;
  13. using System.Drawing.Imaging;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Ink;
  20. using System.Windows.Media;
  21. using System.Windows.Media.Imaging;
  22. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  23. {
  24. class SignatureCreateDialogViewModel : BindableBase, IDialogAware
  25. {
  26. public string Title => "";
  27. private CPDFViewer PDFViewer;
  28. public event Action<IDialogResult> RequestClose;
  29. public DelegateCommand CancelCommand { get; set; }
  30. public DelegateCommand CreateCommnad { get; set; }
  31. public DelegateCommand<object> CheckedCommnad { get; set; }
  32. public DelegateCommand OpenImageCommnad { get; set; }
  33. public DelegateCommand ClearInkCanvasCommnad { get; set; }
  34. public ObservableCollection<string> FontNameList { get; set; }
  35. public ObservableCollection<string> ThicknessList { get; set; }
  36. private int fontNameIndex = 0;
  37. public int FontNameIndex
  38. {
  39. get { return fontNameIndex; }
  40. set
  41. {
  42. SetProperty(ref fontNameIndex, value);
  43. }
  44. }
  45. private int radioButtonIndex = 1;
  46. public int RadioButtonIndex
  47. {
  48. get { return radioButtonIndex; }
  49. set
  50. {
  51. SetProperty(ref radioButtonIndex, value);
  52. }
  53. }
  54. private int tabItemIndex;
  55. public int TabItemIndex
  56. {
  57. get { return tabItemIndex; }
  58. set
  59. {
  60. SetProperty(ref tabItemIndex, value);
  61. }
  62. }
  63. private string inputText = "";
  64. public string InputText
  65. {
  66. get { return inputText; }
  67. set
  68. {
  69. SetProperty(ref inputText, value);
  70. }
  71. }
  72. private Visibility showImageButton = Visibility.Visible;
  73. public Visibility ShowImageButton
  74. {
  75. get { return showImageButton; }
  76. set
  77. {
  78. SetProperty(ref showImageButton, value);
  79. }
  80. }
  81. private BitmapSource imagePreviewSource;
  82. public BitmapSource ImagePreviewSource
  83. {
  84. get { return imagePreviewSource; }
  85. set
  86. {
  87. SetProperty(ref imagePreviewSource, value);
  88. }
  89. }
  90. private bool issRemoveBackground;
  91. public bool IsRemoveBackground
  92. {
  93. get { return issRemoveBackground; }
  94. set
  95. {
  96. SetProperty(ref issRemoveBackground, value);
  97. if (issRemoveBackground)
  98. {
  99. if (IsNewFile)
  100. {
  101. removeBackgroundImagePreviewSource = ToBitmapImage(KnockOutGzf());
  102. }
  103. ImagePreviewSource = removeBackgroundImagePreviewSource;
  104. }
  105. else
  106. {
  107. ImagePreviewSource = originalimagePreviewSource;
  108. }
  109. }
  110. }
  111. private DrawingAttributes drawingAttributes;
  112. public DrawingAttributes DrawingAttributeObject
  113. {
  114. get { return drawingAttributes; }
  115. set
  116. {
  117. SetProperty(ref drawingAttributes, value);
  118. }
  119. }
  120. private StrokeCollection strokes;
  121. public StrokeCollection StrokesObject
  122. {
  123. get { return strokes; }
  124. set
  125. {
  126. SetProperty(ref strokes, value);
  127. }
  128. }
  129. private int imageRadioButtonIndex = 1;
  130. public int ImageRadioButtonIndex
  131. {
  132. get { return imageRadioButtonIndex; }
  133. set
  134. {
  135. SetProperty(ref imageRadioButtonIndex, value);
  136. switch (imageRadioButtonIndex)
  137. {
  138. case 1:
  139. DrawingAttributeObject.Color = System.Windows.Media.Color.FromArgb(0xFF, 0x25, 0x26, 0x29);
  140. break;
  141. case 2:
  142. DrawingAttributeObject.Color = System.Windows.Media.Color.FromArgb(0xFF, 0xF3, 0x46, 0x5B);
  143. break;
  144. case 3:
  145. DrawingAttributeObject.Color = System.Windows.Media.Color.FromArgb(0xFF, 0x27, 0x3C, 0x62);
  146. break;
  147. case 4:
  148. DrawingAttributeObject.Color = System.Windows.Media.Color.FromArgb(0xFF, 0x94, 0x98, 0x9C);
  149. break;
  150. default:
  151. break;
  152. }
  153. UpDataToStrokesObject();
  154. }
  155. }
  156. private int thicknessListIndex = 0;
  157. public int ThicknessListIndex
  158. {
  159. get { return thicknessListIndex; }
  160. set
  161. {
  162. SetProperty(ref thicknessListIndex, value);
  163. if (thicknessListIndex >= 0)
  164. {
  165. DrawingAttributeObject.Width = DrawingAttributeObject.Height = Convert.ToDouble(ThicknessList[thicknessListIndex].Substring(0, 3));
  166. UpDataToStrokesObject();
  167. }
  168. }
  169. }
  170. public string SaveToPath { get; private set; }
  171. public string RemoveBackgroundSaveToPath { get; private set; }
  172. public string DrawingSaveToPath { get; private set; }
  173. /// <summary>
  174. /// 用于判断是否更换了文件,更换了文件则需要重新计算去背功能
  175. /// </summary>
  176. private bool IsNewFile = false;
  177. //去背图片和原图的图片数据
  178. private BitmapSource originalimagePreviewSource = null;
  179. private BitmapSource removeBackgroundImagePreviewSource = null;
  180. public SignatureCreateDialogViewModel()
  181. {
  182. DrawingAttributeObject = new DrawingAttributes();
  183. StrokesObject = new StrokeCollection();
  184. FontNameList = new ObservableCollection<string>();
  185. ThicknessList = new ObservableCollection<string>() { "1.0pt", "2.0pt", "4.0pt", "6.0pt", "8.0pt" };//改了这里记得去改ThicknessListIndex的Set方法里面的取值
  186. CancelCommand = new DelegateCommand(Cancel);
  187. CreateCommnad = new DelegateCommand(Create);
  188. CheckedCommnad = new DelegateCommand<object>(Checked);
  189. OpenImageCommnad = new DelegateCommand(OpenImage);
  190. ClearInkCanvasCommnad = new DelegateCommand(ClearInkCanvas);
  191. DrawingAttributeObject.Color = Colors.Black;
  192. DrawingAttributeObject.Width = 1;
  193. DrawingAttributeObject.Height = 1;
  194. WeakEventManager<StrokeCollection, StrokeCollectionChangedEventArgs>.AddHandler(StrokesObject, "StrokesChanged", StrokesObject_StrokesChanged);
  195. InitFontNameList();
  196. }
  197. private void StrokesObject_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
  198. {
  199. int x1 = 1;
  200. }
  201. private void InitFontNameList()
  202. {
  203. System.Drawing.Text.InstalledFontCollection objFont = new System.Drawing.Text.InstalledFontCollection();
  204. foreach (var item in objFont.Families)
  205. {
  206. FontNameList.Add(item.Name);
  207. }
  208. }
  209. private void Checked(object index)
  210. {
  211. RadioButtonIndex = Convert.ToInt32(index);
  212. }
  213. public void UpDataImageRadioButtonIndex(int Index)
  214. {
  215. ImageRadioButtonIndex = Index;
  216. }
  217. public void UpDataRadioButtonIndex(int Index)
  218. {
  219. RadioButtonIndex = Index;
  220. }
  221. private void UpDataToStrokesObject()
  222. {
  223. foreach (var item in StrokesObject)
  224. {
  225. item.DrawingAttributes = DrawingAttributeObject;
  226. }
  227. }
  228. private void OpenImage()
  229. {
  230. OpenFileDialog openFile = new OpenFileDialog();
  231. openFile.Filter = "All Image Files(*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff|(*.bmp)|*.bmp|" +
  232. "(*.gif)|*.gif|" +
  233. "(*.jpeg)|*.jpeg|" +
  234. "(*.jpg)|*.jpg|" +
  235. "(*.png)|*.png|" +
  236. "(*.tiff)|*.tiff";
  237. if (openFile.ShowDialog() == false)
  238. {
  239. return;
  240. }
  241. string path = App.CachePath.CustomStampPath;
  242. string name = Guid.NewGuid().ToString();
  243. if (!string.IsNullOrEmpty(path))
  244. {
  245. BitmapImage image = new BitmapImage(new Uri(openFile.FileName));
  246. double scale = Math.Min((double)600 / image.PixelWidth, (double)600 / image.PixelHeight);
  247. scale = Math.Min(scale, 1);
  248. string ext = Path.GetExtension(openFile.FileName);
  249. if (ext.ToUpper() == ".PNG")
  250. {
  251. BitmapEncoder encoder = new PngBitmapEncoder();
  252. var targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
  253. encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
  254. path = System.IO.Path.Combine(path, name);
  255. using (FileStream stream = new FileStream(path, FileMode.Create))
  256. {
  257. encoder.Save(stream);
  258. }
  259. if (!string.IsNullOrEmpty(SaveToPath))
  260. {
  261. App.CachePath.AddToDeleteFiles(SaveToPath);
  262. }
  263. IsNewFile = true;
  264. SaveToPath = path;
  265. ImagePreviewSource = originalimagePreviewSource = targetBitmap;
  266. ShowImageButton = Visibility.Collapsed;
  267. }
  268. else
  269. {
  270. BitmapEncoder encoder = new JpegBitmapEncoder();
  271. TransformedBitmap targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
  272. encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
  273. path = System.IO.Path.Combine(path, name);
  274. using (FileStream stream = new FileStream(path, FileMode.Create))
  275. {
  276. encoder.Save(stream);
  277. }
  278. if (!string.IsNullOrEmpty(SaveToPath))
  279. {
  280. App.CachePath.AddToDeleteFiles(SaveToPath);
  281. }
  282. IsNewFile = true;
  283. SaveToPath = path;
  284. ImagePreviewSource = originalimagePreviewSource = targetBitmap;
  285. ShowImageButton = Visibility.Collapsed;
  286. }
  287. }
  288. else
  289. {
  290. SaveToPath = "";
  291. }
  292. }
  293. private void ClearInkCanvas()
  294. {
  295. StrokesObject.Clear();
  296. }
  297. /// <summary>
  298. /// 去除白底
  299. /// </summary>
  300. private Bitmap KnockOutGzf()
  301. {
  302. System.Drawing.Image image = System.Drawing.Image.FromFile(SaveToPath);
  303. Bitmap bitmapProxy = new Bitmap(image);
  304. image.Dispose();
  305. for (int i = 0; i < bitmapProxy.Width; i++)
  306. {
  307. for (int j = 0; j < bitmapProxy.Height; j++)
  308. {
  309. System.Drawing.Color c = bitmapProxy.GetPixel(i, j);
  310. if (!(c.R < 240 || c.G < 240 || c.B < 240))
  311. {
  312. bitmapProxy.SetPixel(i, j, System.Drawing.Color.Transparent);
  313. }
  314. }
  315. }
  316. return bitmapProxy;
  317. }
  318. /// <summary>
  319. /// 将 Bitmap 转化为 BitmapSource
  320. /// </summary>
  321. /// <param name="bmp"/>要转换的 Bitmap
  322. /// <returns>转换后的 BitmapImage</returns>
  323. private BitmapImage ToBitmapImage(System.Drawing.Bitmap bmp)
  324. {
  325. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  326. bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  327. BitmapImage image = new BitmapImage();
  328. image.BeginInit();
  329. image.StreamSource = ms;
  330. image.CacheOption = BitmapCacheOption.OnLoad;
  331. image.EndInit();
  332. //编码,缓存到本地
  333. string path = App.CachePath.CustomStampPath;
  334. string name = Guid.NewGuid().ToString();
  335. BitmapEncoder encoder = new PngBitmapEncoder();
  336. encoder.Frames.Add(BitmapFrame.Create(image));
  337. path = System.IO.Path.Combine(path, name);
  338. using (FileStream stream = new FileStream(path, FileMode.Create))
  339. {
  340. encoder.Save(stream);
  341. }
  342. if (!string.IsNullOrEmpty(RemoveBackgroundSaveToPath))
  343. {
  344. App.CachePath.AddToDeleteFiles(RemoveBackgroundSaveToPath);
  345. }
  346. RemoveBackgroundSaveToPath = path;
  347. IsNewFile = false;
  348. return image;
  349. }
  350. /// <summary>
  351. /// 把文字转换成Bitmap
  352. /// </summary>
  353. /// <param name="text"></param>
  354. /// <param name="font"></param>
  355. /// <param name="rect">用于输出的矩形,文字在这个矩形内显示,为空时自动计算</param>
  356. /// <param name="fontcolor">字体颜色</param>
  357. /// <param name="backColor">背景颜色</param>
  358. /// <returns></returns>
  359. private Bitmap TextToBitmap(string text, Font font, Rectangle rect, System.Drawing.Color fontcolor, System.Drawing.Color backColor)
  360. {
  361. Graphics g;
  362. Bitmap bmp;
  363. StringFormat format = new StringFormat(StringFormatFlags.NoClip);
  364. if (rect == Rectangle.Empty)
  365. {
  366. bmp = new Bitmap(1, 1);
  367. g = Graphics.FromImage(bmp);
  368. //计算绘制文字所需的区域大小(根据宽度计算长度),重新创建矩形区域绘图
  369. SizeF sizef = g.MeasureString(text, font, PointF.Empty, format);
  370. int width = (int)(sizef.Width + 1);
  371. int height = (int)(sizef.Height + 1);
  372. rect = new Rectangle(0, 0, width, height);
  373. bmp.Dispose();
  374. bmp = new Bitmap(width, height);
  375. }
  376. else
  377. {
  378. bmp = new Bitmap(rect.Width, rect.Height);
  379. }
  380. g = Graphics.FromImage(bmp);
  381. //使用ClearType字体功能
  382. g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
  383. g.FillRectangle(new SolidBrush(backColor), rect);
  384. g.DrawString(text, font, new SolidBrush(fontcolor), rect, format);
  385. return bmp;
  386. }
  387. private void Cancel()
  388. {
  389. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  390. }
  391. private void Create()
  392. {
  393. switch (TabItemIndex)
  394. {
  395. case 0:
  396. {
  397. if (string.IsNullOrEmpty(InputText))
  398. {
  399. Cancel();
  400. return;
  401. }
  402. System.Drawing.Color fontcolor = System.Drawing.Color.Black;
  403. switch (RadioButtonIndex)
  404. {
  405. case 1:
  406. fontcolor = System.Drawing.Color.Black;
  407. break;
  408. case 2:
  409. fontcolor = System.Drawing.Color.Red;
  410. break;
  411. case 3:
  412. fontcolor = System.Drawing.Color.Green;
  413. break;
  414. case 4:
  415. fontcolor = System.Drawing.Color.Blue;
  416. break;
  417. default:
  418. break;
  419. }
  420. Bitmap bmp = TextToBitmap(InputText, new Font(FontNameList[fontNameIndex], 48), Rectangle.Empty, fontcolor, System.Drawing.Color.Transparent);
  421. string guid = Guid.NewGuid().ToString();
  422. string path = System.IO.Path.Combine(App.CachePath.SignatureStampPath, guid);
  423. bmp.Save(path, ImageFormat.Png);
  424. SaveToPath = path;
  425. }
  426. break;
  427. case 1:
  428. {
  429. var FreeHandpath = App.CachePath.SignatureFreeHandPath;
  430. string name = Guid.NewGuid().ToString();
  431. FreeHandpath = System.IO.Path.Combine(FreeHandpath, name);
  432. using (System.IO.FileStream stream = new System.IO.FileStream(FreeHandpath, System.IO.FileMode.Create))
  433. {
  434. StrokesObject.Save(stream);
  435. }
  436. StampAnnotArgs stampArgs = new StampAnnotArgs();
  437. List<List<System.Windows.Point>> RawPointList = new List<List<System.Windows.Point>>();
  438. for (int kk = 0; kk < StrokesObject.Count; kk++)
  439. {
  440. List<System.Windows.Point> p = new List<System.Windows.Point>();
  441. RawPointList.Add(p);
  442. for (int gg = 0; gg < StrokesObject[kk].StylusPoints.Count; gg++)
  443. {
  444. var point = StrokesObject[kk].StylusPoints[gg].ToPoint();
  445. if (point.X >= 0 && point.Y >= 0)
  446. RawPointList[kk].Add(point);
  447. }
  448. }
  449. DrawingSaveToPath = FreeHandpath;
  450. stampArgs.SetInkData(RawPointList, drawingAttributes.Width, drawingAttributes.Color);
  451. var writeStamp = stampArgs.GetStampDrawing();
  452. FreeHandpath = App.CachePath.SignatureStampPath;
  453. string thumbnailName = Guid.NewGuid().ToString();
  454. FreeHandpath = System.IO.Path.Combine(FreeHandpath, thumbnailName);
  455. using (FileStream stream5 = new FileStream(FreeHandpath, FileMode.Create))
  456. {
  457. PngBitmapEncoder encoder5 = new PngBitmapEncoder();
  458. encoder5.Frames.Add(BitmapFrame.Create(writeStamp));
  459. encoder5.Save(stream5);
  460. }
  461. SaveToPath = FreeHandpath;
  462. }
  463. break;
  464. case 2:
  465. break;
  466. default:
  467. break;
  468. }
  469. DialogParameters valuePairs = new DialogParameters();
  470. valuePairs.Add(ParameterNames.DataModel, this);
  471. RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
  472. }
  473. public bool CanCloseDialog()
  474. {
  475. return true;
  476. }
  477. public void OnDialogClosed()
  478. {
  479. return;
  480. }
  481. public void OnDialogOpened(IDialogParameters parameters)
  482. {
  483. if (parameters != null)
  484. {
  485. PDFViewer = parameters.GetValue<CPDFViewer>(ParameterNames.PDFViewer);
  486. if (PDFViewer != null)
  487. {
  488. }
  489. }
  490. return;
  491. }
  492. }
  493. }