SignatureCreateDialogViewModel.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. //4.5注释 解决切换颜色后 线条粗细变回3的问题
  189. //UpDataToStrokesObject();
  190. }
  191. }
  192. private int thicknessListIndex = 1;
  193. public int ThicknessListIndex
  194. {
  195. get { return thicknessListIndex; }
  196. set
  197. {
  198. SetProperty(ref thicknessListIndex, value);
  199. if (thicknessListIndex >= 0)
  200. {
  201. DrawingAttributeObject.Width = DrawingAttributeObject.Height = Convert.ToDouble(ThicknessList[thicknessListIndex].Substring(0, 3));
  202. UpDataToStrokesObject();
  203. }
  204. }
  205. }
  206. public string SaveToPath { get; private set; }
  207. public string RemoveBackgroundSaveToPath { get; private set; }
  208. public string DrawingSaveToPath { get; private set; }
  209. /// <summary>
  210. /// 用于判断是否更换了文件,更换了文件则需要重新计算去背功能
  211. /// </summary>
  212. private bool IsNewFile = false;
  213. //去背图片和原图的图片数据
  214. private BitmapSource originalimagePreviewSource = null;
  215. private BitmapSource removeBackgroundImagePreviewSource = null;
  216. public SignatureCreateDialogViewModel()
  217. {
  218. DrawingAttributeObject = new DrawingAttributes();
  219. StrokesObject = new StrokeCollection();
  220. FontNameList = new ObservableCollection<string>();
  221. ThicknessList = new ObservableCollection<string>() { "1.0pt", "2.0pt", "4.0pt", "6.0pt", "8.0pt" };//改了这里记得去改ThicknessListIndex的Set方法里面的取值
  222. CancelCommand = new DelegateCommand(Cancel);
  223. CreateCommnad = new DelegateCommand(Create);
  224. UpdataDrawingStrokesCommnad = new DelegateCommand(UpdataDrawingStrokes);
  225. CheckedCommnad = new DelegateCommand<object>(Checked);
  226. OpenImageCommnad = new DelegateCommand(OpenImage);
  227. ClearTextCommnad = new DelegateCommand(ClearText);
  228. ClearInkCanvasCommnad = new DelegateCommand(ClearInkCanvas);
  229. DrawingAttributeObject.Color = Colors.Black;
  230. DrawingAttributeObject.Width = StrokeWidth;
  231. DrawingAttributeObject.Height = StrokeHigh;
  232. WeakEventManager<StrokeCollection, StrokeCollectionChangedEventArgs>.AddHandler(StrokesObject, "StrokesChanged", StrokesObject_StrokesChanged);
  233. InitFontNameList();
  234. }
  235. private void StrokesObject_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
  236. {
  237. DrawingAttributeObject.Width = StrokeWidth;
  238. DrawingAttributeObject.Height = StrokeHigh;
  239. }
  240. private void InitFontNameList()
  241. {
  242. System.Drawing.Text.InstalledFontCollection objFont = new System.Drawing.Text.InstalledFontCollection();
  243. foreach (var item in objFont.Families)
  244. {
  245. FontNameList.Add(item.Name);
  246. }
  247. }
  248. private void Checked(object index)
  249. {
  250. RadioButtonIndex = Convert.ToInt32(index);
  251. }
  252. public void UpDataImageRadioButtonIndex(int Index)
  253. {
  254. ImageRadioButtonIndex = Index;
  255. }
  256. public void UpDataRadioButtonIndex(int Index)
  257. {
  258. RadioButtonIndex = Index;
  259. }
  260. private void UpDataToStrokesObject()
  261. {
  262. foreach (var item in StrokesObject)
  263. {
  264. item.DrawingAttributes = DrawingAttributeObject.Clone();
  265. }
  266. DrawingAttributeObject.Width = DrawingAttributeObject.Height = 3;
  267. }
  268. private void ClearText()
  269. {
  270. InputText = "";
  271. }
  272. private void OpenImage()
  273. {
  274. OpenFileDialog openFile = new OpenFileDialog();
  275. openFile.Filter = "All Image Files(*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff|(*.bmp)|*.bmp|" +
  276. "(*.gif)|*.gif|" +
  277. "(*.jpeg)|*.jpeg|" +
  278. "(*.jpg)|*.jpg|" +
  279. "(*.png)|*.png|" +
  280. "(*.tiff)|*.tiff";
  281. if (openFile.ShowDialog() == false)
  282. {
  283. return;
  284. }
  285. string path = App.CachePath.CustomStampPath;
  286. string name = Guid.NewGuid().ToString();
  287. if (!string.IsNullOrEmpty(path))
  288. {
  289. BitmapImage image = new BitmapImage(new Uri(openFile.FileName));
  290. double scale = Math.Min((double)600 / image.PixelWidth, (double)600 / image.PixelHeight);
  291. scale = Math.Min(scale, 1);
  292. BitmapEncoder encoder = new PngBitmapEncoder();
  293. var targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
  294. encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
  295. path = System.IO.Path.Combine(path, name);
  296. using (FileStream stream = new FileStream(path, FileMode.Create))
  297. {
  298. encoder.Save(stream);
  299. }
  300. if (!string.IsNullOrEmpty(SaveToPath))
  301. {
  302. App.CachePath.AddToDeleteFiles(SaveToPath);
  303. }
  304. IsNewFile = true;
  305. SaveToPath = path;
  306. ImagePreviewSource = originalimagePreviewSource = targetBitmap;
  307. ShowImageButton = Visibility.Collapsed;
  308. if (IsRemoveBackground)
  309. {
  310. ImagePreviewSource = removeBackgroundImagePreviewSource = ToBitmapImage(KnockOutGzf());
  311. }
  312. }
  313. else
  314. {
  315. SaveToPath = "";
  316. }
  317. }
  318. private void ClearInkCanvas()
  319. {
  320. StrokesObject.Clear();
  321. }
  322. /// <summary>
  323. /// 去除白底
  324. /// </summary>
  325. private Bitmap KnockOutGzf()
  326. {
  327. System.Drawing.Image image = System.Drawing.Image.FromFile(SaveToPath);
  328. Bitmap bitmapProxy = new Bitmap(image);
  329. image.Dispose();
  330. for (int i = 0; i < bitmapProxy.Width; i++)
  331. {
  332. for (int j = 0; j < bitmapProxy.Height; j++)
  333. {
  334. System.Drawing.Color c = bitmapProxy.GetPixel(i, j);
  335. if (!(c.R < 240 || c.G < 240 || c.B < 240))
  336. {
  337. bitmapProxy.SetPixel(i, j, System.Drawing.Color.Transparent);
  338. }
  339. }
  340. }
  341. return bitmapProxy;
  342. }
  343. /// <summary>
  344. /// 将 Bitmap 转化为 BitmapSource
  345. /// </summary>
  346. /// <param name="bmp"/>要转换的 Bitmap
  347. /// <returns>转换后的 BitmapImage</returns>
  348. private BitmapImage ToBitmapImage(System.Drawing.Bitmap bmp)
  349. {
  350. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  351. bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  352. BitmapImage image = new BitmapImage();
  353. image.BeginInit();
  354. image.StreamSource = ms;
  355. image.CacheOption = BitmapCacheOption.OnLoad;
  356. image.EndInit();
  357. //编码,缓存到本地
  358. string path = App.CachePath.CustomStampPath;
  359. string name = Guid.NewGuid().ToString();
  360. BitmapEncoder encoder = new PngBitmapEncoder();
  361. encoder.Frames.Add(BitmapFrame.Create(image));
  362. path = System.IO.Path.Combine(path, name);
  363. using (FileStream stream = new FileStream(path, FileMode.Create))
  364. {
  365. encoder.Save(stream);
  366. }
  367. if (!string.IsNullOrEmpty(RemoveBackgroundSaveToPath))
  368. {
  369. App.CachePath.AddToDeleteFiles(RemoveBackgroundSaveToPath);
  370. }
  371. RemoveBackgroundSaveToPath = path;
  372. IsNewFile = false;
  373. return image;
  374. }
  375. /// <summary>
  376. /// 把文字转换成Bitmap
  377. /// </summary>
  378. /// <param name="text"></param>
  379. /// <param name="font"></param>
  380. /// <param name="rect">用于输出的矩形,文字在这个矩形内显示,为空时自动计算</param>
  381. /// <param name="fontcolor">字体颜色</param>
  382. /// <param name="backColor">背景颜色</param>
  383. /// <returns></returns>
  384. private Bitmap TextToBitmap(string text, string FontFamily, double size, Rectangle rect, System.Windows.Media.Brush fontcolor, System.Drawing.Color backColor)
  385. {
  386. FormattedText formatText = new FormattedText(text,
  387. CultureInfo.CurrentCulture,
  388. FlowDirection.LeftToRight,
  389. new Typeface(new System.Windows.Media.FontFamily(FontFamily), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
  390. size,
  391. fontcolor, Helper.DpiHelpers.Dpi / 96F);
  392. DrawingVisual drawingVisual = new DrawingVisual();
  393. DrawingContext dc = drawingVisual.RenderOpen();
  394. dc.DrawText(formatText, new System.Windows.Point(2, 10));
  395. dc.Close();
  396. Rect x = drawingVisual.ContentBounds;
  397. Rect DrawRect = new Rect(0, 0, x.Width + 2, x.Height + 10);
  398. RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((int)DrawRect.Width + 2, (int)DrawRect.Height + 10, 96F, 96F, PixelFormats.Pbgra32);
  399. renderTargetBitmap.Render(drawingVisual);
  400. MemoryStream stream = new MemoryStream();
  401. BitmapEncoder encoder = new PngBitmapEncoder();
  402. encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
  403. encoder.Save(stream);
  404. Bitmap bitmap = new Bitmap(stream);
  405. return bitmap;
  406. }
  407. private void Cancel()
  408. {
  409. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  410. }
  411. private void Create()
  412. {
  413. switch (TabItemIndex)
  414. {
  415. case 0:
  416. {
  417. if (string.IsNullOrEmpty(InputText))
  418. {
  419. Cancel();
  420. return;
  421. }
  422. System.Windows.Media.Brush fontcolor = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#252629"));
  423. switch (RadioButtonIndex)
  424. {
  425. case 1:
  426. fontcolor = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#252629"));
  427. break;
  428. case 2:
  429. fontcolor = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#F3465B"));
  430. break;
  431. case 3:
  432. fontcolor = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#273C62"));
  433. break;
  434. case 4:
  435. fontcolor = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#94989C"));
  436. break;
  437. default:
  438. break;
  439. }
  440. Bitmap bmp = TextToBitmap(InputText, FontNameList[fontNameIndex], 20, Rectangle.Empty, fontcolor, System.Drawing.Color.Transparent);
  441. string guid = Guid.NewGuid().ToString();
  442. string path = System.IO.Path.Combine(App.CachePath.SignatureStampPath, guid);
  443. bmp.Save(path, ImageFormat.Png);
  444. SaveToPath = path;
  445. }
  446. break;
  447. case 1:
  448. {
  449. var FreeHandpath = App.CachePath.SignatureFreeHandPath;
  450. string name = Guid.NewGuid().ToString();
  451. FreeHandpath = System.IO.Path.Combine(FreeHandpath, name);
  452. using (System.IO.FileStream stream = new System.IO.FileStream(FreeHandpath, System.IO.FileMode.Create))
  453. {
  454. StrokesObject.Save(stream);
  455. }
  456. StampAnnotArgs stampArgs = new StampAnnotArgs();
  457. List<List<System.Windows.Point>> RawPointList = new List<List<System.Windows.Point>>();
  458. for (int kk = 0; kk < StrokesObject.Count; kk++)
  459. {
  460. List<System.Windows.Point> p = new List<System.Windows.Point>();
  461. RawPointList.Add(p);
  462. for (int gg = 0; gg < StrokesObject[kk].StylusPoints.Count; gg++)
  463. {
  464. var point = StrokesObject[kk].StylusPoints[gg].ToPoint();
  465. if (point.X >= 0 && point.Y >= 0)
  466. RawPointList[kk].Add(point);
  467. }
  468. }
  469. DrawingSaveToPath = FreeHandpath;
  470. stampArgs.SetInkData(RawPointList, drawingAttributes.Width, drawingAttributes.Color);
  471. var writeStamp = stampArgs.GetStampDrawing();
  472. FreeHandpath = App.CachePath.SignatureStampPath;
  473. string thumbnailName = Guid.NewGuid().ToString();
  474. FreeHandpath = System.IO.Path.Combine(FreeHandpath, thumbnailName);
  475. using (FileStream stream5 = new FileStream(FreeHandpath, FileMode.Create))
  476. {
  477. PngBitmapEncoder encoder5 = new PngBitmapEncoder();
  478. encoder5.Frames.Add(BitmapFrame.Create(writeStamp));
  479. encoder5.Save(stream5);
  480. }
  481. SaveToPath = FreeHandpath;
  482. }
  483. break;
  484. case 2:
  485. break;
  486. default:
  487. break;
  488. }
  489. DialogParameters valuePairs = new DialogParameters();
  490. valuePairs.Add(ParameterNames.DataModel, this);
  491. RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
  492. }
  493. /// <summary>
  494. /// 绘制时使用的画笔
  495. /// </summary>
  496. private void UpdataDrawingStrokes()
  497. {
  498. DrawingAttributeObject.Width = DrawingAttributeObject.Height = Convert.ToDouble(ThicknessList[thicknessListIndex].Substring(0, 3));
  499. }
  500. public bool CanCloseDialog()
  501. {
  502. return true;
  503. }
  504. public void OnDialogClosed()
  505. {
  506. return;
  507. }
  508. public void OnDialogOpened(IDialogParameters parameters)
  509. {
  510. if (parameters != null)
  511. {
  512. PDFViewer = parameters.GetValue<CPDFViewer>(ParameterNames.PDFViewer);
  513. if (PDFViewer != null)
  514. {
  515. }
  516. }
  517. return;
  518. }
  519. }
  520. }