public DataSource(TwainDevice twain, TwIdentity identity)
 {
   fTwain = twain;
   fIdent = identity;
   fState = StateType.Closed;
   fSettings = null;
   fCapabilities = null;
 }
 private void Raise_OnNewPictureData(Image image, DataSourceSettings theSettings)
 {
   if(OnNewPictureData != null)
   {
     NewPictureEventArgs args = new NewPictureEventArgs();
     args.TheImage = image;
     args.TheSettings = theSettings;
     OnNewPictureData(this, args);
   }
 }
      public bool Acquire(DataSourceSettings settings)
      {
        bool success = false;

        if(this.Open())
        {
          fSettings = settings;

          if(fSettings != null)
          {
            // Setup Pixel Type
            TwCapPixelType pixelType;

            switch(fSettings.ColorMode)
            {
              case ColorModeEnum.BW: pixelType = TwCapPixelType.BW; break;
              case ColorModeEnum.RGB: pixelType = TwCapPixelType.RGB; break;
              case ColorModeEnum.Gray: pixelType = TwCapPixelType.Gray; break;
              default: pixelType = TwCapPixelType.BW; break;
            }

            fCapabilityPixelType.CurrentValue = pixelType;

#if USE_PAGE_TYPE
            // Page Type Setting is not used anymore; Image Layout is defined instead
            TwCapPageType pageType;

            switch(fSettings.PageType)
            {
              case PageTypeEnum.Letter: pageType = TwCapPageType.UsLetter; break;
              case PageTypeEnum.Legal: pageType = TwCapPageType.UsLegal; break;
              default: pageType = TwCapPageType.UsLetter; break;
            }

            fCapabilityPageSize.CurrentValue = pageType;
#else
            // Setup Scan Area
            TwImageLayout ilayout = new TwImageLayout();

            if(fTwain.GetImageLayout(fIdent, ilayout))
            {
              BoundsInches scanArea = settings.ScanArea;

              ilayout.Frame.Left = (float)scanArea.X;
              ilayout.Frame.Top = (float)scanArea.Y;
              ilayout.Frame.Right = (float)(scanArea.X + scanArea.Width);
              ilayout.Frame.Bottom = (float)(scanArea.Y + scanArea.Height);
              fTwain.SetImageLayout(fIdent, ilayout);
            }
#endif

            // Setup Resolution
            float resolution = (float)fSettings.Resolution;

            fCapabilityResolutionX.CurrentValue = resolution;
            fCapabilityResolutionY.CurrentValue = resolution;

            // Enable/Disable Document Feeder
            if(fSettings.EnableFeeder)
            {
              fCapabilityFeederEnabled.CurrentValue = fCapabilityFeederLoaded.CurrentValue;
            }
            else
            {
              fCapabilityFeederEnabled.CurrentValue = false;
            }

            // Analog adjustments
            fCapabilityThreshold.ScaledValue = settings.Threshold;
            fCapabilityBrightness.ScaledValue = settings.Brightness;
            fCapabilityContrast.ScaledValue = settings.Contrast;
          }

          Application.AddMessageFilter(this);
          fState = StateType.Active; // This makes IsActive = true

          if(fTwain.StartDataSession(fIdent, settings.ShowSettingsUI, settings.ShowTransferUI))
          {
            success = true;
          }
          else
          {
            Stop(); // TODO: Is this OK?
          }
        }

        return success;
      }
      public bool Acquire(DataSourceSettings settings)
      {
        bool result = false;

        if(this.Open())
        {
          int colorMode;

          switch(settings.ColorMode)
          {
            case ColorModeEnum.RGB: colorMode = (int)WiaPropertyCurrentIntent.WIA_INTENT_IMAGE_TYPE_COLOR; break;
            case ColorModeEnum.Gray: colorMode = (int)WiaPropertyCurrentIntent.WIA_INTENT_IMAGE_TYPE_GRAYSCALE; break;
            default: colorMode = (int)WiaPropertyCurrentIntent.WIA_INTENT_IMAGE_TYPE_TEXT; break;
          }

          int resolution = settings.Resolution;

          int brightness = (int)((settings.Brightness - 0.5) * 2000);

          int contrast = (int)((settings.Contrast - 0.5) * 2000);

          AdjustScannerSettings(resolution, settings.ScanArea, brightness, contrast, colorMode);

          bool morePages = true;

          while(morePages)
          {
            Image newImage = Scan(settings.ShowTransferUI);

            if(newImage != null)
            {
              Raise_OnNewPictureData(newImage, settings);
            }

            morePages = HasMorePages();
          }

          this.Close();
          Raise_OnScanningComplete();

          result = true;
        }

        return result;
      }