Home : TWAIN Desktop Scanning : Scan Images with DotTwain Acquire Method
Q10196 - HOWTO: Scan Images with DotTwain Acquire Method

Batch Scanning consists of two parts: The call to aquire and the ImageAcquired event which is called when each page is scanned. The following example illustrates how this can be done:

Class Fields:

// this goes in your class definition as a field for your applciation
private Acquisition _acquisition;
private Device _device;

Initialization:

//This code would happen in your main constructor etc..

this._acquisition = new Acquisition();
if (this._acquisition.Devices.Default != null)
{
    this._device = this._acquisition.Devices.Default;
}
this._acquisition.ImageAcquired += new ImageAcquiredEventHandler(_acquisition_ImageAcquired);
this._acquisition.AcquireCanceled += new EventHandler(_acquisition_AcquireCanceled);
this._acquisition.AcquireFinished += new EventHandler(_acquisition_AcquireFinished);
this._acquisition.AsynchronousException += new AsynchronousExceptionEventHandler(_acquisition_AsynchronousException);

Please see the Acquisition Demo in our Legacy Demos KB article for a more complete implementation

Aquire:
if (this._device == null)
{
    this._device = this._acquisition.Devices.Default;
}
this._device.Acquire();

Event handlers:
private void _acquisition_ImageAcquired(object sender, Atalasoft.Twain.AcquireEventArgs e)
{
    // Perform for each scanned page
    if (e.Image != null)
    {
        AtalaImage currentImg = AtalaImage.FromBitmap(e.Image);
        // do whatever with currentImg
        currentImg.Dispose();
    }
}

private void _acquisition_AsynchronousException(object sender, AsynchronousExceptionEventArgs e)
{
    // Try to gracefully close the device so we don't end up "keeping our thumb on the scanner"
    if (this._device.State == TwainState.SourceOpen)
    {
        this._device.Close();
    }
    // in this simple example we're just blabbing the message..
    // production code would likely want to provide user friendly message and probably log the error
    // message and StackTrace for use in troubleshooting
    MessageBox.Show("ERROR: " + e.Exception.Message);
}

private void _acquisition_AcquireFinished(object sender, EventArgs e)
{
    // there is no processing needed on finished but if you did need to take some action
    // after a batch acquire finished, this is the place to do it
    if (this._device.State == TwainState.SourceOpen)
    {
        this._device.Close();
    }
}

private void _acquisition_AcquireCanceled(object sender, EventArgs e)
{
    // Again, we are just making sure we gracefully close the device so as not to cause
    // errors where device is blocked because it's been left hanging
    if (this._device.State == TwainState.SourceOpen)
    {
        this._device.Close();
    }
}

 

 

Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.

Powered By InstantKB.NET v1.3
Copyright © 2002, 2019. InstantASP Ltd. All Rights Reserved