VB.NET: Twain Scan for Document & Images

How to Twain scan for document image using VB.NET
Yiigo provides Twain scanning support included in Yiigo.Image for .NET, which can be easily added to your document imaging applications with fully-automated, dynamic, and feature-packed Twain scanning and capturing functionalities. Find more Twain scanning information on Yiigo.Imaging for .NET Twain Scanning Plugin.
Yiigo.Imaging for .NET Twain Scanning Plugin has those following functionalities:
  • Support for multi-page and single page scanning
  • Scan all documents & images in black and white, halftone, grayscale, palette or full color
  • Scan both front and back of page with duplex scanning
  • Easy to control Resolution, BitDepth, Brightness, Contrast, Unit, Size, Color-depth and other properties
  • Upload images to HTTP and FTP servers
For VB.NET developers, Yiigo provides online tutorials scan all the documents and images using Yiigo.Image for .NET and .NET Twain Scanner Plugin in details. Find more Twain scanning tutorials on Twain Scanning Support for Visual C#.NET, Web Imaging Viewer Twain Scanning Support and Twain Scanning in .NET Winforms.
Twain Scanning with VB.NET Sample Code

Requirements

Before using Twain scanning plugin in VB.NET, make sure that you have installed.

Start to Twain Scan

  • Start your Microsoft Visual Studio;
  • Choose "Project" in "File-New" from the menu;
  • In "New Project" dialog box, choose "Visual Basic Projects";
  • Choose "Windows Application" in the "Templates" List and name it "YiigoImagingTwainScanVBNET";
  • In the "Solution Explorer" window from the context menu, right-click on the "References" folder, and select "Add Reference...";
  • In the "Add Reference" dialog box, select the ".NET" tab and browse to Yiigo.Imaging.Net.dll library and Yiigo.Imaging.Net.Twain.dll ;
  • Add it to your VB.NET applications;
  • Import Yiigo .NET Image Namespace & copy those following codes:
Imports System.IO
Imports System.Drawing.Printing
Imports Yiigo.Imaging
Imports Yiigo.Imaging.Processing
Imports Yiigo.Imaging.TwainScan

' Initialize Twain scan
Private Sub Scanning()
Dim acq As New AcquireImage()
AddHandler acq.ImageAcquireImaged, New ImageAcquireImagedEventHandler(AddressOf(acq_ImageAcquireImaged))
Dim device As Device = acq.Devices.[Default]
If device.TryOpen() Then
device.ModalAcquireImage = True
device.AcquireImage()
device.Close()
End If
End Sub

Private Sub acq_ImageAcquireImaged(sender As Object, e As AcquireImageEventArgs)
Dim myImage As Yiigo.Imaging = Yiigo.Imaging.FromBitmap(e.Image)
End Sub

' Acquire Size and Position
Me.device.Units = UnitType.Inches
If Me.device.Units <> UnitType.Inches Then
Return
End If
Me.device.Frame = New System.Drawing.RectangleF(0, 0, 8.5, 11)

' Scan a multi page document into a PDF or TIFF file
Public Class AcquireImageClass
Private _acquireCanceled As Boolean
Private myAcquireImage As New AcquireImage(Me)
Public Sub ScanImages()
_acquireCanceled = False
myAcquireImage.AcquireImageCanceled += New EventHandler(OnAcquireImageCanceled)
myAcquireImage.AcquireImageFinished += New EventHandler(OnAcquireImageFinished)
myAcquireImage.ImageAcquireImaged += New ImageAcquireImagedEventHandler(OnImageAcquireImaged)
Dim activeDevice As Device = myAcquireImage.ShowSelectSource()
activeDevice.AcquireImage()
End Sub


Private Sub OnImageAcquireImaged(sender As Object, e As AcquireImageEventArgs)
If e.Image IsNot Nothing Then
Dim enc As New TiffEncoder(TiffCompression.[Default], True)
Dim fs As New FileStream("outputTiff.tif", FileMode.OpenOrCreate, FileAccess.ReadWrite)
enc.Save(fs, Yiigo.Imaging.FromBitmap(e.Image), Nothing)
fs.Close()
End If
End Sub


Private Sub OnAcquireImageCanceled(sender As Object, e As EventArgs)
_acquireCanceled = True
End Sub


Private Sub OnAcquireImageFinished(sender As Object, e As EventArgs)
If _acquireCanceled Then
Return
End If
Dim col As New PdfImageCollection()
Dim dec As New TiffDecoder()
Dim fs As New FileStream("outputTiff.tif", FileMode.Open, FileAccess.Read)
Dim frameCount As Integer = dec.GetFrameCount(fs)
fs.Close()
Dim i As Integer = 0
While i < frameCount
col.Add(New PdfImage("outputTiff.tif", i, PdfCompressionType.Auto))
System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While

Dim outStream As New FileStream("outputPdf.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite)
Dim enc As New PdfEncoder()
enc.Save(outStream, col, Nothing)
End Sub

End Class

' Upload Image to Sever
Private Sub UploadImage(image As System.Drawing.Bitmap)
Dim post As New Yiigo.Twain.HttpPost()
post.FormData.Add("image1", image, "filename.png")
post.FormData.Add("username", user)
post.FormData.Add("password", password)
Dim ret As String = post.PostData("http://www.website.com/postImage.aspx")
End Sub
Products
Other Plugins
Image Viewer CoreBarcode PluginPDF Read & WriteTesseract OCR PluginForm Processing PluginJBIG2 CodecJPEG2000 CodecISIS ScannerTwain ScannerDICOM ReaderCAD Scanner