GetAsStream(TwainImageEncoderSettings) Method (AcquiredImage)
This C#/VB.NET code shows how to store all acquired images as multipage PDF document in the memory. Further PDF document can be saved into database or transferred to the server.
Private Shared Sub AcquireImagesAndSaveToStreamAsPdf()
' create the device manager
Using deviceManager As New Vintasoft.Twain.DeviceManager()
' open the device manager
deviceManager.Open()
' select the device in the default device selectio ndialog
deviceManager.ShowDefaultDeviceSelectionDialog()
' get reference to the selected device
Dim device As Vintasoft.Twain.Device = deviceManager.DefaultDevice
' specify that device UI must not be used
device.ShowUI = False
' specify that device must be closed after scan
device.DisableAfterAcquire = True
' acquire images from device
Dim acquireModalState As Vintasoft.Twain.AcquireModalState = Vintasoft.Twain.AcquireModalState.None
Dim mem As System.IO.MemoryStream = Nothing
Dim firstImage As Boolean = True
Do
acquireModalState = device.AcquireModal()
Select Case acquireModalState
Case Vintasoft.Twain.AcquireModalState.ImageAcquired
' save all acquired images as PDF stored in the memory
If firstImage Then
' get the first image as PDF stored in the memory
mem = device.AcquiredImage.GetAsStream(New Vintasoft.Twain.ImageEncoders.TwainPdfEncoderSettings())
firstImage = False
Else
' add image to PDF stored in the memory
device.AcquiredImage.Save(mem, New Vintasoft.Twain.ImageEncoders.TwainPdfEncoderSettings())
End If
' dispose the acquired image
device.AcquiredImage.Dispose()
Exit Select
End Select
Loop While acquireModalState <> Vintasoft.Twain.AcquireModalState.None
' Upload stream data to the server or doing something else...
' ...
' close the device
device.Close()
' close the device manager
deviceManager.Close()
End Using
End Sub
static void AcquireImagesAndSaveToStreamAsPdf()
{
// create the device manager
using (Vintasoft.Twain.DeviceManager deviceManager = new Vintasoft.Twain.DeviceManager())
{
// open the device manager
deviceManager.Open();
// select the device in the default device selectio ndialog
deviceManager.ShowDefaultDeviceSelectionDialog();
// get reference to the selected device
Vintasoft.Twain.Device device = deviceManager.DefaultDevice;
// specify that device UI must not be used
device.ShowUI = false;
// specify that device must be closed after scan
device.DisableAfterAcquire = true;
// acquire images from device
Vintasoft.Twain.AcquireModalState acquireModalState = Vintasoft.Twain.AcquireModalState.None;
System.IO.MemoryStream mem = null;
bool firstImage = true;
do
{
acquireModalState = device.AcquireModal();
switch (acquireModalState)
{
case Vintasoft.Twain.AcquireModalState.ImageAcquired:
// save all acquired images as PDF stored in the memory
if (firstImage)
{
// get the first image as PDF stored in the memory
mem = device.AcquiredImage.GetAsStream(new Vintasoft.Twain.ImageEncoders.TwainPdfEncoderSettings());
firstImage = false;
}
else
{
// add image to PDF stored in the memory
device.AcquiredImage.Save(mem, new Vintasoft.Twain.ImageEncoders.TwainPdfEncoderSettings());
}
// dispose the acquired image
device.AcquiredImage.Dispose();
break;
}
}
while (acquireModalState != Vintasoft.Twain.AcquireModalState.None);
// Upload stream data to the server or doing something else...
// ...
// close the device
device.Close();
// close the device manager
deviceManager.Close();
}
}
Target Platforms: .NET 9; .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5