Как сохранить TWAIN отсканированное изображение в формате TIFF?
В этом разделе
Вы можете получить изображение от TWAIN сканера и сохранить его в файл TIFF двумя способами:
1. C помощью JPEG кодека из SDK для режимов передачи Native или Memory
- Этот метод работает с любым TWAIN сканером.
- Изображение кодируется с помощью внутреннего кодека SDK.
-
Изображение сохраняется в файл TIFF с помощью метода Vintasoft.Twain.AcquiredImage.Save.
-
Вот C#/VB.NET код, который демонстрирует, как отсканировать изображение и сохранить его в файл TIFF, используя кодик TIFF из 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. С помощью JPEG кодека TWAIN драйвера для режима передачи File
- Драйвер TWAIN должен иметь встроенный кодек TIFF - вы можете проверить это с помощью метода Vintasoft.Twain.Device.GetSupportedImageFileFormats.
- Драйвер TWAIN кодирует полученное изображение и сохраняет его непосредственно в файл на диск.
-
Имя файла TIFF можно задать с помощью свойства Vintasoft.Twain.Device.FileName.
-
Вот C#/VB.NET код, который демонстрирует, как сохранить полученное изображение непосредственно на диск в виде файла TIFF:
/// <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