2017-10-20 17 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");

EDIT::このような
は私があなたの代わりにディレクトリ名の有効なファイル名を提供するために持っていると思うので、あなたが上書きしたくない場合は、各ファイルの新しい名前を生成しています以前にスキャンされたファイル

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

関連する問題