How to get Images From Scanner in C# with Windows Image Acqusition(WIA) Library
27-11-2015Getting images from Scanner in C#, we will use ShowTransfer() method of WIA.ICommonDialog class in Windows Image Acqusition(WIA) library
Full Code
WIA.ICommonDialog dialog = new WIA.CommonDialog(); WIA.Device device = dialog.ShowSelectDevice(WIA.WiaDeviceType.UnspecifiedDeviceType, true, false); var d = device.DeviceID; WIA.DeviceManager manager = new WIA.DeviceManager(); foreach (WIA.DeviceInfo info in manager.DeviceInfos) { if (info.DeviceID == d) { // connect to scanner device = WiaDeviceId.Connect(); break; } } //First item starts with 1 not 0 Item item = device.Items[1]; //gets image from scanner ImageFile image = (ImageFile) dialog.ShowTransfer(item, WIA.FormatID.wiaFormatTIFF, true); var imageBytes = (byte[])image.FileData.get_BinaryData(); var ms = new MemoryStream(imageBytes); Bitmap bitmap = (Bitmap)System.Drawing.Image.FromStream(ms); //tiff images can contain multiple images named as frame int count = bitmap.GetFrameCount(FrameDimension.Page); for (int idx = 0; idx < count; idx++) { // save each frame to a bytestream bitmap.SelectActiveFrame(FrameDimension.Page, idx); MemoryStream byteStream = new MemoryStream(); bitmap.Save(byteStream, ImageFormat.Tiff); // and then create a new Image from it System.Drawing.Image newImage = System.Drawing.Image.FromStream(byteStream); string filePath = System.IO.Path.GetTempPath() + "\\wiamnx" + DateTime.Now.ToString("yyyyMMddHHmmss.fff") + "." + image.FileExtension; newImage.Save(filePath); }
If you want to scan single side or multiple side of a page,simplex or dublex, you can use following lines of codes:
deviceManager = new WIA.DeviceManager(); if (!string.IsNullOrEmpty(this.WiaDeviceName)) { IEnumerable<WIA.DeviceInfo> devices = this.deviceManager.DeviceInfos.OfType<WIA.DeviceInfo>() .Where(x => x.DeviceID == (string) this.WiaDeviceName); if (devices != null && devices.Count() > 0) { this.WiaDeviceId = devices.FirstOrDefault(); } } var device = WiaDeviceId.Connect(); //Connects to selected Scanner device.Properties.get_Item("3088").set_Value(5); // Double scanning or dublex scanning device.Properties.get_Item("3088").set_Value(1);// Single scanning or simplex scanning
If you want to change DPI resolution, you can use following codes:
private const int WIA_IPS_DPI_HORIZONTAL = 6147; private const int WIA_IPS_DPI_VERTICAL = 6148; var dpi=500; Item item = device.Items[1]; //First item starts with 1, not 0 item.Properties.get_Item("6147").set_Value(dpi); // horizontal DPI item.Properties.get_Item("6148").set_Value(dpi); // vertical DPI
Note: Normally ShowTransfer() method scan all pages inserted in the scanner. If you want to change this default value, you can use 3096 property as follows:
private const int WIA_IPS_PAGES = 3096; var device = WiaDeviceId.Connect(); foreach (Property p in device.Properties) { switch (p.PropertyID) { case WIA_IPS_PAGES: // 0 means fast scan, 1 means single page scan, 2 means double scan, dublex . //And also set above 3088 property as 5 p.set_Value(0); break; } }