public Window1() { InitializeComponent(); Loaded += delegate { _twain = new Twain(new WpfWindowMessageHook(this)); _twain.TransferImage += delegate(Object sender, TransferImageEventArgs args) { if (args.Image != null) { resultImage = args.Image; IntPtr hbitmap = new Bitmap(args.Image).GetHbitmap(); MainImage.Source = Imaging.CreateBitmapSourceFromHBitmap( hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); Gdi32Native.DeleteObject(hbitmap); } }; _twain.ScanningComplete += delegate { IsEnabled = true; }; var sourceList = _twain.SourceNames; ManualSource.ItemsSource = sourceList; if (sourceList != null && sourceList.Count > 0) { ManualSource.SelectedItem = sourceList[0]; } }; }
private void Sw_TransferTempImage(object sender, TransferImageEventArgs e) { try { Dispatcher.Invoke(() => { IntPtr hbitmap = new Bitmap(e.Image).GetHbitmap(); MainImage.Source = Imaging.CreateBitmapSourceFromHBitmap( hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromRotation(Rotation.Rotate270)); Gdi32Native.DeleteObject(hbitmap); }); } catch (Exception ex) { Console.WriteLine(ex); } }
private void _twain_TransferImage(object sender, TransferImageEventArgs e) { //IsEnabled = true; if (e.Image != null) { resultImage = e.Image; string savePath = @"C:\Users\Tenny\Pictures\TwainTest\testBufferPic_"; savePath += imageCount.ToString() + @".bmp"; resultImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Bmp); fileName.Content = savePath; IntPtr hbitmap = new Bitmap(e.Image).GetHbitmap(); MainImage.Source = Imaging.CreateBitmapSourceFromHBitmap( hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); Gdi32Native.DeleteObject(hbitmap); imageCount++; } }
private void Sw_TransferCompleteImage(object sender, TransferImageEventArgs e) { try { Dispatcher.Invoke(() => { IntPtr hbitmap = new Bitmap(e.Image).GetHbitmap(); MainImage.Source = Imaging.CreateBitmapSourceFromHBitmap( hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromRotation(Rotation.Rotate270)); Gdi32Native.DeleteObject(hbitmap); fileName.Content = "complete!"; progressBar.Visibility = Visibility.Hidden; }); } catch (Exception ex) { Console.WriteLine(ex); } }
public DigitalizarDocumento(Window hook) { try { this.hook = hook; PDFViewer = PopUpsViewModels.MainWindow.DigitalizarDocumentos.pdfViewer; document = new PdfDocument(); limiteHojas = Parametro.LIMITES_HOJAS_ESCANER; EscaneoCompletado += delegate { }; _twain = new Twain(new WpfWindowMessageHook(this.hook)); _twain.TransferImage += delegate(Object sender, TransferImageEventArgs args) { //obtenemos el limite de hojas a escanear, en caso de haber sobrepasado utilizamos un return if (limiteHojas < (pagecount + 1)) { return; } var resultImage = args.Image; IntPtr hbitmap = new Bitmap(args.Image).GetHbitmap(); var imagen = Imaging.CreateBitmapSourceFromHBitmap( hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); document.Pages.Add(new PdfPage()); var xgr = XGraphics.FromPdfPage(document.Pages[pagecount]); var Ximg = XImage.FromBitmapSource(imagen); xgr.DrawImage(Ximg, 0, 0, 595, 842); pagecount++; Gdi32Native.DeleteObject(hbitmap); }; _twain.ScanningComplete += delegate(object sender, ScanningCompleteEventArgs e) { var s = e.Exception; StaticSourcesViewModel.CloseMensajeProgreso(); if (pagecount == 0) { StaticSourcesViewModel.Mensaje("Digitalización", "*- Hay problemas para conectarse con el escáner\n*- No tiene papel en el escáner.", StaticSourcesViewModel.enumTipoMensaje.MENSAJE_INFORMACION); return; } else { pagecount = 0; } fileNamepdf = Path.GetTempPath() + Path.GetRandomFileName().Split('.')[0] + ".pdf"; document.Save(fileNamepdf); ScannedDocument = File.ReadAllBytes(fileNamepdf); Application.Current.Dispatcher.Invoke((System.Action)(delegate { obj.LoadFile(fileNamepdf); obj.Visibility = Visibility.Visible; })); EscaneoCompletado(this, null); }; } catch (TwainException) { } }