DynamsoftDotNetTwain FAQ

From PeformIQ Upgrade
Revision as of 06:36, 17 October 2021 by PeterHarding (talk | contribs) (Created page with " Programming Questions 1. How can I acquire black-white images? <pre> Twain1.OpenSource(); Twain1.IfShowUI = false; Twain1.IfDisableSourceAfterAcquire = true; Twain1.PixelTy...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Programming Questions

1. How can I acquire black-white images?

Twain1.OpenSource();
Twain1.IfShowUI = false;
Twain1.IfDisableSourceAfterAcquire = true;
Twain1.PixelType = Dynamsoft.DotNet.TWAIN.Enums.TWICapPixelType.TWPT_BW;
Twain1.AcquireImage();

2. How can I work without User Interface?

private void BeginScan()
{
    Twain1.OpenSource();
    Twain1.IfShowUI = false; // set without user interface
    Twain1.IfDisableSourceAfterAcquire = true;

    Twain1.AcquireImage();
}


private void Twain1_OnPostTransfer()
{
    Twain1.SaveAsBMP("c:\temp.bmp", 0);
}

3. How can I scan only a part of a picture?

Twain1.SelectSource();
Twain1.OpenSource();
Twain1.IfShowUI = false;


// Set Image Layout
Twain1.Unit = Dynamsoft.DotNet.TWAIN.Enums.TWICapUNits.TWUN_INCHES; // INCHES
Twain1.SetImageLayout(0, 0, 5, 5);

Twain1.AcquireImage();

4. How can I call my scanner UI without acquiring an image? This feature is supported by EnableSourceUI.

Twain1.EnableSourceUI();

5. How can I select a device without the device selection dialog?

Twain1.OpenSourceManager();

int count = Twain1.SourceCount;

for (short lngNum = 0; lngNum < count; lngNum ++)
{
    if (Twain1.SourceNameItems(lngNum) == "Specified device name")
    {
        Twain1.SelectSourceByIndex (lngNum);
        break;
    }
}

// if can't find the specified source, it'll select default source
Twain1.AcquireImage();

6. How can I disable the progress indicator dialog when I acquire images without UI?

Twain1.IfShowUI = false;
Twain1.IfDisableSourceAfterAcquire = true;

Twain1.OpenSource();
Twain1.IfShowIndicator = false;
Twain1.AcquireImage();

7. How can I set resolution in the X and Y directions separately?

Twain1.SelectSource();
Twain1.OpenSource();

//Set XRESOLUTION current value.
Twain1.Capability = Dynamsoft.DotNet.TWAIN.Enums.TWCapability.ICAP_XRESOLUTION;
Twain1.CapType = Dynamsoft.DotNet.TWAIN.Enums.TWCapType.TWON_ONEVALUE;
Twain1.CapValue = 300;
if (Twain1.CapSet() == false)
    MessageBox.Show("Failed to set the x-resolution.\r\n" + Twain1.ErrorString);

//Set YRESOLUTION current value.
Twain1.Capability = Dynamsoft.DotNet.TWAIN.Enums.TWCapability.ICAP_YRESOLUTION;
Twain1.CapType = Dynamsoft.DotNet.TWAIN.Enums.TWCapType.TWON_ONEVALUE;
Twain1.CapValue = 200;
if (Twain1.CapSet() == false)
    MessageBox.Show("Failed to set the y-resolution.\r\n" + Twain1.ErrorString);

Twain1.AcquireImage();

8. How can I upload acquired images to the Web server? Dynamic .NET TWAIN supports FTP, HTTP Put and HTTP Post upload method. The following is a http put method sample:

private void BeginScan()
{
    Twain1.AcquireImage();
}

private void Twain1_OnPostTransfer()
{
	if (Twain1.HTTPUploadThroughPut(
	"127.0.0.1", 0, "httpUploadFileTemp.bmp") == false)
	MessageBox.Show(Twain1.ErrorString);
}

9. How to use ADF? We want to save each document in a separate file.

int iDocumentCounter = 0;

private void ADFScan()
{
    iDocumentCounter = 0;  //set initialize value

    Twain1.OpenSource();

    Twain1.IfShowUI = false;
    Twain1.IfDisableSourceAfterAcquire = true;
    if (Twain1.Duplex != Dynamsoft.DotNet.TWAIN.Enums.TWICapDuplex.TWDX_NONE)
        Twain1.IfDuplexEnabled = true; //enable duplex

    if (Twain1.IfFeederEnabled)
    {
        Twain1.XferCount = -1;
        Twain1.IfAutoFeed = true; //auto feed 

        if (Twain1.IfFeederLoaded)
            Twain1.AcquireImage();
    }
}

private void Twain1_OnPostTransfer()
{
    iDocumentCounter = iDocumentCounter + 1;

    if (!Twain1.SaveAsBMP(@"c:\" + iDocumentCounter.ToString() + ".bmp", 0))
            MessageBox.Show(Twain1.ErrorString);
}

10. How can I work with SSL? You can use the IfSSL property.

Twain1.HTTPPort = 80; 
Twain1.IfSSL = false; // if 80 is the port number of non-secure port


/* 
Twain1.HTTPPort = 443; 
Twain1.IfSSL = true; // if 443 is the port number of secure port
*/

Twain1.HTTPUploadThroughPost("127.0.0.1", 0, "/SaveToFile.php", "imageData.jpg");

if (Twain1.ErrorCode != Dynamsoft.DotNet.TWAIN.Enums.ErrorCode.Succeed)
    MessageBox.Show(Twain1.ErrorString);
else //succeded
    MessageBox.Show("Successful");


11. How can I add a new scanned image to an existing multi-page TIFF file?

private void Scan()
{
    Twain1.AcquireImage();
}
        
private void Twain1_OnPostTransfer()
{
    Twain1.CloseSource();

    Twain1.IfTiffMultiPage = true;
    Twain1.SaveAsTIFF (@"C:\SaveAsTIFF.tif", 0);

    if (Twain1.ErrorCode != Dynamsoft.DotNet.TWAIN.Enums.ErrorCode.Succeed)
        MessageBox.Show(Twain1.ErrorString);
    else 
        MessageBox.Show("Successful");
}

12. How can I rotate an image before I save it? You can use the RotateLeft property and the RotateRight property.

private void Scan()
{
    Twain1.AcquireImage();
}

private void RotateLeft()
{
    Twain1.RotateLeft(0);
}

private void RotateRight()
{
    Twain1.RotateRight(0);
}

private void Rotate()
{
    Twain1.Rotate(0, 45.0, true, 
	Dynamsoft.DotNet.TWAIN.Enums.DWTInterpolationMethod.Bicubic); 
}
Also, you can use the ShowImageEditor method.

private void Scan()
{
    Twain1.AcquireImage();
}

private void Twain1_OnPostTransfer()
{
    Twain1.CloseSource();

    Twain1.ShowImageEditor();
// You need to save the rotated image in the image editor before closing it    
}

13. How can I view a specified image if there are several images in buffer? You can view a specified image or go through all the images in buffer with clicking "NextImage" or "PreImage" buttons.

private void Next()
{
    if (Twain1.HowManyImagesInBuffer == 0)
        MessageBox.Show("There is no image in buffer");

    Twain1.CurrentImageIndexInBuffer = (short)(Twain1.CurrentImageIndexInBuffer + 1);
}

private void Previous()
{
    if (Twain1.HowManyImagesInBuffer == 0)
        MessageBox.Show("There is no image in buffer");

    Twain1.CurrentImageIndexInBuffer = (short)(Twain1.CurrentImageIndexInBuffer - 1);
}

14. How can I do a duplex scan?

private void DuplexScan()
{
    Twain1.SelectSource();
    Twain1.OpenSource();

    Twain1.IfDuplexEnabled = true; //enable duplex
    Twain1.XferCount = 2;
    Twain1.MaxImagesInBuffer = 2;

    Twain1.AcquireImage();
}

15. How can I scan an inverted image? You can use the PixelFlavor property.

private void Scan()
{
    Twain1.PixelFlavor = TWICapPixelFlavor.TWPF_CHOCOLATE;
    Twain1.AcquireImage();
}