SignatureCreateDialogViewModel.cs 21 KB

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