CreateFromScannerDialogsViewModel.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using ComPDFKit.PDFDocument;
  2. using NTwain;
  3. using NTwain.Data;
  4. using PDF_Master.CustomControl;
  5. using PDF_Master.Helper;
  6. using PDF_Master.Model;
  7. using Prism.Commands;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using Prism.Services.Dialogs;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Diagnostics;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Reflection;
  17. using System.Runtime.InteropServices;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using System.Windows;
  21. using System.Windows.Controls;
  22. using System.Windows.Forms;
  23. using System.Windows.Interop;
  24. using System.Windows.Media;
  25. using System.Windows.Media.Imaging;
  26. using WIA;
  27. namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs
  28. {
  29. //扫描仪设备
  30. public class ScannerItem : BindableBase
  31. {
  32. public DataSource DataSourceItem { get; set; }
  33. public ScannerItem(DataSource dataSource)
  34. {
  35. DataSourceItem = dataSource;
  36. ScannerName = dataSource.Name;
  37. }
  38. public string ScannerName { get; set; }
  39. }
  40. public class CreateFromScannerDialogsViewModel : BindableBase, IDialogAware
  41. {
  42. public static ImageFile imageFile;
  43. private List<ScannerItem> _scanners = new List<ScannerItem>();
  44. public List<ScannerItem> Scanners
  45. {
  46. get { return _scanners; }
  47. set => SetProperty(ref _scanners, value);
  48. }
  49. public string Title => "";
  50. //图片路径
  51. public string FilePath { get; private set; }
  52. public event Action<IDialogResult> RequestClose;
  53. public virtual void RaiseRequestClose(IDialogResult dialogResult)
  54. {
  55. RequestClose?.Invoke(dialogResult);
  56. }
  57. #region 框架
  58. public bool CanCloseDialog()
  59. {
  60. return true;
  61. }
  62. public void OnDialogClosed()
  63. {
  64. }
  65. public void OnDialogOpened(IDialogParameters parameters)
  66. {
  67. }
  68. #endregion
  69. //没有检测到扫描仪
  70. private int _SeIndex=0;
  71. public int SeIndex
  72. {
  73. get { return _SeIndex; }
  74. set => SetProperty(ref _SeIndex, value);
  75. }
  76. public MainContentViewModel mainContentViewModel;
  77. public DelegateCommand ToScanCommand { get; set; }
  78. public DelegateCommand CreateCommand { get; set; }
  79. public DelegateCommand<object> SelectedScannerCommand { get; set; }
  80. CreateFromScannerDialogsViewModel(IRegionManager regionManager, IDialogService dialogService)
  81. {
  82. ToScanCommand = new DelegateCommand(ToScan);
  83. }
  84. public void ToScan()
  85. {
  86. imageFile = Scan();
  87. }
  88. public ImageFile Scan()
  89. {
  90. ImageFile image = null;
  91. DeviceManager deviceManager;
  92. deviceManager = new DeviceManager();
  93. Device device;
  94. try
  95. {
  96. WIA.CommonDialog dialog = new WIA.CommonDialog();
  97. Items items;
  98. for (int i = 1, j = 0; i <= deviceManager.DeviceInfos.Count; i++)
  99. {
  100. if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
  101. {
  102. continue;
  103. }
  104. if (j == SeIndex)
  105. {
  106. device = deviceManager.DeviceInfos[i].Connect();
  107. items = dialog.ShowSelectItems(device, SingleSelect: true);
  108. if (items != null)
  109. {
  110. image = dialog.ShowTransfer(device.Items[1]);
  111. ToImage(image);
  112. }
  113. }
  114. j++;
  115. }
  116. if (image != null)
  117. {
  118. }
  119. return image;
  120. }
  121. catch (COMException ex)
  122. {
  123. if (ex.ErrorCode == -2145320939)
  124. {
  125. //throw new ScannerNotFoundException();
  126. }
  127. else
  128. {
  129. //throw new ScannerException("COM Exception", ex);
  130. }
  131. return null;
  132. }
  133. }
  134. public void ToImage(ImageFile img)
  135. {
  136. //System.Windows.Forms.SaveFileDialog mys = new System.Windows.Forms.SaveFileDialog();
  137. //mys.FileName = "MergeDocuments";
  138. //mys.Filter = "bmp文件|*.bmp";
  139. ////mys.ShowDialog();
  140. //string myFileN = mys.FileName.ToString();
  141. //myFileN = "C\\";//要保存的文件路径
  142. //string FileName = myFileN+ mys.FileName;
  143. //FileName = (FileName.Remove(0, FileName.Length - 4).Contains(".bmp")) ? FileName : FileName + ".bmp";
  144. //System.IO.Directory.CreateDirectory("C\\img.bmp");
  145. if (img != null)
  146. {
  147. //System.IO.Directory.CreateDirectory("C\\img.bmp");
  148. string path = App.CachePath.ScanFilePath;
  149. string FileName = path+ "\\"+ Guid.NewGuid().ToString() + ".bmp";
  150. //File.Create(FileName);
  151. img.SaveFile(FileName);
  152. string keyy = "Imagepath";
  153. DialogParameters keyValues = new DialogParameters();
  154. keyValues.Add(keyy, FileName);
  155. RaiseRequestClose(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK, keyValues));
  156. App.CachePath.AddToDeleteFiles(path);
  157. }
  158. }
  159. private void JpegInsertPage(ref CPDFDocument topdfdoc, string filename, FileInfo fileinfo, int width, int height, int index = 0)
  160. {
  161. string tempFileName = "";
  162. if (fileinfo.Extension == ".jpg" || fileinfo.Extension == ".jpeg")
  163. {
  164. topdfdoc.InsertPage(index, width, height, filename);
  165. }
  166. else
  167. {
  168. tempFileName = Path.GetTempPath() + "pngtemp.jpg";
  169. if (!PictureConverter.SaveJpeg(filename, tempFileName))
  170. {
  171. MessageBoxEx.Show("图片格式有问题");
  172. }
  173. topdfdoc.InsertPage(index, width, height, tempFileName);
  174. }
  175. }
  176. public string savefilename(string filename, string filepath)
  177. {
  178. FileInfo file = new FileInfo(filename);
  179. return CommonHelper.CreateFilePath(filepath + file.Name.Replace(file.Extension, ".pdf") );
  180. }
  181. }
  182. }