Как сохранить TWAIN отсканированные изображения в базу данных?
В этом разделе
Вот C#/VB.NET код, который демонстрирует, как получить изображения от TWAIN сканера и сохранить изображения в памяти в виде многостраничного PDF документа. Позже PDF документ может быть сохранен в базу данных или передан на сервер.
/// <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