This project has moved. For the latest updates, please go here.

Saraff.Twain.Website index2.htm Web Sample

Mar 11, 2016 at 12:42 AM
Edited Mar 11, 2016 at 12:43 AM

After you click "Acquire" on index2.htm the scan process is started (I know it because my scanner starts to work), after that, a message is displayed, for example "Uploaded images: tmpD2C8.jpg". My question is, how can I access the scanned file? I'd like to know how to get the bytes from the scanned document so I can upload it to a database or display it directly to the browser, I just need to know how to get those bytes.

Thanks in advance!!! (Y)
Mar 11, 2016 at 2:06 AM
Edited Mar 11, 2016 at 4:57 PM
Ok, I got it after some trial and error, once I click "Acquire" these functions are fired at TwainHandler.ashx:
private string _Create(string ext) {
            return Path.GetFileName(Path.ChangeExtension(Path.GetTempFileName(),ext));

        private void _Append(string name,byte[] data,int offset,int count) {
            using(var _stream=File.Open(Path.Combine(HostingEnvironment.MapPath("~/App_Data"), name), FileMode.Append)) {
                _stream.Write(data, offset, count);
Nos my question is, where in index2.htm is this called? Or is it called from the "dll" files?

Thank you :D
Mar 11, 2016 at 5:41 PM
Hello, smtrejo.
See line 43 of index2.htm.
AxHost.AddComponentParameter(AxHost.WorkingDirectory + "TwainHandler.ashx")
and line 143 of ScanComponent.cs
this._helper.Upload(_stream, ".jpg", out _name);
Marked as answer by SARAFF on 3/16/2016 at 11:44 AM
Mar 15, 2016 at 1:32 AM
Edited Mar 15, 2016 at 1:35 AM

Based on the web sample (index2.htm), where can I call the "AcquireCompleted" event?
private void _twain_AcquireCompleted(object sender,EventArgs e) {
        try {
            if(this._twain.ImageCount>0) {
        } catch(Exception ex) {
Thank you!!!
Mar 15, 2016 at 7:25 PM
Hello, smtrejo.
I do not understand essence of a question. Why call the "AcquireCompleted" event?
Mar 15, 2016 at 8:18 PM
Edited Mar 15, 2016 at 8:23 PM
Hello again SARAFF,

I've found a way to achieve my requirement so I will explain the solution I'm working on if it can be helpful to someone else.

I've integrated the ashx handler to a VB.Net Web Application (aspx). I want to scan multiple pages, and merge them on a single pdf document when all the images are aquired. So, after some research I'm going to do the following:
  1. Scan and upload all the images to server side in jpg format to a folder associated to the user's session ID, which is what your web example does already in index2.htm. I've added the IRequiresSessionState in the handler declaration to gain access to session state via HttpContext.Current.Session.SessionID:
public class TwainHandler : IHttpHandler, IRequiresSessionState
  1. Each time a document is scanned, the page will refresh and display the scanned documents as thumbnails. The refresh will be done firing an Ajax doPostBack on the Uploaded() function from your Javascript code, so it will update an Update Panel.
  2. When the user has scanned all his documents, he will click a button to get all the uploaded images on his session ID folder and merge them into a sigle PDF file using iTextSharp.
How to merge .pdf and .jpg file in one pdf

I know you have implemented a custom control for that, but is quite difficult for me to adapt it to my needs.

Best regards.