In This Topic
Programming / TIFF / Creating a multipage TIFF file from a document in the scanner using the TWAIN protocol

Creating a multipage TIFF file from a document in the scanner using the TWAIN protocol

In This Topic

In this tutorial, we will create a multipage TIFF file from scans of a document, that is in the scanner feeder. We will set the image bit depth to 1 bit per pixel and we will scan the images to black and white. For the communication with the scanner we will use the TWAIN protocol.

Copy Code
'We assume that GdPicture has been correctly installed and unlocked.
Dim oGdPictureImaging As New GdPictureImaging()
'Variable to hold image identifiers.
Dim imageId As Integer = 0
'Variable to hold created multipage image handle.
Dim multipageHandle As Integer = 0
'Number of images.
Dim imageCount As Integer = 0
'Setting up the Twain features.
oGdPictureImaging.TwainOpenDefaultSource(Me.Handle)
'Enabling AutoFeed option.
oGdPictureImaging.TwainSetAutoFeed(True)
'To achieve the maximum scanning rate.
oGdPictureImaging.TwainSetAutoScan(True)
'1 bit bw image
oGdPictureImaging.TwainSetPixelType(TwainPixelType.TWPT_BW)
'Setting duplex acquisition if supported.
oGdPictureImaging.TwainEnableDuplex(True)
'Asking the device to hide its GUI.
oGdPictureImaging.TwainSetHideUI(True)

'Scanning each page and adding it to a multipage TIFF GdPictureImage identifier.
Dim status As GdPictureStatus = GdPictureStatus.OK
Do
    'Acquiring an image from the scanner.
    imageId = oGdPictureImaging.TwainAcquireToGdPictureImage(Me.Handle)
    'Checking, if the image has been loaded successfully.
    status = oGdPictureImaging.GetStat()
    If status = GdPictureStatus.OK Then
        imageCount += 1
        'If it is the first image, then create a new multipage tiff file.
        If imageCount = 1 Then
            multipageHandle = imageId
            status = oGdPictureImaging.TiffSaveAsMultiPageFile(multipageHandle, "multipage.tif", TiffCompression.TiffCompressionAUTO)
        Else
            'If it is the second image or more, add it to the previously created multipage tiff file.
            status = oGdPictureImaging.TiffAddToMultiPageFile(multipageHandle, imageId)
            'Releasing the current image to minimize memory usage.
            oGdPictureImaging.ReleaseGdPictureImage(imageId)
        End If
    Else
        MessageBox.Show("The image can't be loaded. Error:" + status.ToString(), "Multipage TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
    If status <> GdPictureStatus.OK Then
        Exit Do
    End If
Loop While oGdPictureImaging.TwainGetState() > TwainStatus.TWAIN_SOURCE_ENABLED
If status = GdPictureStatus.OK Then
    MessageBox.Show("Done!", "Multipage TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
    MessageBox.Show("Error: " + status.ToString(), "Multipage TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
'Cleaning up resources.
'Closing the source
oGdPictureImaging.TwainCloseSource()
'Closing the multipage file.
oGdPictureImaging.TiffCloseMultiPageFile(multipageHandle)
'Releasing the multipage image.
oGdPictureImaging.ReleaseGdPictureImage(multipageHandle)
oGdPictureImaging.Dispose()
Copy Code
//We assume that GdPicture has been correctly installed and unlocked.
GdPictureImaging oGdPictureImaging = new GdPictureImaging();
//Variable to hold image identifiers.
int imageId = 0;
//Variable to hold created multipage image handle.
int multipageHandle = 0;
//Number of images.
int imageCount = 0;
//Setting up the Twain features.
oGdPictureImaging.TwainOpenDefaultSource(this.Handle);
oGdPictureImaging.TwainSetAutoFeed(true); //Enabling AutoFeed option.
oGdPictureImaging.TwainSetAutoScan(true); //To achieve the maximum scanning rate.
oGdPictureImaging.TwainSetPixelType(TwainPixelType.TWPT_BW); //1 bit bw image
oGdPictureImaging.TwainEnableDuplex(true); //Setting duplex acquisition if supported.
oGdPictureImaging.TwainSetHideUI(true); //Asking the device to hide its GUI.
GdPictureStatus status = GdPictureStatus.OK;
//Scanning each page and adding it to a multipage TIFF GdPictureImage identifier.
do
{
    //Acquiring an image from the scanner.
    imageId = oGdPictureImaging.TwainAcquireToGdPictureImage(this.Handle);
    //Checking, if the image has been loaded successfully.
    status = oGdPictureImaging.GetStat();
    if (status == GdPictureStatus.OK)
    {
        imageCount++;
        //If it is the first image, then create a new multipage tiff file.
        if (imageCount == 1)
        {
            multipageHandle = imageId;
            status = oGdPictureImaging.TiffSaveAsMultiPageFile(multipageHandle, "multipage.tif", TiffCompression.TiffCompressionAUTO);
        }
        else //If it is the second image or more, add it to the previously created multipage tiff file.
        {
            status = oGdPictureImaging.TiffAddToMultiPageFile(multipageHandle, imageId);
            //Releasing the current image to minimize memory usage.
            oGdPictureImaging.ReleaseGdPictureImage(imageId);
        }
    }
    else
        MessageBox.Show("The image can't be loaded. Error: " + status.ToString(), "Multipage TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
    if (status != GdPictureStatus.OK)
        break;
} while (oGdPictureImaging.TwainGetState() > TwainStatus.TWAIN_SOURCE_ENABLED);
if (status == GdPictureStatus.OK)
    MessageBox.Show("Done!", "Multipage TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
    MessageBox.Show("Error: " + status.ToString(), "Multipage TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
//Cleaning up resources.
oGdPictureImaging.TwainCloseSource(); //Closing the source.
oGdPictureImaging.TiffCloseMultiPageFile(multipageHandle); //Closing the multipage file.
oGdPictureImaging.ReleaseGdPictureImage(multipageHandle); //Releasing the multipage image.
oGdPictureImaging.Dispose();