What's wrong with this code?

Discussions about TWAIN & WIA scanning in GdPicture.NET using GdPictureImaging.
Post Reply
xr1fab
Posts: 2
Joined: Tue Oct 19, 2010 3:24 pm

What's wrong with this code?

Post by xr1fab » Tue Oct 19, 2010 3:32 pm

Hello, I'm struggling with this problem: I need to acquire from a TWAIN source (a scanner with automated feeder) a multidocument with many sheets. Following your documentation, I wrote the following code:

Code: Select all

private void AcquireImages()
{
    int imageID = 0;
    int multiPageHandle = 0;
    int imageCount = 0;
    string fileName = string.Empty;
    
    try
    {
        do
        {
            imageID = this.oGdPictureImaging.TwainAcquireToGdPictureImage(this.Handle);
            imageCount++;
            if (imageID > 0)
            {
                if (imageCount == 1)
                {
                    multiPageHandle = imageID;
                    fileName = "LDV" + multiPageHandle.ToString(CultureInfo.CurrentCulture) + ".tif";
                    this.oGdPictureImaging.TiffSaveAsMultiPageFile(multiPageHandle, fileName, TiffCompression.TiffCompressionCCITT4);
                    this.oGdPictureImaging.ReleaseGdPictureImage(imageID);
                }
                else
                {
                    try
                    {
                        this.oGdPictureImaging.TiffAddToMultiPageFile(multiPageHandle, imageID);
                    }
                    catch (Exception e)
                    {
                        Debug.Write(e.Message);
                    }  
                
                    this.oGdPictureImaging.ReleaseGdPictureImage(imageID);
                }
            }
        }
        while (this.oGdPictureImaging.TwainGetState() > TwainStatus.TWAIN_SOURCE_ENABLED);
    }
    finally
    {
        this.oGdPictureImaging.ReleaseGdPictureImage(multiPageHandle);
        this.oGdPictureImaging.TwainCloseSource();
    }
}
Whenever I test my application, in the call to method TiffAddToMultiPageFile I catch the exception "object reference not set with an instance of an object".
My question is: what's wrong with my code?

Thanks in advance
Fabio

User avatar
Loïc
Site Admin
Posts: 5701
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: What's wrong with this code?

Post by Loïc » Tue Oct 19, 2010 5:00 pm

Hi Fabio,

Try to replace

Code: Select all

 if (imageCount == 1)
                {
                    multiPageHandle = imageID;
                    fileName = "LDV" + multiPageHandle.ToString(CultureInfo.CurrentCulture) + ".tif";
                    this.oGdPictureImaging.TiffSaveAsMultiPageFile(multiPageHandle, fileName, TiffCompression.TiffCompressionCCITT4);
                    this.oGdPictureImaging.ReleaseGdPictureImage(imageID);
                }
by

Code: Select all

 if (imageCount == 1)
                {
                    multiPageHandle = imageID;
                    fileName = "LDV" + multiPageHandle.ToString(CultureInfo.CurrentCulture) + ".tif";
                    this.oGdPictureImaging.TiffSaveAsMultiPageFile(multiPageHandle, fileName, TiffCompression.TiffCompressionCCITT4);
                }
Should work now !

xr1fab
Posts: 2
Joined: Tue Oct 19, 2010 3:24 pm

Re: What's wrong with this code?

Post by xr1fab » Fri Oct 22, 2010 2:04 pm

Great! Now it works!
Thanks a lot.

Fabio

User avatar
Loïc
Site Admin
Posts: 5701
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: What's wrong with this code?

Post by Loïc » Fri Oct 22, 2010 3:49 pm

I'm glad !

Thank you for the return.

Cheers,

Loïc

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests