123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- 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<ScannerItem> _scanners = new List<ScannerItem>();
- public List<ScannerItem> Scanners
- {
- get { return _scanners; }
- set => SetProperty(ref _scanners, value);
- }
- public string Title => "";
- //图片路径
- public string FilePath { get; private set; }
- public event Action<IDialogResult> 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<object> 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") );
- }
- }
- }
|