CreateFromScannerDialogsViewModel.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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=-1;
  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. if (SeIndex>-1)
  87. {
  88. imageFile = Scan();
  89. }
  90. }
  91. public ImageFile Scan()
  92. {
  93. ImageFile image = null;
  94. DeviceManager deviceManager;
  95. deviceManager = new DeviceManager();
  96. Device device;
  97. try
  98. {
  99. WIA.CommonDialog dialog = new WIA.CommonDialog();
  100. Items items;
  101. for (int i = 1, j = 0; i <= deviceManager.DeviceInfos.Count; i++)
  102. {
  103. if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
  104. {
  105. continue;
  106. }
  107. if (j == SeIndex)
  108. {
  109. device = deviceManager.DeviceInfos[i].Connect();
  110. items = dialog.ShowSelectItems(device, SingleSelect: true);
  111. if (items != null)
  112. {
  113. image = dialog.ShowTransfer(device.Items[1]);
  114. ToImage(image);
  115. }
  116. }
  117. j++;
  118. }
  119. if (image != null)
  120. {
  121. }
  122. return image;
  123. }
  124. catch (COMException ex)
  125. {
  126. if (ex.ErrorCode == -2145320939)
  127. {
  128. //throw new ScannerNotFoundException();
  129. }
  130. else
  131. {
  132. //throw new ScannerException("COM Exception", ex);
  133. }
  134. return null;
  135. }
  136. }
  137. public void ToImage(ImageFile img)
  138. {
  139. //System.Windows.Forms.SaveFileDialog mys = new System.Windows.Forms.SaveFileDialog();
  140. //mys.FileName = "MergeDocuments";
  141. //mys.Filter = "bmp文件|*.bmp";
  142. ////mys.ShowDialog();
  143. //string myFileN = mys.FileName.ToString();
  144. //myFileN = "C\\";//要保存的文件路径
  145. //string FileName = myFileN+ mys.FileName;
  146. //FileName = (FileName.Remove(0, FileName.Length - 4).Contains(".bmp")) ? FileName : FileName + ".bmp";
  147. //System.IO.Directory.CreateDirectory("C\\img.bmp");
  148. if (img != null)
  149. {
  150. //System.IO.Directory.CreateDirectory("C\\img.bmp");
  151. string path = App.CachePath.ScanFilePath;
  152. string FileName = path+ "\\"+ Guid.NewGuid().ToString() + ".bmp";
  153. //File.Create(FileName);
  154. img.SaveFile(FileName);
  155. string keyy = "Imagepath";
  156. DialogParameters keyValues = new DialogParameters();
  157. keyValues.Add(keyy, FileName);
  158. RaiseRequestClose(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK, keyValues));
  159. App.CachePath.AddToDeleteFiles(path);
  160. }
  161. }
  162. private void JpegInsertPage(ref CPDFDocument topdfdoc, string filename, FileInfo fileinfo, int width, int height, int index = 0)
  163. {
  164. string tempFileName = "";
  165. if (fileinfo.Extension == ".jpg" || fileinfo.Extension == ".jpeg")
  166. {
  167. topdfdoc.InsertPage(index, width, height, filename);
  168. }
  169. else
  170. {
  171. tempFileName = Path.GetTempPath() + "pngtemp.jpg";
  172. if (!PictureConverter.SaveJpeg(filename, tempFileName))
  173. {
  174. MessageBoxEx.Show("图片格式有问题");
  175. }
  176. topdfdoc.InsertPage(index, width, height, tempFileName);
  177. }
  178. }
  179. public string savefilename(string filename, string filepath)
  180. {
  181. FileInfo file = new FileInfo(filename);
  182. return CommonHelper.CreateFilePath(filepath + file.Name.Replace(file.Extension, ".pdf") );
  183. }
  184. }
  185. }