2017-10-20 164 views
0

我想在WPF应用程序中使用Interop.WIA扫描图像。我下载了下面的课程代码,但是constants有错误?我添加了Interop.WIA.dll。如何在WPF应用程序中使用Interop.WIA扫描图像

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using WIA; 

namespace WPF_Example.Services 
{ 
    public class ScannerService 
    { 
     public static void Scan() 
     { 
      try 
      { 
       CommonDialogClass commonDialogClass = new CommonDialogClass(); 
       Device scannerDevice = commonDialogClass.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, false, false); 
       if (scannerDevice != null) 
       { 
        Item scannnerItem = scannerDevice.Items[1]; 
        AdjustScannerSettings(scannnerItem, 600, 0, 0, 1010, 620, 0, 0); 
        object scanResult = commonDialogClass.ShowTransfer(scannnerItem, WIA.FormatID.wiaFormatPNG, false); 
        if (scanResult != null) 
        { 
         ImageFile image = (ImageFile)scanResult; 
         SaveImageToJpgFile(image, Constants.ScannedImageLocation); 
        } 
       } 
      } 
      catch (System.Runtime.InteropServices.COMException) 
      { 
       MessageBox.Show("Problem with scanning device. Please ensure that the scanner is properly connected and switched on", "Inweon Grain Management System"); 
      } 
     } 

     private static void AdjustScannerSettings(IItem scannnerItem, int scanResolutionDPI, int scanStartLeftPixel, int scanStartTopPixel, 
      int scanWidthPixels, int scanHeightPixels, int brightnessPercents, int contrastPercents) 
     { 
      const string WIA_HORIZONTAL_SCAN_RESOLUTION_DPI = "6147"; 
      const string WIA_VERTICAL_SCAN_RESOLUTION_DPI = "6148"; 
      const string WIA_HORIZONTAL_SCAN_START_PIXEL = "6149"; 
      const string WIA_VERTICAL_SCAN_START_PIXEL = "6150"; 
      const string WIA_HORIZONTAL_SCAN_SIZE_PIXELS = "6151"; 
      const string WIA_VERTICAL_SCAN_SIZE_PIXELS = "6152"; 
      const string WIA_SCAN_BRIGHTNESS_PERCENTS = "6154"; 
      const string WIA_SCAN_CONTRAST_PERCENTS = "6155"; 
      const string WIA_SCAN_BIT_DEPTH = "4104"; 
      SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_RESOLUTION_DPI, scanResolutionDPI); 
      SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_RESOLUTION_DPI, scanResolutionDPI); 
      SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_START_PIXEL, scanStartLeftPixel); 
      SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_START_PIXEL, scanStartTopPixel); 
      //SetWIAProperty(scannnerItem.Properties, WIA_SCAN_BIT_DEPTH, 48); 
      SetWIAProperty(scannnerItem.Properties, WIA_SCAN_BRIGHTNESS_PERCENTS, brightnessPercents); 
      SetWIAProperty(scannnerItem.Properties, WIA_SCAN_CONTRAST_PERCENTS, contrastPercents); 
     } 

     private static void SetWIAProperty(IProperties properties, object propName, object propValue) 
     { 
      Property prop = properties.get_Item(ref propName); 
      prop.set_Value(ref propValue); 
     } 

     private static void SaveImageToJpgFile(ImageFile image, string fileName) 
     { 
      ImageProcess imgProcess = new ImageProcess(); 
      object convertFilter = "Convert"; 
      string convertFilterID = imgProcess.FilterInfos.get_Item(ref convertFilter).FilterID; 
      imgProcess.Filters.Add(convertFilterID, 0); 
      SetWIAProperty(imgProcess.Filters[imgProcess.Filters.Count].Properties, "FormatID", WIA.FormatID.wiaFormatJPEG); 
      image = imgProcess.Apply(image); 
      image.SaveFile(fileName); 
     } 
    } 
} 

错误文本是The name 'Constants' does not exist in the current context 换句话说,我希望当我点击按钮的图像被扫描仪扫描。

回答

0

那么,错误信息很清楚,不是吗?

它说Constants不存在。但是,您将它用作SaveImageToJpgFile的参数。因此,无论是在定义这些常量的位置添加丢失的文件,还是将缺少的using语句添加到常量类定义的命名空间中,或者删除Constants东西,并硬编码扫描图像应保存的路径。
这样的:

SaveImageToJpgFile(image, @"C:\Users\[your-user-name]\Pictures");

编辑: 我认为你必须提供一个有效的文件名,而不是一个目录名,所以你必须为每一个文件一个新的名称,如果你不希望覆盖先前扫描的文件。

SaveImageToJpgFile(image, @"C:\Users\[your-user-name]\Pictures\img001.jpg");