VintaSoft Twain .NET SDK 15.4: Documentation for .NET developer
Vintasoft.Twain Namespace / AcquiredImage Class / GetAsStream(TwainImageEncoderSettings) Method
Syntax Exceptions Remarks Example Requirements SeeAlso
In This Topic
GetAsStream(TwainImageEncoderSettings) Method (AcquiredImage)
In This Topic
Returns this image as a System.IO.MemoryStream object.
Syntax
Exceptions
ExceptionDescription
Thrown if error occurs at the image saving.
Remarks

Method:

  • Returns an image data received from scanner without modifications if image data can be saved to specified image file format, for example, scanner returned image data compressed with CCITT4 compression and you want get data as TIFF file (TIFF supports CCITT4 compression).
  • Converts image data received from scanner if image data cannot be saved to specified image file format, for example, scanner returned image data compressed with CCITT4 compression and you want get data as BMP file (BMP does not support CCITT4 compression).

Example

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();
    }
}

Requirements

Target Platforms: .NET 10; .NET 9; .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

See Also