VintaSoft Twain .NET SDK 15.1: Documentation for Web developer
In This Topic
    How to acquire the top part of page from TWAIN/SANE scanner?
    In This Topic
    If you want to set the scan area for TWAIN or SANE scanner, please use the WebTwainDeviceJS.setImageLayout function.

    Also TWAIN scanner allows to set the scan area using the WebTwainDeviceJS.setCapability function.


    Here is JavaScript code that demonstrates how to use WebDeviceJS.setImageLayout function for acquiring image area from TWAIN/SANE scanner:
    // acquire top part of page from TWAIN/SANE scanner
    __acquireTopPartOfPageFromTwainScanner();
    
    
    
    /**
     * Acquires top part of page from TWAIN/SANE scanner.
     */
    function __acquireTopPartOfPageFromTwainScanner() {
        // register the evaluation version of VintaSoft Web TWAIN service
        // please read how to get evaluation license in documentation: https://www.vintasoft.com/docs/vstwain-dotnet-web/Licensing-Twain_Web-Evaluation.html
        Vintasoft.Twain.WebTwainGlobalSettingsJS.register('REG_USER', 'REG_URL', 'REG_CODE', 'EXPIRATION_DATE');
    
        // URL to the VintaSoft Web TWAIN service
        var serviceUrl = 'https://localhost:25329/api/VintasoftTwainApi';
        // a Web API controller that allows to work with TWAIN and SANE devices
        var twainService = new Vintasoft.Shared.WebServiceControllerJS(serviceUrl);
    
        // TWAIN/SANE device manager
        var deviceManager = new Vintasoft.Twain.WebTwainDeviceManagerJS(twainService);
    
        // the default settings of device manager
        var deviceManagerInitSetting = new Vintasoft.Twain.WebTwainDeviceManagerInitSettingsJS();
    
        var device = null;
        try {
            // open device manager
            deviceManager.open(deviceManagerInitSetting);
    
            // get the default TWAIN/SANE device
            device = deviceManager.get_DefaultDevice();
    
            // open device without UI
            device.open(false);
    
            // if device is TWAIN device
            if (device.get_DeviceName().substring(0, 5) == "Twain") {
                // set the inches as unit of measure
                device.set_UnitOfMeasure(new Vintasoft.Twain.WebUnitOfMeasureEnumJS("Inches"));
            }
            // get current image layout
            let imageLayout: Vintasoft.Twain.WebTwainImageLayoutJS = device.getImageLayout();
            // set the image layout (get only the top half of the page)
            device.setImageLayout(0, 0, imageLayout.get_Width(), imageLayout.get_Height() / 2); 
    
            // a collection that stores images, which are acquired from TWAIN/SANE devices and stored in memory of VintaSoft Web TWAIN service
            var acquiredImages = new Vintasoft.Twain.WebAcquiredImageCollectionJS(deviceManager);
            var acquireModalState;
            do {
                // do one step of modal image acquisition process
                var acquireModalResult = device.acquireModalSync();
                // get state of image acquisition
                acquireModalState = acquireModalResult.get_AcquireModalState().valueOf();
    
                switch (acquireModalState) {
                    case 2:   // image is acquired
                        // get acquired image
                        var acquiredImage = acquireModalResult.get_AcquiredImage();
                        // add acquired image to the image collection
                        acquiredImages.add(acquiredImage);
    
                        // get image as Base64 string
                        var bitmapAsBase64String = acquiredImage.getAsBase64String();
                        // update image preview
                        var previewImageElement = document.getElementById('previewImage');
                        previewImageElement.src = bitmapAsBase64String;
    
                        // clear image collection (delete images from memory of VintaSoft Web TWAIN service) because image is not necessary anymore
                        acquiredImages.clear();
                        break;
    
                    case 4:   // image scan is failed
                        alert(acquireModalResult.get_ErrorMessage());
                        break;
    
                    case 9:   // image scan is finished
                        break;
                }
            }
            while (acquireModalState !== 0);
        }
        catch (ex) {
            alert(ex);
        }
        finally {
            if (device != null) {
                // close the device
                device.close();
            }
            // close the device manager
            deviceManager.close();
        }
    }
    
    


    Here is TypeScript code that demonstrates how to use WebDeviceJS.setImageLayout function for acquiring image area from TWAIN/SANE scanner:
    import { Component } from '@angular/core';
    
    @Component({
      selector: 'twain-scanning-demo',
      templateUrl: './twain-scanning-demo.component.html'
    })
    export class TwainScanningDemoComponent {
    
      ngOnInit() {
        // acquire top part of page from TWAIN/SANE scanner
        this.__acquireTopPartOfPageFromTwainScanner();
      }
    
    
      /**
       * Acquires top part of page from TWAIN/SANE scanner.
       */
      __acquireTopPartOfPageFromTwainScanner() {
        // register the evaluation version of VintaSoft Web TWAIN service
        // please read how to get evaluation license in documentation: https://www.vintasoft.com/docs/vstwain-dotnet-web/Licensing-Twain_Web-Evaluation.html
        Vintasoft.Twain.WebTwainGlobalSettingsJS.register('REG_USER', 'REG_URL', 'REG_CODE', 'EXPIRATION_DATE');
    
        // URL to the VintaSoft Web TWAIN service
        let serviceUrl: string = 'https://localhost:25329/api/VintasoftTwainApi';
        // a Web API controller that allows to work with TWAIN and SANE devices
        let twainService: Vintasoft.Shared.WebServiceControllerJS = new Vintasoft.Shared.WebServiceControllerJS(serviceUrl);
    
        // TWAIN/SANE device manager
        let deviceManager: Vintasoft.Twain.WebTwainDeviceManagerJS = new Vintasoft.Twain.WebTwainDeviceManagerJS(twainService);
    
        // the default settings of device manager
        let deviceManagerInitSetting: Vintasoft.Twain.WebTwainDeviceManagerInitSettingsJS = new Vintasoft.Twain.WebTwainDeviceManagerInitSettingsJS();
    
        try {
          // open device manager
          deviceManager.open(deviceManagerInitSetting);
        }
        catch (ex) {
          alert(ex);
          return;
        }
    
        let device: Vintasoft.Twain.WebTwainDeviceJS = null;
        try {
          // get the default TWAIN/SANE device
          device = deviceManager.get_DefaultDevice();
    
          // open device without UI
          device.open(false);
    
          // if device is TWAIN device
          if (device.get_DeviceName().substring(0, 5) == "Twain") {
              // set the inches as unit of measure
              device.set_UnitOfMeasure(new Vintasoft.Twain.WebUnitOfMeasureEnumJS("Inches"));
          }
          // get current image layout
          let imageLayout: Vintasoft.Twain.WebTwainImageLayoutJS = device.getImageLayout();
          // set the image layout (get only the top half of the page)
          device.setImageLayout(0, 0, imageLayout.get_Width(), imageLayout.get_Height() / 2); 
    
          // a collection that stores images, which are acquired from TWAIN/SANE devices and stored in memory of VintaSoft Web TWAIN service
          let acquiredImages: Vintasoft.Twain.WebAcquiredImageCollectionJS = new Vintasoft.Twain.WebAcquiredImageCollectionJS(deviceManager);
          let acquireModalState: number;
          do {
            // do one step of modal image acquisition process
            let acquireModalResult: Vintasoft.Twain.WebTwainDeviceAcquireModalResultJS = device.acquireModalSync();
            // get state of image acquisition
            acquireModalState = acquireModalResult.get_AcquireModalState().valueOf() as number;
    
            switch (acquireModalState) {
              case 2:   // image is acquired
                // get acquired image
                let acquiredImage: Vintasoft.Twain.WebAcquiredImageJS = acquireModalResult.get_AcquiredImage();
                // add acquired image to the image collection
                acquiredImages.add(acquiredImage);
    
                // get image as Base64 string
                let bitmapAsBase64String: string = acquiredImage.getAsBase64String();
                // update image preview
                let previewImageElement: HTMLImageElement = document.getElementById('previewImage') as HTMLImageElement;
                previewImageElement.src = bitmapAsBase64String;
    
                // clear image collection (delete images from memory of VintaSoft Web TWAIN service) because image is not necessary anymore
                acquiredImages.clear();
                break;
    
              case 4:   // image scan is failed
                alert(acquireModalResult.get_ErrorMessage());
                break;
    
              case 9:   // image scan is finished
                break;
            }
          }
          while (acquireModalState !== 0);
        }
        catch (ex) {
          alert(ex);
        }
        finally {
          if (device != null) {
            // close the device
            device.close();
          }
          // close the device manager
          deviceManager.close();
        }
      }
    
    }
    
    



    Here is TypeScript code that demonstrates how to use WebDeviceJS.setCapability function for acquiring image area from TWAIN/SANE scanner:
    // acquire top part of page from TWAIN scanner
    __acquireTopPartOfPageFromTwainScanner();
    
    
    
    /**
     * Acquires top part of page from TWAIN scanner.
     */
    function __acquireTopPartOfPageFromTwainScanner() {
        // register the evaluation version of VintaSoft Web TWAIN service
        // please read how to get evaluation license in documentation: https://www.vintasoft.com/docs/vstwain-dotnet-web/Licensing-Twain_Web-Evaluation.html
        Vintasoft.Twain.WebTwainGlobalSettingsJS.register('REG_USER', 'REG_URL', 'REG_CODE', 'EXPIRATION_DATE');
    
        // URL to the VintaSoft Web TWAIN service
        var serviceUrl = 'https://localhost:25329/api/VintasoftTwainApi';
        // a Web API controller that allows to work with TWAIN and SANE devices
        var twainService = new Vintasoft.Shared.WebServiceControllerJS(serviceUrl);
    
        // TWAIN device manager
        var deviceManager = new Vintasoft.Twain.WebTwainDeviceManagerJS(twainService);
    
        // the default settings of device manager
        var deviceManagerInitSetting = new Vintasoft.Twain.WebTwainDeviceManagerInitSettingsJS();
    
        var device = null;
        try {
            // open device manager
            deviceManager.open(deviceManagerInitSetting);
    
            // get the default TWAIN device
            device = deviceManager.get_DefaultDevice();
    
            // open device without UI
            device.open(false);
    
            // set the inches as unit of measure
            device.set_UnitOfMeasure(new Vintasoft.Twain.WebUnitOfMeasureEnumJS("Inches"));
            // set the image layout rectangle as value of IFrames capability
            device.setCapability(new Vintasoft.Twain.WebDeviceCapabilityIdEnumJS("IFrames"), new Vintasoft.Twain.WebTwainFrameJS(0, 0, 2, 2));
    
            // a collection that stores images, which are acquired from TWAIN/SANE devices and stored in memory of VintaSoft Web TWAIN service
            var acquiredImages = new Vintasoft.Twain.WebAcquiredImageCollectionJS(deviceManager);
            var acquireModalState;
            do {
                // do one step of modal image acquisition process
                var acquireModalResult = device.acquireModalSync();
                // get state of image acquisition
                acquireModalState = acquireModalResult.get_AcquireModalState().valueOf();
    
                switch (acquireModalState) {
                    case 2:   // image is acquired
                        // get acquired image
                        var acquiredImage = acquireModalResult.get_AcquiredImage();
                        // add acquired image to the image collection
                        acquiredImages.add(acquiredImage);
    
                        // get image as Base64 string
                        var bitmapAsBase64String = acquiredImage.getAsBase64String();
                        // update image preview
                        var previewImageElement = document.getElementById('previewImage');
                        previewImageElement.src = bitmapAsBase64String;
    
                        // clear image collection (delete images from memory of VintaSoft Web TWAIN service) because image is not necessary anymore
                        acquiredImages.clear();
                        break;
    
                    case 4:   // image scan is failed
                        alert(acquireModalResult.get_ErrorMessage());
                        break;
    
                    case 9:   // image scan is finished
                        break;
                }
            }
            while (acquireModalState !== 0);
        }
        catch (ex) {
            alert(ex);
        }
        finally {
            if (device != null) {
                // close the device
                device.close();
            }
            // close the device manager
            deviceManager.close();
        }
    }
    
    


    Here is TypeScript code that demonstrates how to use WebDeviceJS.setCapability function for acquiring image area from TWAIN/SANE scanner:
    import { Component } from '@angular/core';
    
    @Component({
      selector: 'twain-scanning-demo',
      templateUrl: './twain-scanning-demo.component.html'
    })
    export class TwainScanningDemoComponent {
    
      ngOnInit() {
        // acquire top part of page from TWAIN scanner
        this.__acquireTopPartOfPageFromTwainScanner();
      }
    
    
      /**
       * Acquires top part of page from TWAIN scanner.
       */
      __acquireTopPartOfPageFromTwainScanner() {
        // register the evaluation version of VintaSoft Web TWAIN service
        // please read how to get evaluation license in documentation: https://www.vintasoft.com/docs/vstwain-dotnet-web/Licensing-Twain_Web-Evaluation.html
        Vintasoft.Twain.WebTwainGlobalSettingsJS.register('REG_USER', 'REG_URL', 'REG_CODE', 'EXPIRATION_DATE');
    
        // URL to the VintaSoft Web TWAIN service
        let serviceUrl: string = 'https://localhost:25329/api/VintasoftTwainApi';
        // a Web API controller that allows to work with TWAIN and SANE devices
        let twainService: Vintasoft.Shared.WebServiceControllerJS = new Vintasoft.Shared.WebServiceControllerJS(serviceUrl);
    
        // TWAIN device manager
        let deviceManager: Vintasoft.Twain.WebTwainDeviceManagerJS = new Vintasoft.Twain.WebTwainDeviceManagerJS(twainService);
    
        // the default settings of device manager
        let deviceManagerInitSetting: Vintasoft.Twain.WebTwainDeviceManagerInitSettingsJS = new Vintasoft.Twain.WebTwainDeviceManagerInitSettingsJS();
    
        try {
          // open device manager
          deviceManager.open(deviceManagerInitSetting);
        }
        catch (ex) {
          alert(ex);
          return;
        }
    
        let device: Vintasoft.Twain.WebTwainDeviceJS = null;
        try {
          // get the default TWAIN device
          device = deviceManager.get_DefaultDevice();
    
          // open device without UI
          device.open(false);
    
          // set the inches as unit of measure
          device.set_UnitOfMeasure(new Vintasoft.Twain.WebUnitOfMeasureEnumJS("Inches"));
          // set the image layout rectangle as value of IFrames capability
          device.setCapability(new Vintasoft.Twain.WebDeviceCapabilityIdEnumJS("IFrames"), new Vintasoft.Twain.WebTwainFrameJS(0, 0, 2, 2));
    
          // a collection that stores images, which are acquired from TWAIN/SANE devices and stored in memory of VintaSoft Web TWAIN service
          let acquiredImages: Vintasoft.Twain.WebAcquiredImageCollectionJS = new Vintasoft.Twain.WebAcquiredImageCollectionJS(deviceManager);
          let acquireModalState: number;
          do {
            // do one step of modal image acquisition process
            let acquireModalResult: Vintasoft.Twain.WebTwainDeviceAcquireModalResultJS = device.acquireModalSync();
            // get state of image acquisition
            acquireModalState = acquireModalResult.get_AcquireModalState().valueOf() as number;
    
            switch (acquireModalState) {
              case 2:   // image is acquired
                // get acquired image
                let acquiredImage: Vintasoft.Twain.WebAcquiredImageJS = acquireModalResult.get_AcquiredImage();
                // add acquired image to the image collection
                acquiredImages.add(acquiredImage);
    
                // get image as Base64 string
                let bitmapAsBase64String: string = acquiredImage.getAsBase64String();
                // update image preview
                let previewImageElement: HTMLImageElement = document.getElementById('previewImage') as HTMLImageElement;
                previewImageElement.src = bitmapAsBase64String;
    
                // clear image collection (delete images from memory of VintaSoft Web TWAIN service) because image is not necessary anymore
                acquiredImages.clear();
                break;
    
              case 4:   // image scan is failed
                alert(acquireModalResult.get_ErrorMessage());
                break;
    
              case 9:   // image scan is finished
                break;
            }
          }
          while (acquireModalState !== 0);
        }
        catch (ex) {
          alert(ex);
        }
        finally {
          if (device != null) {
            // close the device
            device.close();
          }
          // close the device manager
          deviceManager.close();
        }
      }
    
    }