using ComPDFKit.PDFDocument; using NTwain; using NTwain.Data; using PDF_Master.CustomControl; using PDF_Master.Helper; using PDF_Master.Model; using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Forms; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; using WIA; namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs { //扫描仪设备 public class ScannerItem : BindableBase { public DataSource DataSourceItem { get; set; } public ScannerItem(DataSource dataSource) { DataSourceItem = dataSource; ScannerName = dataSource.Name; } public string ScannerName { get; set; } } public class CreateFromScannerDialogsViewModel : BindableBase, IDialogAware { public static ImageFile imageFile; private List _scanners = new List(); public List Scanners { get { return _scanners; } set => SetProperty(ref _scanners, value); } public string Title => ""; //图片路径 public string FilePath { get; private set; } public event Action RequestClose; public virtual void RaiseRequestClose(IDialogResult dialogResult) { RequestClose?.Invoke(dialogResult); } #region 框架 public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } public void OnDialogOpened(IDialogParameters parameters) { } #endregion //没有检测到扫描仪 private int _SeIndex=-1; public int SeIndex { get { return _SeIndex; } set => SetProperty(ref _SeIndex, value); } public MainContentViewModel mainContentViewModel; public DelegateCommand ToScanCommand { get; set; } public DelegateCommand CreateCommand { get; set; } public DelegateCommand SelectedScannerCommand { get; set; } CreateFromScannerDialogsViewModel(IRegionManager regionManager, IDialogService dialogService) { ToScanCommand = new DelegateCommand(ToScan); } public void ToScan() { if (SeIndex>-1) { imageFile = Scan(); } } public ImageFile Scan() { ImageFile image = null; DeviceManager deviceManager; deviceManager = new DeviceManager(); Device device; try { WIA.CommonDialog dialog = new WIA.CommonDialog(); Items items; for (int i = 1, j = 0; i <= deviceManager.DeviceInfos.Count; i++) { if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType) { continue; } if (j == SeIndex) { device = deviceManager.DeviceInfos[i].Connect(); items = dialog.ShowSelectItems(device, SingleSelect: true); if (items != null) { image = dialog.ShowTransfer(device.Items[1]); ToImage(image); break; } } j++; } if (image != null) { } return image; } catch (COMException ex) { if (ex.ErrorCode == -2145320939) { //throw new ScannerNotFoundException(); } else { //throw new ScannerException("COM Exception", ex); } return null; } } public void ToImage(ImageFile img) { //System.Windows.Forms.SaveFileDialog mys = new System.Windows.Forms.SaveFileDialog(); //mys.FileName = "MergeDocuments"; //mys.Filter = "bmp文件|*.bmp"; ////mys.ShowDialog(); //string myFileN = mys.FileName.ToString(); //myFileN = "C\\";//要保存的文件路径 //string FileName = myFileN+ mys.FileName; //FileName = (FileName.Remove(0, FileName.Length - 4).Contains(".bmp")) ? FileName : FileName + ".bmp"; //System.IO.Directory.CreateDirectory("C\\img.bmp"); if (img != null) { //System.IO.Directory.CreateDirectory("C\\img.bmp"); string path = App.CachePath.ScanFilePath; string FileName = path+ "\\"+ Guid.NewGuid().ToString() + ".bmp"; //File.Create(FileName); img.SaveFile(FileName); string keyy = "Imagepath"; DialogParameters keyValues = new DialogParameters(); keyValues.Add(keyy, FileName); RaiseRequestClose(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK, keyValues)); App.CachePath.AddToDeleteFiles(path); } } private void JpegInsertPage(ref CPDFDocument topdfdoc, string filename, FileInfo fileinfo, int width, int height, int index = 0) { string tempFileName = ""; if (fileinfo.Extension == ".jpg" || fileinfo.Extension == ".jpeg") { topdfdoc.InsertPage(index, width, height, filename); } else { tempFileName = Path.GetTempPath() + "pngtemp.jpg"; if (!PictureConverter.SaveJpeg(filename, tempFileName)) { MessageBoxEx.Show("图片格式有问题"); } topdfdoc.InsertPage(index, width, height, tempFileName); } } public string savefilename(string filename, string filepath) { FileInfo file = new FileInfo(filename); return CommonHelper.CreateFilePath(filepath + file.Name.Replace(file.Extension, ".pdf") ); } } }