using ComPDFKit.PDFDocument; 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 { public Item DataSourceItem { get; set; } public string ScannerName { get; set; } public ScannerItem(Item dataSource) { DataSourceItem = dataSource; ScannerName = dataSource.Properties["Name"].ToString(); } } public class CreateFromScannerDialogsViewModel : BindableBase, IDialogAware { public static ImageFile imageFile; public string Title => ""; //图片路径 public string FilePath { get; private set; } public event Action RequestClose; public virtual void RaiseRequestClose(IDialogResult dialogResult) { RequestClose?.Invoke(dialogResult); } private List _scanners = new List(); public List Scanners { get { return _scanners; } set => SetProperty(ref _scanners, value); } public List _ScanItems =new List(); //图片路径 public List Scanitems { get { return _ScanItems; } set => SetProperty(ref _ScanItems, value); } private bool _IsEnable = true; public bool IsEnable { get { return _IsEnable; } set { SetProperty(ref _IsEnable, value); } } /// /// 没有扫描仪显示图 /// private Visibility _Gridvis=Visibility.Collapsed; public Visibility Gridvis { get { return _Gridvis; } set { SetProperty(ref _Gridvis, value); if (Gridvis == Visibility.Visible) { IsEnable = false; } else { IsEnable = true; } } } #region 框架 public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } public void OnDialogOpened(IDialogParameters parameters) { } #endregion #region 文案 private string _TextCreate; public string TextCreate { get { return _TextCreate; } set { SetProperty(ref _TextCreate, value); } } private string _BtnScan; public string BtnScan { get { return _BtnScan; } set { SetProperty(ref _BtnScan, value); } } private string _BtnCancle; public string BtnCancle { get { return _BtnCancle; } set { SetProperty(ref _BtnCancle, value); } } private string _TextScans; public string TextScans { get { return _TextScans; } set { SetProperty(ref _TextScans, value); } } private string _TextNoScan; public string TextNoScan { get { return _TextNoScan; } set { SetProperty(ref _TextNoScan, value); } } private void InitString() { TextCreate = App.HomePageLoader.GetString("TextCreate"); BtnScan = App.HomePageLoader.GetString("BtnScan"); BtnCancle = App.HomePageLoader.GetString("BtnCancle"); TextScans = App.HomePageLoader.GetString("TextScans"); TextNoScan = App.HomePageLoader.GetString("TextNoScan"); } #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); ScanAdd(); InitString(); } public void ScanAdd() { DeviceManager deviceManager; deviceManager = new DeviceManager(); for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++) { if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType) { continue; } Scanitems.Add(deviceManager.DeviceInfos[i].Properties["Name"].get_Value()); } if (Scanitems.Count == 0) { Gridvis = Visibility.Visible; } else { SeIndex = 0; } } 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(); //const string WIA_SCAN_COLOR_MODE = "{B9F49665-9A07-4B70-9AE7-B06FC1A4BE3D}"; //const string WIA_HORIZONTAL_RESOLUTION = "{6146}"; //const string WIA_VERTICAL_RESOLUTION = "{6147}"; //var scanner = deviceManager.DeviceInfos[i].Connect(); //var item = device.Items[1] as Item; //item.Properties["6146"].set_Value(300); //item.Properties[WIA_SCAN_COLOR_MODE].set_Value(2); //item.Properties[WIA_HORIZONTAL_RESOLUTION].set_Value(300); //item.Properties[WIA_VERTICAL_RESOLUTION].set_Value(300); device = deviceManager.DeviceInfos[i].Connect(); items = dialog.ShowSelectItems(device, SingleSelect: true); if (items != null) { //image = (ImageFile)device.Items[1]; image = dialog.ShowTransfer(device.Items[1]); ToImage(image); break; } } j++; } if (image != null) { } return image; } catch (COMException) { AlertsMessage alertsMessage = new AlertsMessage(); alertsMessage.ShowDialog(App.HomePageLoader.GetString("Scanner_Error"), App.HomePageLoader.GetString("Scanner_Errorbody"), App.HomePageLoader.GetString("Scanner_Errorok"), IconType.Tip); return null; } } public void ToImage(ImageFile img) { if (img != null) { string path = App.CachePath.ScanFilePath; string FileName = path+ "\\"+ Guid.NewGuid().ToString() + ".bmp"; 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") ); } } }