How to save acquired image as TIFF file?
In This Topic
You can acquire image from TWAIN scanner and save it 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 and save image as TIFF file using TIFF encoder of SDK:
namespace TwainExamples_CSharp
{
/// <summary>
/// Class allows to scan non compressed image and save image to a disk as TIFF file.
/// </summary>
public partial class How_to_scan_non_compressed_image_and_save_image_to_disk_as_Tiff_file : System.Windows.Forms.Form
{
/// <summary>
/// TWAIN device manager.
/// </summary>
Vintasoft.Twain.DeviceManager _deviceManager;
public How_to_scan_non_compressed_image_and_save_image_to_disk_as_Tiff_file()
{
InitializeComponent();
// create and open device manager
_deviceManager = new Vintasoft.Twain.DeviceManager(this);
_deviceManager.Open();
}
/// <summary>
/// Acquire images asynchronously.
/// </summary>
public void AcquireImagesAsynchronously()
{
try
{
// get reference to the default device
Vintasoft.Twain.Device device = _deviceManager.OpenedDevice;
// subscribe to the device events
device.ImageAcquired += new System.EventHandler<Vintasoft.Twain.ImageAcquiredEventArgs>(device_ImageAcquired);
device.ScanCanceled += new System.EventHandler(device_ScanCanceled);
device.ScanFailed += new System.EventHandler<Vintasoft.Twain.ScanFailedEventArgs>(device_ScanFailed);
device.ScanFinished += new System.EventHandler(device_ScanFinished);
// disable UI
device.ShowUI = false;
// set Memory (or Native) transfer mode
device.TransferMode = Vintasoft.Twain.TransferMode.Memory;
// acquire images asynchronously
device.Acquire();
}
catch (Vintasoft.Twain.TwainException ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
/// <summary>
/// Image is acquired.
/// </summary>
private void device_ImageAcquired(object sender, Vintasoft.Twain.ImageAcquiredEventArgs e)
{
Vintasoft.Twain.ImageEncoders.TwainTiffEncoderSettings twainCodecParams = new Vintasoft.Twain.ImageEncoders.TwainTiffEncoderSettings();
twainCodecParams.TiffCompression = Vintasoft.Twain.ImageEncoders.TiffCompression.LZW;
twainCodecParams.TiffMultiPage = true;
// save acquired image to disk using TIFF encoder of SDK
e.Image.Save("test.tiff", twainCodecParams);
// dispose the acquired image
e.Image.Dispose();
}
/// <summary>
/// Scan is canceled.
/// </summary>
private void device_ScanCanceled(object sender, System.EventArgs e)
{
System.Windows.Forms.MessageBox.Show("Scan is canceled.");
}
/// <summary>
/// Scan is failed.
/// </summary>
private void device_ScanFailed(object sender, Vintasoft.Twain.ScanFailedEventArgs e)
{
System.Windows.Forms.MessageBox.Show("Scan is failed: " + e.ErrorString);
}
/// <summary>
/// Scan is finished.
/// </summary>
void device_ScanFinished(object sender, System.EventArgs e)
{
Vintasoft.Twain.Device device = (Vintasoft.Twain.Device)sender;
// unsubscribe from device events
device.ImageAcquired -= new System.EventHandler<Vintasoft.Twain.ImageAcquiredEventArgs>(device_ImageAcquired);
device.ScanCanceled -= new System.EventHandler(device_ScanCanceled);
device.ScanFailed -= new System.EventHandler<Vintasoft.Twain.ScanFailedEventArgs>(device_ScanFailed);
device.ScanFinished -= new System.EventHandler(device_ScanFinished);
// if device is not closed
if (device.State != Vintasoft.Twain.DeviceState.Closed)
// close the device
device.Close();
System.Windows.Forms.MessageBox.Show("Scan is finished.");
}
}
}
''' <summary>
''' Class allows to scan non compressed image and save image to a disk as TIFF file.
''' </summary>
Partial Public Class How_to_scan_non_compressed_image_and_save_image_to_disk_as_Tiff_file
Inherits Form
''' <summary>
''' TWAIN device manager.
''' </summary>
Private _deviceManager As Vintasoft.Twain.DeviceManager
Public Sub New()
InitializeComponent()
' create and open device manager
_deviceManager = New Vintasoft.Twain.DeviceManager(Me)
_deviceManager.Open()
End Sub
''' <summary>
''' Acquire images asynchronously.
''' </summary>
Public Sub AcquireImagesAsynchronously()
Try
' get reference to the default device
Dim device1 As Vintasoft.Twain.Device = _deviceManager.OpenedDevice
' subscribe to the device events
AddHandler device1.ImageAcquired, New EventHandler(Of Vintasoft.Twain.ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
AddHandler device1.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
AddHandler device1.ScanFailed, New EventHandler(Of Vintasoft.Twain.ScanFailedEventArgs)(AddressOf device_ScanFailed)
AddHandler device1.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
' disable UI
device1.ShowUI = False
' set Memory (or Native) transfer mode
device1.TransferMode = Vintasoft.Twain.TransferMode.Memory
' acquire images asynchronously
device1.Acquire()
Catch ex As Vintasoft.Twain.TwainException
MessageBox.Show(ex.Message)
End Try
End Sub
''' <summary>
''' Image is acquired.
''' </summary>
Private Sub device_ImageAcquired(ByVal sender As Object, ByVal e As Vintasoft.Twain.ImageAcquiredEventArgs)
Dim twainCodecParams As New Vintasoft.Twain.ImageEncoders.TwainTiffEncoderSettings()
twainCodecParams.TiffCompression = Vintasoft.Twain.ImageEncoders.TiffCompression.LZW
twainCodecParams.TiffMultiPage = True
' save acquired image to disk using TIFF encoder of SDK
e.Image.Save("test.tiff", twainCodecParams)
' dispose the acquired image
e.Image.Dispose()
End Sub
''' <summary>
''' Scan is canceled.
''' </summary>
Private Sub device_ScanCanceled(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("Scan is canceled.")
End Sub
''' <summary>
''' Scan is failed.
''' </summary>
Private Sub device_ScanFailed(ByVal sender As Object, ByVal e As Vintasoft.Twain.ScanFailedEventArgs)
MessageBox.Show("Scan is failed: " + Convert.ToString(e.ErrorString))
End Sub
''' <summary>
''' Scan is finished.
''' </summary>
Private Sub device_ScanFinished(ByVal sender As Object, ByVal e As EventArgs)
Dim device1 As Vintasoft.Twain.Device = DirectCast(sender, Vintasoft.Twain.Device)
' unsubscribe from device events
RemoveHandler device1.ImageAcquired, New EventHandler(Of Vintasoft.Twain.ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
RemoveHandler device1.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
RemoveHandler device1.ScanFailed, New EventHandler(Of Vintasoft.Twain.ScanFailedEventArgs)(AddressOf device_ScanFailed)
RemoveHandler device1.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
' if device is not closed
If device1.State <> Vintasoft.Twain.DeviceState.Closed Then
' close the device
device1.Close()
End If
MessageBox.Show("Scan is finished.")
End Sub
End Class
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 save acquired image directly to disk as TIFF file:
namespace TwainExamples_CSharp
{
/// <summary>
/// Class allows to scan and save image directly to disk as TIFF file.
/// </summary>
public partial class How_to_scan_and_save_compressed_image_directly_to_disk_as_Tiff_file : System.Windows.Forms.Form
{
/// <summary>
/// TWAIN device manager.
/// </summary>
Vintasoft.Twain.DeviceManager _deviceManager;
/// <summary>
/// Image count.
/// </summary>
int _imageCount = 0;
public How_to_scan_and_save_compressed_image_directly_to_disk_as_Tiff_file()
{
InitializeComponent();
// create and open device manager
_deviceManager = new Vintasoft.Twain.DeviceManager(this);
_deviceManager.Open();
}
/// <summary>
/// Acquire images asynchronously.
/// </summary>
public void AcquireImagesAsynchronously()
{
try
{
// get reference to the default device
Vintasoft.Twain.Device device = _deviceManager.DefaultDevice;
// 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;
}
// subscribe to the device events
device.ImageAcquiring += new System.EventHandler<Vintasoft.Twain.ImageAcquiringEventArgs>(device_ImageAcquiring);
device.ImageAcquired += new System.EventHandler<Vintasoft.Twain.ImageAcquiredEventArgs>(device_ImageAcquired);
device.ScanCanceled += new System.EventHandler(device_ScanCanceled);
device.ScanFailed += new System.EventHandler<Vintasoft.Twain.ScanFailedEventArgs>(device_ScanFailed);
device.ScanFinished += new System.EventHandler(device_ScanFinished);
// disable UI
device.ShowUI = false;
// set the File transfer mode
device.TransferMode = Vintasoft.Twain.TransferMode.File;
// save images as TIFF files
device.FileFormat = Vintasoft.Twain.TwainImageFileFormat.Tiff;
// acquire images asynchronously
device.Acquire();
}
catch (Vintasoft.Twain.TwainException ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
/// <summary>
/// Image is acquiring.
/// </summary>
private void device_ImageAcquiring(object sender, Vintasoft.Twain.ImageAcquiringEventArgs e)
{
// set the filename for image
e.Filename = string.Format("test{0}.tif", _imageCount);
}
/// <summary>
/// Image is acquired.
/// </summary>
private void device_ImageAcquired(object sender, Vintasoft.Twain.ImageAcquiredEventArgs e)
{
// image is already saved to disk as TIFF file
// increment image count
_imageCount = _imageCount + 1;
}
/// <summary>
/// Scan is canceled.
/// </summary>
private void device_ScanCanceled(object sender, System.EventArgs e)
{
System.Windows.Forms.MessageBox.Show("Scan is canceled.");
}
/// <summary>
/// Scan is failed.
/// </summary>
private void device_ScanFailed(object sender, Vintasoft.Twain.ScanFailedEventArgs e)
{
System.Windows.Forms.MessageBox.Show("Scan is failed: " + e.ErrorString);
}
/// <summary>
/// Scan is finished.
/// </summary>
void device_ScanFinished(object sender, System.EventArgs e)
{
Vintasoft.Twain.Device device = (Vintasoft.Twain.Device)sender;
// unsubscribe from device events
device.ImageAcquired -= new System.EventHandler<Vintasoft.Twain.ImageAcquiredEventArgs>(device_ImageAcquired);
device.ScanCanceled -= new System.EventHandler(device_ScanCanceled);
device.ScanFailed -= new System.EventHandler<Vintasoft.Twain.ScanFailedEventArgs>(device_ScanFailed);
device.ScanFinished -= new System.EventHandler(device_ScanFinished);
// if device is not closed
if (device.State != Vintasoft.Twain.DeviceState.Closed)
// close the device
device.Close();
System.Windows.Forms.MessageBox.Show("Scan is finished.");
}
}
}
''' <summary>
''' Class allows to scan and save image directly to disk as TIFF file.
''' </summary>
Partial Public Class How_to_scan_and_save_compressed_image_directly_to_disk_as_Tiff_file
Inherits Form
''' <summary>
''' TWAIN device manager.
''' </summary>
Private _deviceManager As Vintasoft.Twain.DeviceManager
''' <summary>
''' Image count.
''' </summary>
Private _imageCount As Integer = 0
Public Sub New()
InitializeComponent()
' create and open device manager
_deviceManager = New Vintasoft.Twain.DeviceManager(Me)
_deviceManager.Open()
End Sub
''' <summary>
''' Acquire images asynchronously.
''' </summary>
Public Sub AcquireImagesAsynchronously()
Try
' get reference to the default device
Dim device1 As Vintasoft.Twain.Device = _deviceManager.DefaultDevice
' open the device
device1.Open()
' check if device driver can save image to disk as TIFF files
Dim supportedImageFileFormats As Vintasoft.Twain.TwainImageFileFormat() = device1.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
device1.Close()
MessageBox.Show("Device cannot save acquired image directly to disk as TIFF image.")
Return
End If
' subscribe to the device events
AddHandler device1.ImageAcquiring, New EventHandler(Of Vintasoft.Twain.ImageAcquiringEventArgs)(AddressOf device_ImageAcquiring)
AddHandler device1.ImageAcquired, New EventHandler(Of Vintasoft.Twain.ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
AddHandler device1.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
AddHandler device1.ScanFailed, New EventHandler(Of Vintasoft.Twain.ScanFailedEventArgs)(AddressOf device_ScanFailed)
AddHandler device1.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
' disable UI
device1.ShowUI = False
' set the File transfer mode
device1.TransferMode = Vintasoft.Twain.TransferMode.File
' save images as TIFF files
device1.FileFormat = Vintasoft.Twain.TwainImageFileFormat.Tiff
' acquire images asynchronously
device1.Acquire()
Catch ex As Vintasoft.Twain.TwainException
MessageBox.Show(ex.Message)
End Try
End Sub
''' <summary>
''' Image is acquiring.
''' </summary>
Private Sub device_ImageAcquiring(ByVal sender As Object, ByVal e As Vintasoft.Twain.ImageAcquiringEventArgs)
' set the filename for image
e.Filename = String.Format("test{0}.tif", _imageCount)
End Sub
''' <summary>
''' Image is acquired.
''' </summary>
Private Sub device_ImageAcquired(ByVal sender As Object, ByVal e As Vintasoft.Twain.ImageAcquiredEventArgs)
' image is already saved to disk as TIFF file
' increment image count
_imageCount = _imageCount + 1
End Sub
''' <summary>
''' Scan is canceled.
''' </summary>
Private Sub device_ScanCanceled(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("Scan is canceled.")
End Sub
''' <summary>
''' Scan is failed.
''' </summary>
Private Sub device_ScanFailed(ByVal sender As Object, ByVal e As Vintasoft.Twain.ScanFailedEventArgs)
MessageBox.Show("Scan is failed: " + Convert.ToString(e.ErrorString))
End Sub
''' <summary>
''' Scan is finished.
''' </summary>
Private Sub device_ScanFinished(ByVal sender As Object, ByVal e As EventArgs)
Dim device1 As Vintasoft.Twain.Device = DirectCast(sender, Vintasoft.Twain.Device)
' unsubscribe from device events
RemoveHandler device1.ImageAcquired, New EventHandler(Of Vintasoft.Twain.ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
RemoveHandler device1.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
RemoveHandler device1.ScanFailed, New EventHandler(Of Vintasoft.Twain.ScanFailedEventArgs)(AddressOf device_ScanFailed)
RemoveHandler device1.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
' if device is not closed
If device1.State <> Vintasoft.Twain.DeviceState.Closed Then
' close the device
device1.Close()
End If
MessageBox.Show("Scan is finished.")
End Sub
End Class