SignatureCreateDialogViewModel.cs 21 KB

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