2017-10-20 16 views
0

Je souhaite analyser une image à l'aide d'Interop.WIA dans une application WPF. J'ai téléchargé le code de classe ci-dessous mais constants a une erreur? J'ai ajouté Interop.WIA.dll.Comment numériser une image à l'aide d'Interop.WIA dans une application WPF

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); 
     } 
    } 
} 

Le texte d'erreur est The name 'Constants' does not exist in the current context -à-dire que je veux quand je clique sur le bouton de l'image est balayée par scanner.

Répondre

0

Eh bien, le message d'erreur est assez clair, n'est-ce pas? Il est dit Constants n'existe pas. Mais vous l'utilisez comme un paramètre à SaveImageToJpgFile. Donc, ajoutez le fichier manquant où ces constantes sont définies, ajoutez l'instruction missing using à l'espace de noms où la classe de constantes est définie ou supprimez le Constants stuff et codez en dur un chemin où vos images numérisées doivent être enregistrées.
comme ceci:

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

EDIT: Je pense que vous devez fournir un nom de fichier valide au lieu d'un nom de répertoire, de sorte que vous avez générer un nouveau nom pour chaque fichier si vous ne voulez pas écraser fichiers précédemment analysés.

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