How to save TWAIN acquired image as TIFF file?
In This Topic
You can acquire image from TWAIN scanner and save image as TIFF file by 2 ways:
1. Using Native or Memory transfer mode
- This method works with any TWAIN scanner.
- Image is encoded using internal encoder of SDK.
-
Image is saved as TIFF file using the AcquiredImage.Save method.
-
Here is an example that demonstrates how to acquire image from TWAIN device and save image as TIFF file using TIFF encoder of SDK:
/// <summary>
/// Synchronously acquires not compressed images from TWAIN device and saves images to a multipage TIFF file.
/// </summary>
static void AcquireNotCompressedImagesFromTwainDeviceAndSaveImagesToTiffFile()
{
// create the device manager
using (Vintasoft.Twain.DeviceManager deviceManager = new Vintasoft.Twain.DeviceManager())
{
// open the device manager
deviceManager.Open();
// get reference to the default device
Vintasoft.Twain.Device device = deviceManager.DefaultDevice;
// specify that device UI must not be used
device.ShowUI = false;
// open the device
device.Open();
// specify that Memory transfer mode must be used
device.TransferMode = Vintasoft.Twain.TransferMode.Memory;
// acquire images from device
Vintasoft.Twain.AcquireModalState acquireModalState;
do
{
acquireModalState = device.AcquireModal();
switch (acquireModalState)
{
case Vintasoft.Twain.AcquireModalState.ImageAcquired:
Vintasoft.Twain.ImageEncoders.TwainTiffEncoderSettings twainCodecParams =
new Vintasoft.Twain.ImageEncoders.TwainTiffEncoderSettings();
twainCodecParams.TiffCompression = Vintasoft.Twain.ImageEncoders.TwainTiffCompression.LZW;
twainCodecParams.TiffMultiPage = true;
// save acquired image to disk using TIFF encoder of SDK
device.AcquiredImage.Save("test.tiff", twainCodecParams);
// dispose the acquired image
device.AcquiredImage.Dispose();
break;
}
}
while (acquireModalState != Vintasoft.Twain.AcquireModalState.None);
// close the device
device.Close();
// close the device manager
deviceManager.Close();
}
}
''' <summary>
''' Synchronously acquires not compressed images from TWAIN device and saves images to a multipage TIFF file.
''' </summary>
Private Shared Sub AcquireNotCompressedImagesFromTwainDeviceAndSaveImagesToTiffFile()
' create the device manager
Using deviceManager As New Vintasoft.Twain.DeviceManager()
' open the device manager
deviceManager.Open()
' get reference to the default device
Dim device As Vintasoft.Twain.Device = deviceManager.DefaultDevice
' specify that device UI must not be used
device.ShowUI = False
' open the device
device.Open()
' specify that Memory transfer mode must be used
device.TransferMode = Vintasoft.Twain.TransferMode.Memory
' acquire images from device
Dim acquireModalState As Vintasoft.Twain.AcquireModalState
Do
acquireModalState = device.AcquireModal()
Select Case acquireModalState
Case Vintasoft.Twain.AcquireModalState.ImageAcquired
Dim twainCodecParams As New Vintasoft.Twain.ImageEncoders.TwainTiffEncoderSettings()
twainCodecParams.TiffCompression = Vintasoft.Twain.ImageEncoders.TwainTiffCompression.LZW
twainCodecParams.TiffMultiPage = True
' save acquired image to disk using TIFF encoder of SDK
device.AcquiredImage.Save("test.tiff", twainCodecParams)
' dispose the acquired image
device.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
2. Using File transfer mode
- TWAIN driver must have built-in TIFF encoder - you can check this with the Device.GetSupportedImageFileFormats method.
- TWAIN driver encodes acquired image and saves directly to disk.
-
Filename of TIFF file can be set using the Device.FileName property.
-
Here is an example that demonstrates how to specify that TWAIN scanner should save acquired image directly to a TIFF file on disk:
/// <summary>
/// Starts synchronous acquisition of image from TWAIN device and specifies that acquired images must be saved directly to TIFF files on disk.
/// </summary>
static void StartSynchronousTwainScanningAndSpecifyThatImagesMustBeSavedDirectlyToTiffFiles()
{
// create the device manager
using (Vintasoft.Twain.DeviceManager deviceManager = new Vintasoft.Twain.DeviceManager())
{
// open the device manager
deviceManager.Open();
// get reference to the default device
Vintasoft.Twain.Device device = deviceManager.DefaultDevice;
// specify that device UI must not be used
device.ShowUI = false;
// open the device
device.Open();
// check if device driver can save image to disk as TIFF files
Vintasoft.Twain.TwainImageFileFormat[] supportedImageFileFormats = device.GetSupportedImageFileFormats();
bool isTiffFileFormatSupported = false;
for (int i = 0; i < supportedImageFileFormats.Length; i++)
{
if (supportedImageFileFormats[i] == Vintasoft.Twain.TwainImageFileFormat.Tiff)
{
isTiffFileFormatSupported = true;
break;
}
}
//
if (!isTiffFileFormatSupported)
{
// close the device
device.Close();
System.Windows.Forms.MessageBox.Show("Device cannot save acquired image directly to disk as TIFF image.");
return;
}
// specify that File transfer mode must be used
device.TransferMode = Vintasoft.Twain.TransferMode.File;
// save images as JPEG files
device.FileFormat = Vintasoft.Twain.TwainImageFileFormat.Tiff;
int acquiredImageCount = 0;
// acquire images from device
Vintasoft.Twain.AcquireModalState acquireModalState;
do
{
acquireModalState = device.AcquireModal();
switch (acquireModalState)
{
case Vintasoft.Twain.AcquireModalState.ImageAcquiring:
// set the filename for image
device.FileName = string.Format("test{0}.tif", acquiredImageCount);
break;
case Vintasoft.Twain.AcquireModalState.ImageAcquired:
// image is already saved to disk as TIFF file
// increment image count
acquiredImageCount = acquiredImageCount + 1;
break;
}
}
while (acquireModalState != Vintasoft.Twain.AcquireModalState.None);
// close the device
device.Close();
// close the device manager
deviceManager.Close();
}
}
''' <summary>
''' Starts synchronous acquisition of image from TWAIN device and specifies that acquired images must be saved directly to TIFF files on disk.
''' </summary>
Private Shared Sub StartSynchronousTwainScanningAndSpecifyThatImagesMustBeSavedDirectlyToTiffFiles()
' create the device manager
Using deviceManager As New Vintasoft.Twain.DeviceManager()
' open the device manager
deviceManager.Open()
' get reference to the default device
Dim device As Vintasoft.Twain.Device = deviceManager.DefaultDevice
' specify that device UI must not be used
device.ShowUI = False
' open the device
device.Open()
' check if device driver can save image to disk as TIFF files
Dim supportedImageFileFormats As Vintasoft.Twain.TwainImageFileFormat() = device.GetSupportedImageFileFormats()
Dim isTiffFileFormatSupported As Boolean = False
For i As Integer = 0 To supportedImageFileFormats.Length - 1
If supportedImageFileFormats(i) = Vintasoft.Twain.TwainImageFileFormat.Tiff Then
isTiffFileFormatSupported = True
Exit For
End If
Next
'
If Not isTiffFileFormatSupported Then
' close the device
device.Close()
System.Windows.Forms.MessageBox.Show("Device cannot save acquired image directly to disk as TIFF image.")
Return
End If
' specify that File transfer mode must be used
device.TransferMode = Vintasoft.Twain.TransferMode.File
' save images as JPEG files
device.FileFormat = Vintasoft.Twain.TwainImageFileFormat.Tiff
Dim acquiredImageCount As Integer = 0
' acquire images from device
Dim acquireModalState As Vintasoft.Twain.AcquireModalState
Do
acquireModalState = device.AcquireModal()
Select Case acquireModalState
Case Vintasoft.Twain.AcquireModalState.ImageAcquiring
' set the filename for image
device.FileName = String.Format("test{0}.tif", acquiredImageCount)
Exit Select
Case Vintasoft.Twain.AcquireModalState.ImageAcquired
' image is already saved to disk as TIFF file
' increment image count
acquiredImageCount = acquiredImageCount + 1
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