C #: Scanner continuous scanning with TWAIN protocol

Source: Internet
Author: User

What if I scan multiple images at once using the TWAIN protocol? It is believed that a friend who has contacted the TWAIN protocol to communicate with the scanner has experienced this problem. According to the example on the net, in the continuous paper-feeding scanner, the call can only scan a picture, how to break?

I studied this problem for a long time, finally in the troubled me 7 days later, I took care of, the following share my experience of the solution.

Create a new Scancommon class, in the call interface, write down the method that calls the scan:

1Scancommon scan =NewRs_scancommon (FileName, This. Handle);2Scan. Passdatabetweenform + =NewRs_scancommon.passdatabetweenformhandler (scan_passdatabetweenform);3                  4     //whether continuous scanning5Scan.bcontinuousscan =true;6Scan.dtrow =Dtrow;7      This. Enabled =false;8Scan. Startscan ();

In the Scancommon class, the interface class that calls TWAIN (can be found on the TWAIN class network, this article is slightly), the main code is as follows:

       //Add a delegate         Public Delegate voidPassdatabetweenformhandler (Objectsender, Passdataeventargs e); //Add an event of type Passdatabetweenformhandler         Public EventPassdatabetweenformhandler Passdatabetweenform;  PublicScancommon (stringFileName, IntPtr Handle) {             This. FileName =FileName;  This. Handle =Handle; //Scan Initialization (the default is to use TWAIN mode to connect)TW =NewTwain (); tw. Init (Handle, outRC); //tw. Select ();        }       //Start Scan        Public voidStartscan () {if(RC! =twrc.success) {MessageBox.Show ("Device initialization failed, please check the hardware and driver! "); }            Else            {                               stringPath ="XXX Path"+"\\ScannerFile\\"+Frmname; if(scanning (path) = =false) {MessageBox.Show ("The scan task cannot be created automatically because the device is not found or the device does not support the TWAIN component. ") ; //Callback main WindowPassdataeventargs args =NewPassdataeventargs ("Exit"); Passdatabetweenform ( This, args); Gc.                Collect (); }            }        }                                  
View Code

In the Scancommon class, the main scanning methods can be implemented by calling the Twain class, and scanning is the method that invokes the driver to implement the scan. Application.addmessagefilter is an event listener that adds a scanner, returning a different state of the scanner in a listener event to implement some actions for cancellation and scanning.

The main code is as follows:

       Some methods of #region scanning private bool Scanning (string FilePath) {if (!msgfilter)                {Msgfilter = true;            Application.addmessagefilter (this);            } Scanpath = FilePath; return TW.        Acquire (ref strscanerrormessage, Bcontinuousscan); }///<summary>//Save picture///</summary>//<param name= "DIBHANDP" ></para            m> private void Imagesave (IntPtr dibhandp) {bmprect = new Rectangle (0, 0, 0, 0);            if (DIBHANDP! = IntPtr.Zero) Dibhand = DIBHANDP;            Bmpptr = GlobalLock (Dibhand);        Pixptr = Getpixelinfo (bmpptr); public void Endingscan () {if (msgfilter) {tw.                Finish ();                Removemessagefilter (this);            Msgfilter = false;     }} public static void Removemessagefilter (IMessageFilter value)   {Application.removemessagefilter (value); } #endregion

In the Twaincommand.transferready state of the listener, the scanner scanned the picture. I started by trying to modify the Transferpictures method in the Twain class, but the results proved to be not OK.

In fact, this is a misunderstanding, we only need to modify the TWAIN Start scan event, can be based on whether continuous scanning to the scanner to pass different parameters to achieve this effect.

With a single line of code, you can scan multiple times at once without changing any of the scanner's settings.

If the device supports continuous feed, the scanner will scan directly until the paper is completely swept

  This line of code is a continuous scan
Twcapability cap = new Twcapability (Twcap.xfercount,-1);

The code is as follows:

        <summary>///Start scan///</summary>//<param name= "Strscanerrormessage" ;</param>//<returns></returns> public bool Acquire (ref string Strscanerrormessa                GE, bool Multiscan) {try {TWRC rc;                CLOSESRC (); if (AppID.                    Id = = IntPtr.Zero) {Init (hwnd, out RC); if (AppID. Id = = IntPtr.Zero) Strscanerrormessage = "Scanner device not found, check to see if the scanner driver is installed!"                    ";                return false;                rc = Dsmident (AppID, IntPtr.Zero, Twdg.control, twdat.identity, Twmsg.opends, Srcds);                    if (rc = twrc.success) {strscanerrormessage = "scanner device not found";                return false; } if (Multiscan) {///if it is a continuous scan twcapability cap = new TWcapability (Twcap.xfercount,-1);                rc = Dscap (AppID, Srcds, Twdg.control, twdat.capability, Twmsg.set, CAP); } else {//otherwise scan only one twcapability cap = new Twcapability                    (Twcap.xfercount, 1);                rc = Dscap (AppID, Srcds, Twdg.control, twdat.capability, Twmsg.set, CAP);                    if (rc = twrc.success) {strscanerrormessage = "scanner device not found";                    CLOSESRC ();                return false;                } twuserinterface Guif = new Twuserinterface (); Guif.                ShowUI = 1; Guif.                Modalui = 1; Guif.                Parenthand = hwnd;                rc = Dsuserif (AppID, Srcds, Twdg.control, Twdat.userinterface, Twmsg.enableds, Guif);                    if (rc = twrc.success) {strscanerrormessage = "There is no paper in the scanner";          CLOSESRC ();          Gc.                    Collect ();                return false;                }} catch (Exception e) {strscanerrormessage = E.message; Gc.                Collect ();            return false; } GC.            Collect ();        return true; }

After the scan is finished, the callback main window returns the data, and the main window adds the following event:

      private void Scan_passdatabetweenform (object sender, Passdataeventargs e)        {            if (e.eventstr = = "Exit")            { This                . Cursor = Cursors.waitcursor;                Frm.dtrow = This.dtrow;                                Some ways to work with pictures ....                                This. Enabled = true;                This. Cursor = Cursors.Default;                Gc. Collect ();            }        }        

Special statement: This article by Healer007 Original, signature radish. Reprint please indicate the source. Thank you!

C #: Scanner continuous scanning with TWAIN protocol

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.