We have an example project for VB6 that shows how to display and edit multipage TIFF files. Download the project code.
The demo uses the trial version of csXImage, which must must be registered on the development computer and the licence file must be in the same folder as the .ocx control. Running our installer will have performed the registration and unpacking of files.
When the VB form is loaded, and event runs to populate the combo boxes to request the number of pages to scan. This makes it easier to follow than setting the values in the VB IDE.
Here is a screenshot of the application after the form has loaded. It is simply a csXImage control in a form with some buttons to activate the different functions. These are enables and disabled as appropriate.
The csXImage control can only hold one image for display at any time so the method used to view multiple images is to store them in an array of variables of type Variant. This array, as well as the total number of images and the current image, is declared globally.
Private Pages() As Variant
Private ImageCount As Integer
Private CurrentImage As Integer
The code used to load the image contains several features worth noting. First, the image is selected by the user using the LoadDialog method. As only one page can be read by csXImage at any time, the first page is read by LoadDialog by setting ReadImageNumber to one. Then this first image is stored in the Pages array by exporting it from csXImage using WriteBinary and it is exported in bitmap format. Any format could have been used but bitmap has been chosen because it requires less processing than the compressed types, although it does require more memory.
The file selected by LoadDialog can be found by reading the LastFileName property. This is used to find the total number of pages in the image, using ImageCount. Now a loop can be formed to read each page in turn and write it to the Pages array variable. After completion of the loop, the first image is loaded into csXImage from the array, using the ReadBinary2 method.
Finally some buttons are enabled if there is at least 1 page and some more are enabled if there are multiple pages.
Note: Older versions of csXImage use the command ReadBinary which took an extra parameter to define the file format, although this parameter had become redundant as images were read regardles of the format. The command ReadBinary2 has been introduced for simplification.
Private Sub cmdLoad_Click()
Dim i As Integer
ImageBox1.ReadImageNumber = 1
ImageBox1.LoadDialog
ReDim Pages(1)
Pages(1) = ImageBox1.WriteBinary(gfBMP)
ImageCount = ImageBox1.ImageCount(ImageBox1.LastFileName)
If ImageCount > 1 Then
ReDim Preserve Pages(ImageCount)
For i = 2 To ImageCount
ImageBox1.ReadImageNumber = i
ImageBox1.LoadFromFile (ImageBox1.LastFileName)
Pages(i) = ImageBox1.WriteBinary(gfBMP)
lblPageNumText.Caption = "Loading - Page " & i & " of " & ImageCount
Me.Refresh
Next i
End If
CurrentImage = 1
ImageBox1.ReadBinary2 Pages(CurrentImage)
lblPageNumText.Caption = "Page " & CurrentImage & " of " & ImageCount
If ImageCount > 0 Then
cmdSave.Enabled = True
cmdRotate90.Enabled = True
cmdRotate180.Enabled = True
cmdRotate270.Enabled = True
cmdCrop.Enabled = True
cmdDespeckle.Enabled = True
If ImageCount > 1 Then
cmdPrev.Enabled = True
cmdNext.Enabled = True
Else
cmdPrev.Enabled = False
cmdNext.Enabled = False
End If
End If
End Sub
Stepping forwards and backwards through the image is relatively simple. The new page is read from the Pages array using ReadBinary2 and the CurrentImage variable is updated.
Private Sub cmdNext_Click()
If CurrentImage < ImageCount Then
CurrentImage = CurrentImage + 1
ImageBox1.ReadBinary2 Pages(CurrentImage)
lblPageNumText.Caption = "Page " & CurrentImage & " of " & ImageCount
End If
End Sub
Private Sub cmdPrev_Click()
If CurrentImage > 1 Then
CurrentImage = CurrentImage - 1
ImageBox1.ReadBinary2 Pages(CurrentImage)
lblPageNumText.Caption = "Page " & CurrentImage & " of " & ImageCount
End If
End Sub
We will leave the description of the scanning code because that is covered in the Twain Demo and the ADF Demo. The only significant difference is that in this example each scanned page is stored in the array variable.
To save the image, each page must be added to a multipage TIFF in the memory of csXImage using the AddToTIF method. Each image is loaded into csXImage in turn and AddToTIF is called. Any settings that apply to each page, such as Compression and ColorFormat must be set before calling AddToTIF. The WriteTIFDialog method is used so that the user can specify the location and name of the saved file. Finally the current image is loaded back into the control.
Private Sub cmdSave_Click()
Dim i As Integer
For i = 1 To ImageCount
ImageBox1.ReadBinary2 Pages(i)
lblPageNumText.Caption = "Saving - Page " & CurrentImage & " of " & ImageCount
If ImageBox1.ColorFormat = cfMonoBW Then
ImageBox1.Compression = cmGroup4
Else
ImageBox1.Compression = cmNone
End If
ImageBox1.AddToTIF 0
Next i
ImageBox1.WriteTIFDialog
ImageBox1.ReadBinary2 Pages(CurrentImage)
lblPageNumText.Caption = "Page " & CurrentImage & " of " & ImageCount
End Sub
© Chestysoft, 2024.
This site uses cookies for functionality, traffic analysis and for targeted advertising. Click the Accept button to accept our Cookie Policy. The Cookie Policy page offers configuration for a reduced set of cookies for this site.