How to save TWAIN acquired images in the database?
In This Topic
Here is an example that demonstrates how to save TWAIN acquired images as multipage PDF document in memory. Later, PDF document can be saved in database or transferred to server.
/// <summary>
/// Synchronously acquires images from TWAIN device and saves images in a database.
/// </summary>
public void AcquireImagesFromTwainDeviceAndSaveImagesInDatabase()
{
// create TWAIN device manager
using (Vintasoft.Twain.DeviceManager deviceManager = new Vintasoft.Twain.DeviceManager())
{
// open TWAIN device manager
deviceManager.Open();
// get count of TWAIN devices
int deviceCount = deviceManager.Devices.Count;
if (deviceCount == 0)
{
System.Console.WriteLine("Devices are not found.");
return;
}
// select the default TWAIN device
Vintasoft.Twain.Device device = deviceManager.DefaultDevice;
// open TWAIN device
device.Open();
System.IO.MemoryStream pdfDocumentInMemory = null;
// acquire image(s) from the device
Vintasoft.Twain.AcquireModalState acquireModalState;
do
{
acquireModalState = device.AcquireModal();
switch (acquireModalState)
{
case Vintasoft.Twain.AcquireModalState.ImageAcquired:
// if memory stream is empty
if (pdfDocumentInMemory == null)
// get the first image as PDF stored in the memory
pdfDocumentInMemory = device.AcquiredImage.GetAsStream(new Vintasoft.Twain.ImageEncoders.TwainPdfEncoderSettings());
else
// add image to PDF stored in the memory
device.AcquiredImage.Save(pdfDocumentInMemory, new Vintasoft.Twain.ImageEncoders.TwainPdfEncoderSettings());
device.AcquiredImage.Dispose();
break;
case Vintasoft.Twain.AcquireModalState.ScanCompleted:
// save memory stream to a database
break;
case Vintasoft.Twain.AcquireModalState.ScanCanceled:
// output current state
System.Console.WriteLine("Scan canceled.");
break;
case Vintasoft.Twain.AcquireModalState.ScanFailed:
// output current state
System.Console.WriteLine(string.Format("Scan failed: {0}", device.ErrorString));
break;
}
}
while (acquireModalState != Vintasoft.Twain.AcquireModalState.None);
// close SANE device
device.Close();
// close SANE device manager
deviceManager.Close();
}
System.Console.ReadLine();
}
''' <summary>
''' Synchronously acquires images from TWAIN device and saves images in a database.
''' </summary>
Public Sub AcquireImagesFromTwainDeviceAndSaveImagesInDatabase()
' create TWAIN device manager
Using deviceManager As New Vintasoft.Twain.DeviceManager()
' open TWAIN device manager
deviceManager.Open()
' get count of TWAIN devices
Dim deviceCount As Integer = deviceManager.Devices.Count
If deviceCount = 0 Then
System.Console.WriteLine("Devices are not found.")
Return
End If
' select the default TWAIN device
Dim device As Vintasoft.Twain.Device = deviceManager.DefaultDevice
' open TWAIN device
device.Open()
Dim pdfDocumentInMemory As System.IO.MemoryStream = Nothing
' acquire image(s) from the device
Dim acquireModalState As Vintasoft.Twain.AcquireModalState
Do
acquireModalState = device.AcquireModal()
Select Case acquireModalState
Case Vintasoft.Twain.AcquireModalState.ImageAcquired
' if memory stream is empty
If pdfDocumentInMemory Is Nothing Then
' get the first image as PDF stored in the memory
pdfDocumentInMemory = device.AcquiredImage.GetAsStream(New Vintasoft.Twain.ImageEncoders.TwainPdfEncoderSettings())
Else
' add image to PDF stored in the memory
device.AcquiredImage.Save(pdfDocumentInMemory, New Vintasoft.Twain.ImageEncoders.TwainPdfEncoderSettings())
End If
device.AcquiredImage.Dispose()
Exit Select
Case Vintasoft.Twain.AcquireModalState.ScanCompleted
' save memory stream to a database
Exit Select
Case Vintasoft.Twain.AcquireModalState.ScanCanceled
' output current state
System.Console.WriteLine("Scan canceled.")
Exit Select
Case Vintasoft.Twain.AcquireModalState.ScanFailed
' output current state
System.Console.WriteLine(String.Format("Scan failed: {0}", device.ErrorString))
Exit Select
End Select
Loop While acquireModalState <> Vintasoft.Twain.AcquireModalState.None
' close SANE device
device.Close()
' close SANE device manager
deviceManager.Close()
End Using
System.Console.ReadLine()
End Sub