C# Sample: Acquire and save to BMP file

Top  Previous  Next

The following C# example is all that is required to do the following:

 

Check if TWAIN is installed.
Initialize the DTWAIN Dynamic Link Library.
Select a Source
Acquire a page using the Native transfer mode
Save page to a BMP file called "test.bmp"
Shut down any open TWAIN Source's and DTWAIN itself

___________________________________________________________

 

using Dynarithmic;

//....

[STAThread]

static void Main()

{

    int TwainOK = TwainAPI.DTWAIN_IsTwainAvailable()

    if ( TwainOK == 1 )

    {

         TwainAPI.DTWAIN_SysInitialize();

         int status = 0;

         int SelectedSource = TwainAPI.DTWAIN_SelectSource();

         if ( SelectedSource != 0 )

         {

            TwainAPI.DTWAIN_AcquireFile(

                             SelectedSource,

                             "Test.bmp",

                             TwainAPI.DTWAIN_BMP,

                             TwainAPI.DTWAIN_USENATIVE | TwainAPI.DTWAIN_USELONGNAME,

                             TwainAPI.DTWAIN_PT_DEFAULT,

                             1,1,ref status);

            TwainAPI.DTWAIN_SysDestroy();

         }

    }

}

 

For C#, the Dynarithmic.TwainAPI class found in the file DTWAIN32.CS is a class wrapper for the DTWAIN functions.  Note that it is as simple as calling five DTWAIN functions to get the job done.