How to save acquired image as JPEG file
In This Topic
You can acquire image from scanner and save it as JPEG file by 2 ways:
1. Using Native or Memory transfer mode
- This method works with any scanners.
- Image is encoded using internal encoder of ActiveX.
- JPEG quality can be set using the JpegEncoder_JpegQuality property.
- Image is saved as JPEG file using the AcquiredImages_Save method.
- Example: Here is an example that shows how to acquire image and save image as JPEG file using JPEG encoder of the ActiveX.
Private VSTwain1 As New VintaSoftTwain()
Private imageCount As Integer
''' <summary>
''' Scans images asynchronously.
''' </summary>
Private Sub StartScan()
' open the device manager
If Not VSTwain1.DeviceManager_Open() Then
Console.WriteLine(VSTwain1.errorString)
Exit Sub
End If
' select device using standard device selection dialog
VSTwain1.DeviceManager_ShowDefaultDeviceSelectionDialog()
' open the device
If Not VSTwain1.Device_Open() Then
Console.WriteLine(VSTwain1.errorString)
Exit Sub
End If
' specify that device UI must be shown
VSTwain1.Device_ShowUI = True
' set settings of JPEG encoder
VSTwain1.JpegEncoder_JpegQuality = 70
' initialize the image count
imageCount = 1
' 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
VSTwain1.Device_AcquireImage()
End Sub
Private Sub VSTwain1_ImageAcquired()
' get index of acquired image
Dim imageIndex As Integer = VSTwain1.AcquiredImages_Count - 1
' increment the image count
imageCount = imageCount + 1
' save acquired image to TIFF file
If Not VSTwain1.AcquiredImages_Save(imageIndex, Str(imageCount) + ".jpg") Then
Console.WriteLine(VSTwain1.errorString)
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
2. Using File transfer mode
- TWAIN driver must have built-in JPEG encoder - you can check this with the Device_GetSupportedImageFileFormats method.
- TWAIN driver encodes acquired image and saves directly to disk.
- JPEG compression can be set using the Device_FileFormat property.
- Filename of JPEG file can be set using the Device_FileName property.
- Quality of JPEG file can be set using the Device_FileJpegQuality property.
- Example: Here is an example that shows how to save acquired image directly to disk as JPEG file.
Private VSTwain1 As New VintaSoftTwain()
Private imageCount As Integer
''' <summary>
''' Scans images asynchronously.
''' </summary>
Private Sub ScanImages()
' open the device manager
If Not VSTwain1.DeviceManager_Open() Then
Console.WriteLine(VSTwain1.errorString)
Exit Sub
End If
' open the device
If Not VSTwain1.Device_Open Then
Console.WriteLine(VSTwain1.errorString)
Exit Sub
End If
' scan images without UI
VSTwain1.Device_ShowUI = False
' specify that device must save acquired images directly to disk
VSTwain1.Device_TransferMode = TRANSFERMODE.TRANSFERMODE_File
' specify that acquired images must be save as JPEG files
VSTwain1.Device_FileFormat = TWAINIMAGEFILEFORMAT.TWAINIMAGEFILEFORMAT_Jpeg
' specify the quality of JPEG images
VSTwain1.Device_FileJpegQuality = 70
' initialize the image count
imageCount = 1
' specify the name of file where acquired image must be saved
VSTwain1.Device_FileName = Str(imageCount) + ".jpg"
' 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
VSTwain1.Device_AcquireImage()
End Sub
Private Sub VSTwain1_ImageAcquired()
' increment the image count
imageCount = imageCount + 1
' specify the name of file where acquired image must be saved
VSTwain1.Device_FileName = Str(imageCount) + ".jpg"
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