Creating A Scanning Application in Winforms With C# - Our Code World
Creating A Scanning Application in Winforms With C# - Our Code World
RELATED ARTICLES - C#
are going to use the WIA API. Windows Image Acquisition (WIA) sometimes
also called Windows Imaging Architecture) is a Microsoft driver model and
How to retrieve the RAM amount available on the
application programming interface (API) for Microsoft Windows 2000 and System in WinForms with C#
later operating systems that enables graphics software to communicate AUGUST 26TH 2019
C#
with imaging hardware such as scanners, digital cameras and Digital Video-
equipment.
How to retrieve (list) the titles and process id of all
the opened applications in the taskbar of Windows
WIA makes easy to application developers, device manufacturers, and with C# in WinForms
scanner users who need to interact with imaging hardware. The API set AUGUST 22ND 2019
C#
exposes imaging applications to still image acquisition hardware
functionality by providing support for:
In this article, you will learn how to manipulate an image scanner through
WIA with C#.
Requirements
Visual Studio >= 2007 (we are going to use Visual Studio Community)
An installed and functional scanner
Go to the solution explorer located in the top right corner of Visual Studio
and do right click on your project, then click on Add > Reference.
In the emergent window, select the COM option in the left menu and search
for the Microsoft Windows Image Acquisition Library v2.0 and click on OK.
Once you click ok, the reference will be added to your project. Now you
need to set the Embed Interop Types property of the WIA component to
False. With Visual Studio go to the Solution Explorer and select your project,
then in your project click on References from the tree view component and
search for WIA. Select the WIA reference and look for the Embed Interop
Types option in the Properties panel and set this value to False:
using WIA;
Then just iterate over the device manager to list the devices:
The Properties object has other properties like Id, Port, Manufacturer and
Type, visit the MSDN page about the WIA device info class for more
information.
using WIA;
using System.IO;
Note
The process to select the scanner and then use the rest of code to start the
scanning process can be a little tricky if you don't handle it properly. We
recommend you for example to add a List Box item to your form and
append a new item that displays the name and the DeviceInfos object too
(you will see an example at the end of the article).
// Create a DeviceManager instance
var deviceManager = new DeviceManager();
firstScannerAvailable = deviceManager.DeviceInfos[i];
break;
}
if (File.Exists(path))
{
File.Delete(path);
}
// Save image !
imageFile.SaveFile(path);
If you test it, the code will work and the scanner will start, however the
image will be incompleted on most of the scanners. That's because we
didn't set any of the common properties of the scanner, that you will learn
to set in the following step.
using WIA;
using System.IO;
Important
Remember that WIA has a lot of properties constants that you can modify
and that may be or not available on di erent scanning devices (e.g to
modify the horizontal resolution use the constant 6147), read the following
MSDN page for more information.
/// <summary>
/// Adjusts the settings of the scanner with the providen pa
/// </summary>
/// <param name="scannnerItem">Scanner Item</param>
/// <param name="scanResolutionDPI">Provide the DPI resolut
/// <param name="scanStartLeftPixel"></param>
/// <param name="scanStartTopPixel"></param>
/// <param name="scanWidthPixels"></param>
/// <param name="scanHeightPixels"></param>
/// <param name="brightnessPercents"></param>
/// <param name="contrastPercents">Modify the contrast perce
/// <param name="colorMode">Set the color mode</param>
private static void AdjustScannerSettings(IItem scannnerItem
{
const string WIA_SCAN_COLOR_MODE = "6146";
const string WIA_HORIZONTAL_SCAN_RESOLUTION_DPI = "6147
const string WIA_VERTICAL_SCAN_RESOLUTION_DPI = "6148";
const string WIA_HORIZONTAL_SCAN_START_PIXEL = "6149";
const string WIA_VERTICAL_SCAN_START_PIXEL = "6150";
const string WIA_HORIZONTAL_SCAN_SIZE_PIXELS = "6151";
const string WIA_VERTICAL_SCAN_SIZE_PIXELS = "6152";
const string WIA_SCAN_BRIGHTNESS_PERCENTS = "6154";
const string WIA_SCAN_CONTRAST_PERCENTS = "6155";
SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_S
SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCA
SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_S
SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCA
SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_S
SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCA
SetWIAProperty(scannnerItem.Properties, WIA_SCAN_BRIGHTN
SetWIAProperty(scannnerItem.Properties, WIA_SCAN_CONTRAS
SetWIAProperty(scannnerItem.Properties, WIA_SCAN_COLOR_M
}
/// <summary>
/// Modify a WIA property
/// </summary>
/// <param name="properties"></param>
/// <param name="propName"></param>
/// <param name="propValue"></param>
private static void SetWIAProperty(IProperties properties, o
{
Property prop = properties.get_Item(ref propName);
prop.set_Value(ref propValue);
}
It's worth to say again, that is up to you the customization of the properties,
but we provide you a simple way to customize them with the SetWIAProperty
method.
Finally, to start the scan correctly you just need to execute the
AdjustScannerSettings method before the initialization of the scanner
(mixing the step 3 and 4):
// Create a DeviceManager instance
var deviceManager = new DeviceManager();
firstScannerAvailable = deviceManager.DeviceInfos[i];
break;
}
/**
* Set the scanner settings
*/
int resolution = 150;
int width_pixel = 1250;
int height_pixel = 1700;
int color_mode = 1;
AdjustScannerSettings(scannerItem, resolution, 0, 0, width_
if (File.Exists(path))
{
File.Delete(path);
}
// Save image !
imageFile.SaveFile(path);
5. Catching exceptions
WIA functions methods can throw exceptions that can be identi ed through
error codes. The list of error codes can be found in the documentation of
WIA in the MSDN website here. To handle the errors of WIA, catch the
COMException object. Remember to import previously the InteropServices
type:
using System.Runtime.InteropServices;
And then wrap the code that uses WIA inside a try-catch statement. You can
identify the error with the ErrorCode property of the exception, but
remember to convert it to its uint representation to be able to compare it
with the error codes of the table in MSDN.
try
{
// Some code that uses WIA
// e.g
//
// var device = firstScannerAvailable.Connect();
// var scannerItem = device.Items[1];
// var imageFile = (ImageFile)scannerItem.Transfer(Forma
}
catch (COMException e)
{
// Convert the error code to UINT
uint errorCode = (uint)e.ErrorCode;
using WIA;
using System.Runtime.InteropServices;
firstScannerAvailable = deviceManager.DeviceInfos[i];
break;
}
try
{
object scanResult = dlg.ShowTransfer(scannerItem, WIA.Fo
if (scanResult != null){
ImageFile image = (ImageFile)scanResult;
With all these basic instructions, you will be able to create your own
scanning application in WinForms with C#.
Implementation example
We've just written an example application that lists all the available scanner
devices in a list box and allow you to scan and save the le in a custom
path. It implements the scanning progress dialog and allows you to save the
image in di erent formats as PNG,JPEG or TIFF:
Just clone the repository, open the project with Visual studio and test it. The
source code can be found in this Github repository.
Happy coding !
WhatsApp
Our Code World is a free blog about programming, where you will nd Privacy Policy About
solutions to simple and complex tasks of your daily life as a developer. Comments Policy Advertise with us
Authors
Contact
Write for us
ADVERTISING
Carlos Delgado
Our Comments Section is open to every developer, so
you can contribute (even code) to the main idea of the
Article.
Please read our Comment Policy before commenting.
1
LOG IN WITH
Name
AdjustScannerSettings(scannerItem, resolution, 0, 0,
width_pixel, height_pixel, 100, 5, color_mode);
△ ▽ • Reply • Share ›
Greetings.
△ ▽ 1 • Reply • Share ›