Как сохранить отсканированное изображение в формате JPEG?
В этом разделе
Вы можете получить изображение от TWAIN сканера и сохранить его как файл JPEG тремя способами:
1. Использование режима передачи данных "Native" или "Memory"
- Этот метод работает с любым TWAIN-сканером.
- Изображение кодируется с помощью внутреннего кодера SDK.
- Качество JPEG можно установить с помощью свойства Vintasoft.Twain.ImageEncoders.TwainJpegEncoderSettings.JpegQuality.
- Изображение сохраняется в файл JPEG с помощью метода Vintasoft.Twain.AcquiredImage.Save.
-
Вот пример, который демонстрирует, как получить изображение и сохранить его как JPEG файл, используя JPEG кодер SDK:
namespace TwainExamples_CSharp
{
/// <summary>
/// Class allows to scan non compressed image and save image to a disk as JPEG file.
/// </summary>
public partial class How_to_scan_non_compressed_image_and_save_image_to_disk_as_Jpeg_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_Jpeg_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.TwainJpegEncoderSettings jpegCodecParams = new Vintasoft.Twain.ImageEncoders.TwainJpegEncoderSettings();
jpegCodecParams.JpegQuality = 50;
// save acquired image to disk using JPEG encoder of SDK
e.Image.Save("test.jpg", jpegCodecParams);
// 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 JPEG file.
''' </summary>
Partial Public Class How_to_scan_non_compressed_image_and_save_image_to_disk_as_Jpeg_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 jpegCodecParams As New Vintasoft.Twain.ImageEncoders.TwainJpegEncoderSettings()
jpegCodecParams.JpegQuality = 50
' save acquired image to disk using JPEG encoder of SDK
e.Image.Save("test.jpg", jpegCodecParams)
' 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. Использование режима передачи файлов
- Драйвер TWAIN должен иметь встроенный кодировщик JPEG - вы можете проверить это с помощью метода Vintasoft.Twain.Device.GetSupportedImageFileFormats.
- Драйвер TWAIN кодирует полученное изображение и сохраняет его непосредственно на диск.
- Сжатие JPEG можно установить с помощью свойства Vintasoft.Twain.Device.FileFormat.
- Имя файла JPEG можно задать с помощью свойства Vintasoft.Twain.Device.FileName.
- Качество файла JPEG можно установить с помощью свойства Vintasoft.Twain.Device.FileJpegQuality.
-
Вот пример, который демонстрирует, как сохранить полученное изображение непосредственно на диск в виде файла JPEG:
namespace TwainExamples_CSharp
{
/// <summary>
/// Class allows to scan and save image directly to disk as JPEG file.
/// </summary>
public partial class How_to_scan_and_save_compressed_image_directly_to_disk_as_Jpeg_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_Jpeg_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 JPEG files
Vintasoft.Twain.TwainImageFileFormat[] supportedImageFileFormats = device.GetSupportedImageFileFormats();
bool isJpegFileFormatSupported = false;
for (int i = 0; i < supportedImageFileFormats.Length; i++)
{
if (supportedImageFileFormats[i] == Vintasoft.Twain.TwainImageFileFormat.Jpeg)
{
isJpegFileFormatSupported = true;
break;
}
}
//
if (!isJpegFileFormatSupported)
{
// close the device
device.Close();
System.Windows.Forms.MessageBox.Show("Device cannot save acquired image directly to disk as JPEG 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 JPEG files
device.FileFormat = Vintasoft.Twain.TwainImageFileFormat.Jpeg;
// set JPEG quality
device.FileJpegQuality = 50;
// 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}.jpg", _imageCount);
}
/// <summary>
/// Image is acquired.
/// </summary>
private void device_ImageAcquired(object sender, Vintasoft.Twain.ImageAcquiredEventArgs e)
{
// image is already saved to disk as JPEG 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 JPEG file.
''' </summary>
Partial Public Class How_to_scan_and_save_compressed_image_directly_to_disk_as_Jpeg_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 JPEG files
Dim supportedImageFileFormats As Vintasoft.Twain.TwainImageFileFormat() = device1.GetSupportedImageFileFormats()
Dim isJpegFileFormatSupported As Boolean = False
For i As Integer = 0 To supportedImageFileFormats.Length - 1
If supportedImageFileFormats(i) = Vintasoft.Twain.TwainImageFileFormat.Jpeg Then
isJpegFileFormatSupported = True
Exit For
End If
Next
'
If Not isJpegFileFormatSupported Then
' close the device
device1.Close()
MessageBox.Show("Device cannot save acquired image directly to disk as JPEG 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 JPEG files
device1.FileFormat = Vintasoft.Twain.TwainImageFileFormat.Jpeg
' set JPEG quality
device1.FileJpegQuality = 50
' 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}.jpg", _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 JPEG 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
3. Использование режима передачи данных в память
- Драйвер TWAIN должен иметь встроенный кодировщик JPEG - вы можете проверить это с помощью метода Vintasoft.Twain.Device.GetSupportedImageCompressions.
- Драйвер TWAIN возвращает полученное изображение в виде потока JPEG.
- Сжатие JPEG можно установить с помощью свойства Vintasoft.Twain.Device.ImageCompression.
- Качество файла JPEG можно установить с помощью характеристики Vintasoft.Twain.DeviceCapabilityId.IJpegQuality.
-
Вот пример, который демонстрирует, как получить изображение в виде потока JPEG и сохранить его на диске:
namespace TwainExamples_CSharp
{
/// <summary>
/// Class allows to scan compressed image and save image to a disk as JPEG file.
/// </summary>
public partial class How_to_scan_compressed_image_and_save_image_to_disk_as_Jpeg_file : System.Windows.Forms.Form
{
/// <summary>
/// TWAIN device manager.
/// </summary>
Vintasoft.Twain.DeviceManager _deviceManager;
public How_to_scan_compressed_image_and_save_image_to_disk_as_Jpeg_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 return image data as JPEG stream
Vintasoft.Twain.TwainImageCompression[] supportedImageCompressions = device.GetSupportedImageCompressions();
bool isJpegCompressionSupported = false;
for (int i = 0; i < supportedImageCompressions.Length; i++)
{
if (supportedImageCompressions[i] == Vintasoft.Twain.TwainImageCompression.Jpeg)
{
isJpegCompressionSupported = true;
break;
}
}
//
if (!isJpegCompressionSupported)
{
// close the device
device.Close();
System.Windows.Forms.MessageBox.Show("Device driver cannot return image data as JPEG stream.");
return;
}
// 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 transfer mode
device.TransferMode = Vintasoft.Twain.TransferMode.Memory;
// set JPEG compression for image data
device.ImageCompression = Vintasoft.Twain.TwainImageCompression.Jpeg;
// set JPEG quality
Vintasoft.Twain.DeviceCapability jpegQualityCap = device.Capabilities.Find(Vintasoft.Twain.DeviceCapabilityId.IJpegQuality);
if (jpegQualityCap != null)
jpegQualityCap.SetValue((int)50);
// 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)
{
// save acquired image to disk, SDK will save JPEG stream received
// from device driver
e.Image.Save("test.jpg");
// 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 compressed image and save image to a disk as JPEG file.
''' </summary>
Partial Public Class How_to_scan_compressed_image_and_save_image_to_disk_as_Jpeg_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.DefaultDevice
' open the device
device1.Open()
' check if device driver can return image data as JPEG stream
Dim supportedImageCompressions As Vintasoft.Twain.TwainImageCompression() = device1.GetSupportedImageCompressions()
Dim isJpegCompressionSupported As Boolean = False
For i As Integer = 0 To supportedImageCompressions.Length - 1
If supportedImageCompressions(i) = Vintasoft.Twain.TwainImageCompression.Jpeg Then
isJpegCompressionSupported = True
Exit For
End If
Next
'
If Not isJpegCompressionSupported Then
' close the device
device1.Close()
MessageBox.Show("Device driver cannot return image data as JPEG stream.")
Return
End If
' 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 transfer mode
device1.TransferMode = Vintasoft.Twain.TransferMode.Memory
' set JPEG compression for image data
device1.ImageCompression = Vintasoft.Twain.TwainImageCompression.Jpeg
' set JPEG quality
Dim jpegQualityCap As Vintasoft.Twain.DeviceCapability = device1.Capabilities.Find(Vintasoft.Twain.DeviceCapabilityId.IJpegQuality)
If jpegQualityCap IsNot Nothing Then
jpegQualityCap.SetValue(CInt(50))
End If
' 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)
' save acquired image to disk, SDK will save JPEG stream received
' from device driver
e.Image.Save("test.jpg")
' 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