How to process TWAIN acquired image?
In This Topic
Here is an example that demonstrates how to acquire images from TWAIN device, process images and save only not blank images to a multipage TIFF file:
/// <summary>
/// Acquires images from TWAIN device, processes images and saves only not blank images to a multipage TIFF file.
/// </summary>
public static void AcquireImagesFromTwainDeviceAndProcessImages()
{
// create the device manager
using (Vintasoft.Twain.DeviceManager deviceManager = new Vintasoft.Twain.DeviceManager())
{
// open the device manager
deviceManager.Open();
// select the device in the default device selectio ndialog
deviceManager.ShowDefaultDeviceSelectionDialog();
// get reference to the selected device
Vintasoft.Twain.Device device = deviceManager.DefaultDevice;
// specify that device UI must not be used
device.ShowUI = false;
// specify that device must be closed after scan
device.DisableAfterAcquire = true;
string tiffFilename = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "multipage.tif");
// acquire images from device
Vintasoft.Twain.AcquireModalState acquireModalState = Vintasoft.Twain.AcquireModalState.None;
Vintasoft.Twain.AcquiredImage acquiredImage;
do
{
acquireModalState = device.AcquireModal();
switch (acquireModalState)
{
case Vintasoft.Twain.AcquireModalState.ImageAcquired:
// get reference to the image acquired from device
acquiredImage = device.AcquiredImage;
// despeckle/deskew/detect border
ProcessAcquiredImage(acquiredImage);
// add image to multipage TIFF file if image is not blank
if (!acquiredImage.IsBlank(0.01f))
acquiredImage.Save(tiffFilename);
// dispose acquired image
acquiredImage.Dispose();
break;
}
}
while (acquireModalState != Vintasoft.Twain.AcquireModalState.None);
// close the device
device.Close();
// close the device manager
deviceManager.Close();
}
}
private static void ProcessAcquiredImage(Vintasoft.Twain.AcquiredImage acquiredImage)
{
System.Console.WriteLine(string.Format("Image ({0})", acquiredImage.ImageInfo));
try
{
// subscribe to processing events
acquiredImage.Processing += new System.EventHandler<Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingEventArgs>(acquiredImage_Processing);
acquiredImage.Progress += new System.EventHandler<Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingProgressEventArgs>(acquiredImage_Progress);
acquiredImage.Processed += new System.EventHandler<Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessedEventArgs>(acquiredImage_Processed);
// despeckle/deskew/detect border
acquiredImage.Despeckle(8, 25, 30, 400);
acquiredImage.Deskew(Vintasoft.Twain.ImageProcessing.TwainBorderColor.AutoDetect, 5, 5);
acquiredImage.DetectBorder(5);
}
catch (System.Exception ex)
{
System.Console.WriteLine("Error: " + ex.Message);
}
finally
{
// unsubscribe from processing events
acquiredImage.Processing -= new System.EventHandler<Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingEventArgs>(acquiredImage_Processing);
acquiredImage.Progress -= new System.EventHandler<Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingProgressEventArgs>(acquiredImage_Progress);
acquiredImage.Processed -= new System.EventHandler<Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessedEventArgs>(acquiredImage_Processed);
}
}
static void acquiredImage_Processing(object sender, Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingEventArgs e)
{
System.Console.Write(e.Action.ToString() + " ");
}
static void acquiredImage_Progress(object sender, Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingProgressEventArgs e)
{
System.Console.Write(".");
}
static void acquiredImage_Processed(object sender, Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessedEventArgs e)
{
System.Console.WriteLine(" finished");
}
''' <summary>
''' Acquires images from TWAIN device, processes images and saves only not blank images to a multipage TIFF file.
''' </summary>
Public Shared Sub AcquireImagesFromTwainDeviceAndProcessImages()
' create the device manager
Using deviceManager As New Vintasoft.Twain.DeviceManager()
' open the device manager
deviceManager.Open()
' select the device in the default device selectio ndialog
deviceManager.ShowDefaultDeviceSelectionDialog()
' get reference to the selected device
Dim device As Vintasoft.Twain.Device = deviceManager.DefaultDevice
' specify that device UI must not be used
device.ShowUI = False
' specify that device must be closed after scan
device.DisableAfterAcquire = True
Dim tiffFilename As String = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "multipage.tif")
' acquire images from device
Dim acquireModalState As Vintasoft.Twain.AcquireModalState = Vintasoft.Twain.AcquireModalState.None
Dim acquiredImage As Vintasoft.Twain.AcquiredImage
Do
acquireModalState = device.AcquireModal()
Select Case acquireModalState
Case Vintasoft.Twain.AcquireModalState.ImageAcquired
' get reference to the image acquired from device
acquiredImage = device.AcquiredImage
' despeckle/deskew/detect border
ProcessAcquiredImage(acquiredImage)
' add image to multipage TIFF file if image is not blank
If Not acquiredImage.IsBlank(0.01F) Then
acquiredImage.Save(tiffFilename)
End If
' dispose acquired image
acquiredImage.Dispose()
Exit Select
End Select
Loop While acquireModalState <> Vintasoft.Twain.AcquireModalState.None
' close the device
device.Close()
' close the device manager
deviceManager.Close()
End Using
End Sub
Private Shared Sub ProcessAcquiredImage(acquiredImage As Vintasoft.Twain.AcquiredImage)
System.Console.WriteLine(String.Format("Image ({0})", acquiredImage.ImageInfo))
Try
' subscribe to processing events
AddHandler acquiredImage.Processing, New System.EventHandler(Of Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingEventArgs)(AddressOf acquiredImage_Processing)
AddHandler acquiredImage.Progress, New System.EventHandler(Of Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingProgressEventArgs)(AddressOf acquiredImage_Progress)
AddHandler acquiredImage.Processed, New System.EventHandler(Of Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessedEventArgs)(AddressOf acquiredImage_Processed)
' despeckle/deskew/detect border
acquiredImage.Despeckle(8, 25, 30, 400)
acquiredImage.Deskew(Vintasoft.Twain.ImageProcessing.TwainBorderColor.AutoDetect, 5, 5)
acquiredImage.DetectBorder(5)
Catch ex As System.Exception
System.Console.WriteLine("Error: " & ex.Message)
Finally
' unsubscribe from processing events
RemoveHandler acquiredImage.Processing, New System.EventHandler(Of Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingEventArgs)(AddressOf acquiredImage_Processing)
RemoveHandler acquiredImage.Progress, New System.EventHandler(Of Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingProgressEventArgs)(AddressOf acquiredImage_Progress)
RemoveHandler acquiredImage.Processed, New System.EventHandler(Of Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessedEventArgs)(AddressOf acquiredImage_Processed)
End Try
End Sub
Private Shared Sub acquiredImage_Processing(sender As Object, e As Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingEventArgs)
System.Console.Write(e.Action.ToString() & " ")
End Sub
Private Shared Sub acquiredImage_Progress(sender As Object, e As Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessingProgressEventArgs)
System.Console.Write(".")
End Sub
Private Shared Sub acquiredImage_Processed(sender As Object, e As Vintasoft.Twain.ImageProcessing.TwainAcquiredImageProcessedEventArgs)
System.Console.WriteLine(" finished")
End Sub