VintaSoft Twain .NET SDK v10.2
Vintasoft.Twain Namespace / AcquiredImage Class / GetAsStream Method
Encoder settings.
Example




In This Topic
    GetAsStream Method (AcquiredImage)
    In This Topic
    Returns this image as a System.IO.MemoryStream object.
    Syntax
    'Declaration
     
    Public Function GetAsStream( _
       ByVal encoderSettings As TwainImageEncoderSettings _
    ) As MemoryStream
    'Usage
     
    Dim instance As AcquiredImage
    Dim encoderSettings As TwainImageEncoderSettings
    Dim value As MemoryStream
     
    value = instance.GetAsStream(encoderSettings)

    Parameters

    encoderSettings
    Encoder settings.

    Return Value

    This image as System.IO.MemoryStream.
    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 example 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 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 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 acquireModalState1 As AcquireModalState = AcquireModalState.None
            Dim mem As MemoryStream = Nothing
            Dim firstImage As Boolean = True
            Do
                acquireModalState1 = device.AcquireModal()
                Select Case acquireModalState1
                    Case 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 TwainPdfEncoderSettings())
                            firstImage = False
                        Else
                            ' add image to PDF stored in the memory
                            device.AcquiredImage.Save(mem, New TwainPdfEncoderSettings())
                        End If
                
                        ' dispose the acquired image
                        device.AcquiredImage.Dispose()
                        Exit Select
                End Select
            Loop While acquireModalState1 <> 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 (DeviceManager deviceManager = new DeviceManager())
        {
            // open the device manager
            deviceManager.Open();
                
            // select the device in the default device selectio ndialog
            deviceManager.ShowDefaultDeviceSelectionDialog();
                
            // get reference to the selected device
            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
            AcquireModalState acquireModalState = AcquireModalState.None;
            MemoryStream mem = null;
            bool firstImage = true;
            do
            {
                acquireModalState = device.AcquireModal();
                switch (acquireModalState)
                {
                    case 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 TwainPdfEncoderSettings());
                            firstImage = false;
                        }
                        else
                        {
                            // add image to PDF stored in the memory
                            device.AcquiredImage.Save(mem, new TwainPdfEncoderSettings());
                        }
                
                        // dispose the acquired image
                        device.AcquiredImage.Dispose();
                        break;
                }
            }
            while (acquireModalState != 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: Windows 8, Windows 7, Windows Vista, Windows XP, Windows Server 2012, Windows Server 2008, Windows Server 2003

    See Also