VintaSoftTwain Control v6.0
VintaSoftTwain Object / AcquiredImages_GetImageAsByteArray Method
Zero-based index of image in acquired image collection. Valid values are from 0 to (AcquiredImages_Count-1).
In This Topic
    AcquiredImages_GetImageAsByteArray Method
    In This Topic
    Returns acquired image as byte array.
    Visual Basic
    Public Function AcquiredImages_GetImageAsByteArray( _
       ByVal index As Long _
    ) As Variant
    Zero-based index of image in acquired image collection. Valid values are from 0 to (AcquiredImages_Count-1).
    Return Type
    Image as three dimensional array of bytes:
    - first dimension: (0, ..., 3) - (red, green, blue, alpha)
    - second dimension: (0, ..., width)
    - third dimension: (0, ..., height)
    This method returns a VB picture object for use with other controls that supports the picture object including the standard VB picture control.

    With this method you cannot get really 16-bit gray or 48-bit color images. Images in these formats will be converted to 24-bit images.

    Information about error that occurs during method execution can be get using the Error and ErrorString properties.
    This example shows how to get scanned image as byte array.
    Private VSTwain1 As New VintaSoftTwain()
    ''' <summary>
    ''' Scans images asynchronously.
    ''' </summary>
    Private Sub ScanImages()
        ' open the device manager
        If Not VSTwain1.DeviceManager_Open() Then
            Exit Sub
        End If
        ' select device using standard device selection dialog
        ' open the device
        If Not VSTwain1.Device_Open Then
            Exit Sub
        End If
        ' specify that device UI must be shown
        VSTwain1.Device_ShowUI = True
        ' subscribe to the device events
        AddHandler VSTwain1.DeviceImageAcquired, AddressOf VSTwain1_ImageAcquired
        AddHandler VSTwain1.DeviceScanCompleted, AddressOf VSTwain1_ScanCompleted
        AddHandler VSTwain1.DeviceScanFailed, AddressOf VSTwain1_ScanFailed
        AddHandler VSTwain1.DeviceScanCanceled, AddressOf VSTwain1_ScanCanceled
        ' acquire images asynchronously
    End Sub
    Private Sub VSTwain1_ImageAcquired()
        ' get index of acquired image
        Dim imageIndex As Integer = VSTwain1.AcquiredImages_Count - 1
        'Dim arrPixels() As Byte       ' VB 6.0 syntax
        Dim arrPixels(,,) As Byte      ' VB.NET syntax
        Dim x As Long, y As Long, width As Long, height As Long
        ' get the first image from the buffer
        arrPixels = VSTwain1.AcquiredImages_GetImageAsByteArray(imageIndex)
        If VSTwain1.Error Then
            width = UBound(arrPixels, 2) - LBound(arrPixels, 2)
            height = UBound(arrPixels, 3) - LBound(arrPixels, 3)
            Dim Image1 As Drawing.Bitmap = New Drawing.Bitmap(CInt(width + 1), CInt(height + 1), Drawing.Imaging.PixelFormat.Format24bppRgb)
            For x = width To 0 Step -1
                For y = height To 0 Step -1
                    Image1.SetPixel(x, height - y, Drawing.Color.FromArgb(arrPixels(2, x, y), arrPixels(1, x, y), arrPixels(0, x, y)))
                Next y
            Next x
        End If
    End Sub
    Private Sub VSTwain1_ScanCompleted()
        Console.WriteLine("Scan is completed.")
    End Sub
    Private Sub VSTwain1_ScanFailed(errorString As String)
        Console.WriteLine(String.Format("Scan is failed: {0}.", errorString))
    End Sub
    Private Sub VSTwain1_ScanCanceled()
        Console.WriteLine("Scan is canceled.")
    End Sub
    See Also