public List <Image> start() { List <Image> imageLost = new List <Image>(); WIA.CommonDialog dialog = new WIA.CommonDialog(); WIA.Device device = dialog.ShowSelectDevice(WIA.WiaDeviceType.ScannerDeviceType); WIA.Items items = dialog.ShowSelectItems(device); WIA.ImageFile image = null; dialog = new WIA.CommonDialog(); foreach (WIA.Item item in items) { while (true) { Console.WriteLine(device.Commands); try { if (stop == true) { image = null; stop = false; } else { image = (WIA.ImageFile)dialog.ShowTransfer(item, "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}", false); } if ((image != null) && (image.FileData != null)) { WIA.Vector vector = image.FileData; imageLost.Add(Image.FromStream(new MemoryStream((byte[])vector.get_BinaryData()))); } } catch (System.Runtime.InteropServices.COMException) { return(imageLost); } finally { if (image != null) { Marshal.FinalReleaseComObject(image); } } } } return(imageLost); }
private void scanButton_Click(object sender, RoutedEventArgs e) { //filePath = namefile(); filePath = System.IO.Path.Combine(System.IO.Path.GetTempPath(),"RESUME_SCANNER_image.tif"); System.IO.File.Delete(filePath); dialog = new WIA.CommonDialog(); try { scanner = dialog.ShowSelectDevice( WiaDeviceType.ScannerDeviceType, false, false); } catch (Exception msg) { //Uri uri = new Uri("Images/scanned.tif", UriKind.Relative); Uri uri = new Uri("Images/nnenna_u_resume.tif", UriKind.Relative); var stream = Application.GetResourceStream(uri).Stream; using (FileStream fileStream = System.IO.File.Create(filePath, (int)stream.Length)) { // Fill the bytes[] array with the stream data byte[] bytesInStream = new byte[stream.Length]; stream.Read(bytesInStream, 0, (int)bytesInStream.Length); // Use FileStream object to write to the specified file fileStream.Write(bytesInStream, 0, bytesInStream.Length); } } if (scanner!=null) { foreach (Property item in scanner.Items[1].Properties) { switch (item.PropertyID) { case 6146: //4 is Black-white,gray is 2, color 1 SetProperty(item, 4); break; case 6147: //dots per inch/horizontal SetProperty(item, 300); break; case 6148: //dots per inch/vertical SetProperty(item, 300); break; case 6149: //x point where to start scan SetProperty(item, 0); break; case 6150: //y-point where to start scan SetProperty(item, 0); break; case 3096: //Pages SetProperty(item, 1); break; case 3097: //PageSize SetProperty(item, 1); break; case 6151: //horizontal exent SetProperty(item, (int)(8.5 * 300)); break; case 6152: //vertical extent SetProperty(item, (int)(11 * 300)); break; case 4103: //DataType SetProperty(item, 0); break; case 4104: //BitsPerPixel SetProperty(item, 1); break; case 4110: //BitsPerChannel SetProperty(item, 1); break; case 4114: //NumberOfLines SetProperty(item, 3300); break; case 4116: //ItemSize SetProperty(item, 1056062); break; case 4113: //Bytes Per Line SetProperty(item, 320); break; } } dialog.ShowSelectItems(scanner); ImageFile image = null; try { image = dialog.ShowTransfer(scanner.Items[1], FormatID.wiaFormatTIFF, false) as ImageFile; } catch (Exception msg) { if (msg.Message.Contains("HRESULT")) MessageBox.Show("Please insert paper into scanner and try again."); return; } //System.IO.File.Delete(filePath); image.SaveFile(filePath); } BitmapImage _image = new BitmapImage(); _image.BeginInit(); _image.CacheOption = BitmapCacheOption.None; _image.UriCachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache); _image.CacheOption = BitmapCacheOption.OnLoad; _image.CreateOptions = BitmapCreateOptions.IgnoreImageCache; _image.UriSource = new Uri(filePath, UriKind.RelativeOrAbsolute); _image.EndInit(); scannedImageHolder.Source = _image; }