using WIA;
// Create a DeviceManager instance var deviceManager = new DeviceManager(); // Loop through the list of devices for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++) { // Skip the device if it's not a scanner if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType) { continue; } // Print something like e.g "WIA Canoscan 4400F" Console.WriteLine(deviceManager.DeviceInfos[i].Properties["Name"].get_Value()); // e.g Canoscan 4400F //Console.WriteLine(deviceManager.DeviceInfos[i].Properties["Description"].get_Value()); // e.g \\.\Usbscan0 //Console.WriteLine(deviceManager.DeviceInfos[i].Properties["Port"].get_Value()); }
using WIA; using System.IO;
// Create a DeviceManager instance var deviceManager = new DeviceManager(); // Create an empty variable to store the scanner instance DeviceInfo firstScannerAvailable = null; // Loop through the list of devices to choose the first available for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++) { // Skip the device if it's not a scanner if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType) { continue; } firstScannerAvailable = deviceManager.DeviceInfos[i]; break; } // Connect to the first available scanner var device = firstScannerAvailable.Connect(); // Select the scanner var scannerItem = device.Items[1]; // Retrieve a image in JPEG format and store it into a variable var imageFile = (ImageFile)scannerItem.Transfer(FormatID.wiaFormatJPEG); // Save the image in some path with filename var path = @"C:\Users\<username>\Desktop\scan.jpeg"; if (File.Exists(path)) { File.Delete(path); } // Save image ! imageFile.SaveFile(path);
using WIA; using System.IO;
/// <summary> /// Adjusts the settings of the scanner with the providen parameters. /// </summary> /// <param name="scannnerItem">Scanner Item</param> /// <param name="scanResolutionDPI">Provide the DPI resolution that should be used e.g 150</param> /// <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 percent</param> /// <param name="colorMode">Set the color mode</param> private static void AdjustScannerSettings(IItem scannnerItem, int scanResolutionDPI, int scanStartLeftPixel, int scanStartTopPixel, int scanWidthPixels, int scanHeightPixels, int brightnessPercents, int contrastPercents, int colorMode) { 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_SCAN_RESOLUTION_DPI, scanResolutionDPI); SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_RESOLUTION_DPI, scanResolutionDPI); SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_START_PIXEL, scanStartLeftPixel); SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_START_PIXEL, scanStartTopPixel); SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_SIZE_PIXELS, scanWidthPixels); SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_SIZE_PIXELS, scanHeightPixels); SetWIAProperty(scannnerItem.Properties, WIA_SCAN_BRIGHTNESS_PERCENTS, brightnessPercents); SetWIAProperty(scannnerItem.Properties, WIA_SCAN_CONTRAST_PERCENTS, contrastPercents); SetWIAProperty(scannnerItem.Properties, WIA_SCAN_COLOR_MODE, colorMode); } /// <summary> /// Modify a WIA property /// </summary> /// <param name="properties"></param> /// <param name="propName"></param> /// <param name="propValue"></param> private static void SetWIAProperty(IProperties properties, object propName, object propValue) { Property prop = properties.get_Item(ref propName); prop.set_Value(ref propValue); }
// Create a DeviceManager instance var deviceManager = new DeviceManager(); // Create an empty variable to store the scanner instance DeviceInfo firstScannerAvailable = null; // Loop through the list of devices to choose the first available for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++) { // Skip the device if it's not a scanner if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType) { continue; } firstScannerAvailable = deviceManager.DeviceInfos[i]; break; } // Connect to the first available scanner var device = firstScannerAvailable.Connect(); // Select the scanner var scannerItem = device.Items[1]; /** * 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_pixel, height_pixel, 0, 0, color_mode); // Retrieve a image in JPEG format and store it into a variable var imageFile = (ImageFile)scannerItem.Transfer(FormatID.wiaFormatJPEG); // Save the image in some path with filename var path = @"C:\Users\<username>\Desktop\scan.jpeg"; if (File.Exists(path)) { File.Delete(path); } // Save image ! imageFile.SaveFile(path);
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(FormatID.wiaFormatJPEG); } catch (COMException e) { // Convert the error code to UINT uint errorCode = (uint)e.ErrorCode; // See the error codes if (errorCode == 0x80210006) { Console.WriteLine("The scanner is busy or isn't ready"); } else if(errorCode == 0x80210064) { Console.WriteLine("The scanning process has been cancelled."); } else if(errorCode == 0x8021000C) { Console.WriteLine("There is an incorrect setting on the WIA device."); } else if(errorCode == 0x80210005) { Console.WriteLine("The device is offline. Make sure the device is powered on and connected to the PC."); } else if(errorCode == 0x80210001) { Console.WriteLine("An unknown error has occurred with the WIA device."); } }
using WIA; using System.Runtime.InteropServices;
// Create a DeviceManager instance var deviceManager = new DeviceManager(); // Create an empty variable to store the scanner instance DeviceInfo firstScannerAvailable = null; // Loop through the list of devices to choose the first available for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++) { // Skip the device if it's not a scanner if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType) { continue; } firstScannerAvailable = deviceManager.DeviceInfos[i]; break; } // Connect to the first available scanner var device = firstScannerAvailable.Connect(); // Select the scanner var scannerItem = device.Items[1]; CommonDialogClass dlg = new CommonDialogClass(); try { object scanResult = dlg.ShowTransfer(scannerItem, WIA.FormatID.wiaFormatPNG, true); if (scanResult != null){ ImageFile image = (ImageFile)scanResult; // Do the rest of things as save the image } } catch (COMException e) { // Display the exception in the console. Console.WriteLine(e.ToString()); uint errorCode = (uint)e.ErrorCode; // Catch 2 of the most common exceptions if (errorCode == 0x80210006) { Console.WriteLine("The scanner is busy or isn't ready"); }else if(errorCode == 0x80210064) { Console.WriteLine("The scanning process has been cancelled."); } }