SignatureCreateDialogViewModel.cs 21 KB

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