0

LINK: http://www.codeproject.com/KB/dotnet/twaindotnet.aspx

I'm trying to create a wrapper class for this open source .NET implementation of TWAIN and I'm having trouble understand how it actually gets the image.

I've downloaded the source code and in the GUI there is a button called Acquire. When I click this button to go to it's event handler I find this code which I assume gets the image:

private void menuItemScan_Click(object sender, System.EventArgs e)
{
    if (!msgfilter)
    {
        this.Enabled = false;
        msgfilter = true;
        Application.AddMessageFilter(this);
    }
    tw.Acquire();
}

If I follow the Acquire() method to see it's contents, I see this:

public void Acquire()
{
    TwRC rc;
    CloseSrc();
    if (appid.Id == IntPtr.Zero)
    {
        Init(hwnd);
        if (appid.Id == IntPtr.Zero)
            return;
    }
    rc = DSMident(appid, IntPtr.Zero, TwDG.Control, TwDAT.Identity, TwMSG.OpenDS, srcds);
    if (rc != TwRC.Success)
        return;

    TwCapability cap = new TwCapability(TwCap.XferCount, 1);
    rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, cap);
    if (rc != TwRC.Success)
    {
        CloseSrc();
        return;
    }

    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)
    {
        CloseSrc();
        return;
    }
}

What I don't understand is how a method with a 'void' return type can actually have a return statement. Also, where is it acquiring and returning an image?

Can anyone help out?

I'm trying to create a useful wrapper and open source it, because as it stands there is no easy drag and drop solution for scanning images in C#.

Thanks for the help!

Edit: Thanks for the help regarding early returns. TIL! Now I'm curious about how the application gets the images to display on the form.

Any guidance?

  • 2
    @Serg - You can call return to terminate a method. The last return is irrelevant because the method ends after the if statement anyway. – tpow Dec 02 '10 at 13:11

4 Answers4

3

"Void" means it returns nothing, not that it doesn't return. So the return statement just terminates the function and returns to the caller

For your other question, there are a few other relevant stack overflow questions

The DSCap line is seeing if there are multiple images. The capture happens as part of the call to DSuserif

Community
  • 1
  • 1
The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134
  • Ah thanks! And what about the acquiring of the image. Do you see where he actually has the Image item? –  Dec 02 '10 at 13:17
1

Infact, you set a message filter on your form by calling Application.AddMessageFilter(this) method. So, you have to listen to scanner events and when you get a TwainCommand.TransferReady event, you will call TransferPictures() to get the image collection.

taskinoor
  • 45,586
  • 12
  • 116
  • 142
Can
  • 11
  • 1
0

The method simply returns void to avoid other code segments being executed. That is completely legal. The method is not acquiring an image it only prepares the hardware and UI that is acquiring the image, i'd say.

ovm
  • 2,452
  • 3
  • 28
  • 51
0

return; causes the control flow to exit the function.

Had a look at the library. It seems that Acquire() just causes the driver to perform an acquire, and TransferPictures() is called to retrieve the pictures (that one returns an ArrayList, so yes it is returning something).

lijie
  • 4,811
  • 22
  • 26