private void SetupPageSize(WIA.Item item, PageSize pageSize, ColorDepth colorDepth, Resolution resolution, Orientation orientation, bool setSize) { if (item == null) return; item.Properties["Horizontal Resolution"].set_Value(resolution.Value); item.Properties["Vertical Resolution"].set_Value(resolution.Value); item.Properties["Current Intent"].set_Value(colorDepth.Value); item.Properties["Bits Per Pixel"].set_Value(colorDepth.BitsPerPixel); double hExtent = item.Properties["Horizontal Extent"].SubTypeMax; double vExtent = item.Properties["Vertical Extent"].SubTypeMax; if (setSize) { if (orientation.Direction == 0) { item.Properties["Horizontal Extent"].set_Value(resolution.Value * pageSize.Width); item.Properties["Vertical Extent"].set_Value(resolution.Value * pageSize.Height); } else { item.Properties["Horizontal Extent"].set_Value(resolution.Value * pageSize.Height); item.Properties["Vertical Extent"].set_Value(resolution.Value * pageSize.Width); } } else { item.Properties["Horizontal Extent"].set_Value(hExtent); item.Properties["Vertical Extent"].set_Value(vExtent); } }
public Image Scan(DeviceInfo device, PageSize pageSize, ColorDepth colorDepth, Resolution resolution, Orientation orientation, bool setSize = true) { if (device == null) throw new ArgumentException("Device must be specified"); var scanner = device.Connect(); var wiaCommonDialog = new WPFCommonDialog(); var item = scanner.Items[1]; SetupPageSize(item, pageSize, colorDepth, resolution, orientation, setSize); var image = (ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatBMP, false); string fileName = Path.GetTempFileName(); File.Delete(fileName); image.SaveFile(fileName); image = null; // add file to output list return Image.FromFile(fileName); }