C# Code Examples
Real C# code examples covering the most common use cases of TwainScanning.NET. From basic scanning to advanced configuration.
Minimal Example Using Scanner UI
This will launch the scanner's user interface where you can select the desired settings. All scanned pages will be saved to separate JPEGs.
using (var dsm = new DataSourceManager(this)) // opens TWAIN
using (var ds = dsm.OpenSource()) // opens default source
{
var collector = ds.Acquire(true); // acquires images
collector.SaveAllToJpegs(@"C:\SomeFolder\Some\name.jpeg"); // saves images to disk
}
Scan from ADF and Save to PDF
Scan using predefined settings without showing the scanner interface. Open the default device, set A4 page size, 300 DPI resolution, and scan 20 pages from the ADF in black and white. All pages are saved to a single PDF document.
using (var dsm = new DataSourceManager(this)) // opens TWAIN
using (var ds = dsm.OpenSource()) // opens default source
{
ds.PageSize.Value = TwSS.A4; // sets page size to A4
ds.Resolution.Value = 300f; // sets 300 DPI
ds.ColorMode.Value = TwPixelType.BW; // sets Black and white format
ds.UseFeeder.Value = true; // scanner will use document feeder if possible
ds.UseDuplex.Value = true; // scanner will use duplex if possible
var collector = ds.Acquire(false, true, TwSX.Native, 20); // acquires images
collector.SaveAllToMultipagePdf(@"C:\SomeFolder\Some\name.pdf"); // saves images to disk
}
.SaveAllToMultipagePdf()
.SaveAllToJpegs()
.SaveAllToMultipageTiff()
.SaveAllToPngs()
.SaveAllToBmps()
Iterate Over Installed Scanners
List all installed scanning devices on the host. Get each scanner's name and allow users to select the default device for future scans.
using (var dsm = new DataSourceManager(this)) // opens TWAIN
{
// listing and write scanner names
List<TwIdentity> scannerIds = dsm.AvailableSources();
foreach (TwIdentity id in scannerIds)
Console.WriteLine(id.ProductName);
string defaultScannerName = dsm.DefaultSource().ProductName; // getting default source name
dsm.SelectDefaultSourceDlg(); // Setting the default scanner by default twain dialog
}
Get Scanner Capabilities
Open the default scanning device and retrieve its basic settings — available page sizes, resolutions, color modes, and more.
using (var dsm = new DataSourceManager(this)) // opens TWAIN
using (var ds = dsm.OpenSource()) // opens default source
{
TwSS[] pageSizes = ds.PageSize.AvailableValues;
float[] resolutions = ds.Resolution.AvailableValues;
TwPixelType[] colorModes = ds.ColorMode.AvailableValues;
bool[] adfEnabledStates = ds.UseFeeder.AvailableValues;
bool[] duplexEnabledStates = ds.UseDuplex.AvailableValues;
}
Advanced Scanner Settings
Retrieve detailed information about a scanner. More advanced settings are stored in the DataSource.Settings object, giving you access to power monitoring, image orientation, and much more.
using (var dsm = new DataSourceManager(this)) // opens TWAIN
using (var ds = dsm.OpenSource()) // opens default source
{
Console.Write("Battery percentage: ");
var capBattPerc = ds.Settings.PowerMonitoring.BatteryPercentage;
if (capBattPerc.IsSupportedOnThisDevice()) // scanner can provide battery info
{
TwBM2 perc = capBattPerc.Value;
if (perc == TwBM2.Infinite)
Console.WriteLine("INFINITE POWER!");
else if (perc == TwBM2.CannotReport)
Console.WriteLine("Don't know.");
else
Console.WriteLine("Battery at " + (short)perc + "%");
}
else
{
Console.WriteLine("Don't have battery!");
}
Console.Write("Orientation: ");
var capOrientation = ds.Settings.ImageAcquire.Orientation;
if (capOrientation.IsSupportedOnThisDevice())
{
TwOR orientation = capOrientation.Value;
Console.Write(" Current orientation is " + orientation);
Console.Write(" All possible orientations are:");
foreach(TwOR orient in capOrientation.SupportedValues)
Console.Write(" " + orient);
}
else
{
Console.WriteLine("Scanner doesn't support orientation");
}
}
Scan Asynchronously
Perform scanning in the background while keeping your application responsive. Use async callbacks to get notified when scanning completes, allowing users to continue working in the app.
public partial class ScanningForm : Form
{
DataSourceManager dsm = null;
DataSource ds = null;
public ScanningForm()
{
dsm = new DataSourceManager(this);
InitializeComponent();
}
private void buttonScann_Click(object sender, EventArgs e)
{
dsm.SelectDefaultSourceDlg(); // sets default scanner
ds = dsm.OpenSource(); // opens default scanner
ds.AcquireAsync(onFinishedScanning, true, true, TwSX.Memory, -1);
}
private void onFinishedScanning(TwImgCollector collector)
{
collector.SaveAllToMultipageTiff(@"C:\someFolder\someFile.tiff"); // saving images
collector.Dispose(); // disposing collector
ds.Dispose(); // disposing scanner
ds = null;
}
}
Universal Compatibility
If it supports TWAIN, it works with TwainScanning.NET. We've tested with hundreds of scanners from all major manufacturers. One API for every device.
x64 and AnyCPU Support
Most scanner drivers are 32-bit, so standard examples work best with x86 builds. For x64 or AnyCPU projects, our Bridgex86 mode enables seamless integration without architecture constraints.
View x64/AnyCPU ExamplesYour App. Scanner-Ready. In 5 Minutes.
You've seen what TwainScanning.NET can do. Now experience it yourself. Download the full-featured trial and have scanning working in your app before your next coffee break.
// That's literally it. You're done.
using (var dsm = new DataSourceManager(this))
using (var ds = dsm.OpenSource())
{
ds.Acquire(true)
.SaveAllToMultipagePdf("scan.pdf");
}