Visual C#.NET: Twain Scan for Document & Images

How to Twain scan for document image using C#.NET
Yiigo.Image for .NET provides advanced Twain scanning support, allowing developers to capture digital images documents and images in bitonal, color and grayscale from scanners, cameras, and other TWAIN devices. Find more Twain scanning information on Yiigo.Imaging for .NET Twain Scanning Plugin.
Yiigo.Imaging for .NET Twain Scanning Plugin has those following functionalities:
  • Acquire images & documents from DIB, files, FTP or HTTP server
  • Easy to get scanned images & documents resolution in dpi, position, size, and orientation
  • Scan images with multiple color: grayscale, palette or full color
  • Scan image & documents with barcode, forms, OCR embedded
  • Support batch scanning & duplex scanning
  • Upload scanned results to web sever
This document provides comprehensive Visual C#.NET samples for guiding developers to scan all the documents and images using Yiigo.Image for .NET and .NET Twain scanner. Find more Twain scanning tutorials on VB.NET Twain Scanning, Web Imaging Viewer Twain Scanning Support and Twain Scanning in .NET Winforms.
Twain Scanning with C#.NET Sample Code


Before scanning document and image with Twain scanner in Visual C#.NET, make sure that you have installed

Start to Twain Scan

  • Open and run your Microsoft Visual Studio;
  • Choose either "Visual C# Projects" in "New Project" dialog box;
  • Choose "Windows Application" in the "Templates" List and name it "YiigoImagingTwainScanCsharp";
  • Right-click on the "References" folder, and select "Add Reference..." from the context menu in the "Solution Explorer" window;
  • 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 Visual C# applications;
  • Call Yiigo .NET Image Namespace & Copy those following codes:
using System.IO;
using System.Drawing.Printing;
using Yiigo.Imaging;
using Yiigo.Imaging.Processing;
using Yiigo.Imaging.TwainScan;

// Initialize Twain scan
private void Scanning()
AcquireImage acq = new AcquireImage();
acq.ImageAcquireImaged += new ImageAcquireImagedEventHandler(acq_ImageAcquireImaged);
Device device = acq.Devices.Default;
if (device.TryOpen())
device.ModalAcquireImage = true;
void acq_ImageAcquireImaged(object sender, AcquireImageEventArgs e)
Yiigo.Imaging myImage = Yiigo.Imaging.FromBitmap(e.Image);
// Acquire Size and Position
this.device.Units = UnitType.Inches;
if (this.device.Units != UnitType.Inches) return;
this.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 bool _acquireCanceled;
private AcquireImage myAcquireImage = new AcquireImage(this);
public void ScanImages()
_acquireCanceled = false;
myAcquireImage.AcquireImageCanceled += new EventHandler(OnAcquireImageCanceled);
myAcquireImage.AcquireImageFinished += new EventHandler(OnAcquireImageFinished);
myAcquireImage.ImageAcquireImaged += new ImageAcquireImagedEventHandler(OnImageAcquireImaged);
Device activeDevice = myAcquireImage.ShowSelectSource();
private void OnImageAcquireImaged(object sender, AcquireImageEventArgs e)
if (e.Image != null)
TiffEncoder enc = new TiffEncoder(TiffCompression.Default, true);
FileStream fs = new FileStream("outputTiff.tif",FileMode.OpenOrCreate, FileAccess.ReadWrite);
enc.Save(fs, Yiigo.Imaging.FromBitmap(e.Image), null);
private void OnAcquireImageCanceled(object sender, EventArgs e)
_acquireCanceled = true;
private void OnAcquireImageFinished(object sender, EventArgs e)
if (_acquireCanceled)
PdfImageCollection col = new PdfImageCollection();
TiffDecoder dec = new TiffDecoder();
FileStream fs = new FileStream("outputTiff.tif", FileMode.Open, FileAccess.Read);
int frameCount = dec.GetFrameCount(fs);
for(int i=0; i< frameCount; i++)
col.Add(new PdfImage("outputTiff.tif", i, PdfCompressionType.Auto));

FileStream outStream = new FileStream("outputPdf.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
PdfEncoder enc = new PdfEncoder();
enc.Save(outStream, col, null);
// Upload Image to Sever
private void UploadImage(System.Drawing.Bitmap image)
Yiigo.Twain.HttpPost post = new Yiigo.Twain.HttpPost(); post.FormData.Add("image1", image,
post.FormData.Add("username", user);
post.FormData.Add("password", password);
string ret = post.PostData("");
Other Plugins
Image Viewer CoreBarcode PluginPDF Read & WriteTesseract OCR PluginForm Processing PluginJBIG2 CodecJPEG2000 CodecISIS ScannerTwain ScannerDICOM ReaderCAD Scanner