TwainPRO 9 for .NET - User Guide > How To > Acquire Images and Image Information > Transfer & Save Images |
Once a scanning session has been initiated by calling StartSession, one or more images can be scanned by the Data Source, transferred from the Data Source to the application, and saved by the application to file or memory.
Prior to initiating a transfer of a scanned image from the Data Source to the application, TwainPRO™ raises a Scanning event. The PendingTransfers property will contain the number of images the Data Source has ready for transfer. If the Data Source doesn't know how many images are ready for transfer, PendingTransfers will be equal to -1. TWAIN Data Sources with Automatic Document Feeders (ADF) will return PendingTransfers equal to -1. Such devices will transfer images until the feeder is empty.
Upon proceeding from the Scanning event, pending images will be transferred from the Data Source to the application.
When the transfer of each image is complete, TwainPRO raises a Scanned event. This event is the trigger to save the image to file or memory.
During the Scanned event, the ScannedImage property contains the latest scanned image. This information will be lost once the transfer of another image is initiated. Therefore, the application must select a method for saving the scanned image prior to initiating the next scan:
Upon exiting the Scanned event, the acquisition process continues, by triggering another Scanning event. Setting the Cancel parameter in the Scanning or Scanned events will terminate the scanning session.
C# Example |
Copy Code
|
---|---|
// This code demonstrates how to save to a multi-page file public void SaveMultipage() { mySaveOptions = new Accusoft.TwainProSdk.SaveOptions(); // Note also that PDF files can be saved as multipage files // and this could be done by setting mySaveOptions.Pdf.MultiPage to true // and setting mySaveOptions.Format to Pdf. mySaveOptions.Format = Accusoft.TwainProSdk.ScannedImageFormat.Tiff; mySaveOptions.Tiff.MultiPage = true; outputFile = System.IO.Path.Combine(System.Environment.CurrentDirectory, "..\\..\\multipage.tif"); twainDevice.StartSession(); } public void twainDevice_Scanned(object sender, Accusoft.TwainProSdk.ScannedEventArgs e) { try { e.ScannedImage.SaveFile(outputFile, mySaveOptions); } catch (Accusoft.TwainProSdk.TwainProException ex) { MessageBox.Show(ex.Message); } } |
C# Example |
Copy Code
|
---|---|
private void TransferData() { // This code demonstrates how to successfully negotiate the transfer mode and to transfer data in a compressed format twainDevice.TransferMode = Accusoft.TwainProSdk.TransferMode.TwsxMemory; twainDevice.OpenSession(); if (twainDevice.TransferMode == TransferMode.TwsxMemory) { AdvancedCapability capability = AdvancedCapability.IcapCompression; CapabilityContainer capcontainer = twainDevice.GetCapability(capability); // Only do the following if the Capability is supported and // there is more than one compression type to set. Otherwise, // the data source may say they support this capability but // might return an error if you try to set to TWCPNONE and // it is the only type. All data sources must support TWCPNONE. if (twainDevice.IsCapabilitySupported(capability) && comboBoxCompression.Items.Count > 1) { CapabilityContainerOneValueFloat myCap = new CapabilityContainerOneValueFloat(capability); myCap.Value = (float)AdvancedCapabilityCompression.TwcpJpeg; twainDevice.SetCapability(myCap); // Set the JPEG Quality factor AdvancedCapability capjpeg = AdvancedCapability.IcapJpegPixelType; CapabilityContainer capjpegcontainer = twainDevice.GetCapability(capjpeg); // Normally then you would negotiate the ICAP_JPEGPIXELTYPE // and ICAP_JPEGQUALITY capabilities. // Using the defaults for both of these values. } else { twainDevice.TransferMode = Accusoft.TwainProSdk.TransferMode.TwsxNative; labelStatus.Text = "TwainPRO forced to Native Transfer Mode. Compression unavailable."; } } else { twainDevice.TransferMode = Accusoft.TwainProSdk.TransferMode.TwsxMemory; labelStatus.Text = "TwainPRO forced to Native Transfer Mode. Compression unavailable."; } twainDevice.StartSession(); } // A simple typical Scanned event handler. public void twainDevice_Scanned(object sender, Accusoft.TwainProSdk.ScannedEventArgs e) { try { // Need to check the Compression type to see if we are transferring a DIB // or we are transferring compressed data. if (e.ScannedImage.ScannedImageData.Compression == ScannedImageCompression.TwcpNone) { int hImage = e.ScannedImage.ToHdib().ToInt32(); if (hImage != 0) { imageXView1.Image = Accusoft.ImagXpressSdk.ImageX.FromHdib(imagXpress1, (System.IntPtr)hImage); } } else { int hImage = e.ScannedImage.ToHbitmap().ToInt32(); if (hImage != 0) { imageXView1.Image = Accusoft.ImagXpressSdk.ImageX.FromHbitmap(imagXpress1, (System.IntPtr)hImage); GlobalFree(hImage); } } e.Cancel = false; } catch (Accusoft.ImagXpressSdk.ImagXpressException ex) { MessageBox.Show(ex.Message); } catch (Accusoft.TwainProSdk.TwainProException ex) { MessageBox.Show(ex.Message); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } } |