private void AcquireBuffered_Click(object sender, EventArgs e)
        {
            if (SelectedSource != 0)
            {
                TwainAPI.DTWAIN_SetBlankPageDetection(SelectedSource, 98.5,
                                                      (int)TwainAPI.DTWAIN_BP_AUTODISCARD_ANY,
                                                      DiscardBlankPages.Checked ? 1 : 0);

                DTWAIN_ARRAY acquireArray = TwainAPI.DTWAIN_CreateAcquisitionArray();
                this.Enabled = false;
                int status = 0;
                if (TwainAPI.DTWAIN_AcquireBufferedEx(SelectedSource, TwainAPI.DTWAIN_PT_DEFAULT,
                                                      TwainAPI.DTWAIN_ACQUIREALL, UseSourceUI.Checked ? 1 : 0, 0, acquireArray, ref status) == 0)
                {
                    MessageBox.Show("Acquisition Failed", "TWAIN Error");
                    return;
                }

                if (TwainAPI.DTWAIN_ArrayGetCount(acquireArray) == 0)
                {
                    MessageBox.Show("No Images Acquired", "");
                    return;
                }

                // Display the DIBS
                //...
                DIBDisplayerDlg sDIBDlg = new DIBDisplayerDlg(acquireArray);
                sDIBDlg.ShowDialog();
                TwainAPI.DTWAIN_DestroyAcquisitionArray(acquireArray, 0);
                this.Enabled = true;
            }
        }
        private void AcquireToFile(int nWhich)
        {
            if (SelectedSource != 0)
            {
                int    status    = 0;
                int    bError    = 0;
                long   FileFlags = 0;
                string tFileName = "";
                int    fileType  = 0;
                switch (nWhich)
                {
                case 0:
                    FileFlags = TwainAPI.DTWAIN_USELONGNAME | TwainAPI.DTWAIN_USENATIVE;
                    TwainAPI.DTWAIN_SetBlankPageDetection(SelectedSource, 98.5,
                                                          (int)TwainAPI.DTWAIN_BP_AUTODISCARD_ANY,
                                                          DiscardBlankPages.Checked ? 1 : 0);
                    FileTypeDlg fDlg = new FileTypeDlg();
                    fDlg.ShowDialog();
                    tFileName = fDlg.GetFileName();
                    StringBuilder szSourceName = new StringBuilder(tFileName);
                    fileType = fDlg.GetFileType();
                    break;

                case 1:
                    if (TwainAPI.DTWAIN_IsFileXferSupported(SelectedSource, TwainAPI.DTWAIN_ANYSUPPORT) == 0)
                    {
                        MessageBox.Show("Sorry.  The selected driver does not have built-in file transfer support.");
                        return;
                    }
                    if (TwainAPI.DTWAIN_IsFileXferSupported(SelectedSource, TwainAPI.DTWAIN_FF_BMP) == 0)
                    {
                        string sText = "Sorry.  This demo program only supports built-in BMP file transfers.\r\n";
                        sText += "However, the DTWAIN library will support all built-in formats if your driver\r\n";
                        sText += "supports other formats.";
                        MessageBox.Show(sText);
                        return;
                    }
                    FileFlags = TwainAPI.DTWAIN_USESOURCEMODE | TwainAPI.DTWAIN_USELONGNAME;
                    fileType  = TwainAPI.DTWAIN_FF_BMP;
                    tFileName = ".\\IMAGE.BMP";
                    MessageBox.Show("The name of the image file that will be saved is IMAGE.BMP\n");
                    break;
                }

                bError = TwainAPI.DTWAIN_AcquireFile(SelectedSource,
                                                     tFileName,
                                                     fileType,
                                                     (int)FileFlags,
                                                     TwainAPI.DTWAIN_PT_DEFAULT, /* Use default */
                                                     TwainAPI.DTWAIN_ACQUIREALL, /* Get all pages */
                                                     UseSourceUI.Checked ? 1 : 0,
                                                     1,                          /* Close Source when UI is closed */
                                                     ref status
                                                     );

                if (bError == 0)
                {
                    MessageBox.Show("Error acquiring or saving file.");
                }
                else
                if (status == TwainAPI.DTWAIN_TN_ACQUIREDONE)
                {
                    MessageBox.Show("Image file saved successfully");
                }
                else
                {
                    MessageBox.Show("The acquisition returned a status of " + status.ToString());
                }
            }
        }