|
Hmm - the code you've placed this above is in VB where as the code I gave is in C#. The code needs to be placed in TwainLib.cs under the "public void Aquire". To make it easy I've added the whole of my copy of the void below:
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;
}
TwCapability capDuplex = new TwCapability(TwCap.CAP_DUPLEXENABLED, 0, TwType.Bool);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capDuplex);
if (rc != TwRC.Success)
{
CloseSrc();
return;
}
TwCapability capResX = new TwCapability(TwCap.CAP_RESX, 150, TwType.Fix32);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capResX);
if (rc != TwRC.Success)
{
CloseSrc();
return;
}
TwCapability capResY = new TwCapability(TwCap.CAP_RESY, 150, TwType.Fix32);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capResY);
if (rc != TwRC.Success)
{
CloseSrc();
return;
}
TwCapability capPixelType = new TwCapability(TwCap.IPixelType, 0);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capPixelType);
if (rc != TwRC.Success)
{
CloseSrc();
return;
}
TwCapability capPaperSize = new TwCapability(TwCap.ICAP_SUPPORTEDSIZES, 1);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capPaperSize);
if (rc != TwRC.Success)
{
CloseSrc();
return;
}
TwCapability capAutoDeskew = new TwCapability(TwCap.ICAP_AUTOMATICDESKEW, 1, TwType.Bool);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capAutoDeskew);
if (rc != TwRC.Success)
{
CloseSrc();
return;
}
TwCapability capAutoScan = new TwCapability(TwCap.CAP_AUTOSCAN, 1, TwType.Bool);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capAutoScan);
if (rc != TwRC.Success)
{
CloseSrc();
return;
}
TwCapability capThreshold = new TwCapability(TwCap.ICAP_THRESHOLD, 100, TwType.Fix32 );
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capThreshold);
if (rc != TwRC.Success)
{
CloseSrc();
return;
}
TwCapability capBrightness = new TwCapability(TwCap.ICAP_BRIGHTNESS, 400, TwType.Fix32);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capBrightness);
if (rc != TwRC.Success)
{
CloseSrc();
return;
}
TwCapability capGamma = new TwCapability(TwCap.ICAP_GAMMA, 2, TwType.Fix32);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capGamma);
if (rc != TwRC.Success)
{
CloseSrc();
return;
}
TwCapability capHalfTones = new TwCapability(TwCap.ICAP_HALFTONES, "TextEnhancement Technology");
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capHalfTones);
if (rc != TwRC.Success)
{
CloseSrc();
return;
}
TwUserInterface guif = new TwUserInterface();
guif.ShowUI = 0;
guif.ModalUI = 1;
guif.ParentHand = hwnd;
rc = DSuserif( appid, srcds, TwDG.Control, TwDAT.UserInterface, TwMSG.EnableDS, guif );
if( rc != TwRC.Success )
{
CloseSrc();
return;
}
}
You then need to update the TwainDef.cs file and replace the "internal enum TwCap : short" with the following (the bits commented // DOESN'T WORK are just commands that my particular scanner wouldn't support):
internal enum TwCap : short
{
XferCount = 0x0001,
ICompression = 0x0100,
IPixelType = 0x0101,
IUnits = 0x0102,
IXferMech = 0x0103,
CAP_DUPLEXENABLED = 0x1013,
CAP_RESX = 0x1118,
CAP_RESY = 0x1119,
ICAP_SUPPORTEDSIZES = 0x1122,
ICAP_AUTOMATICDESKEW = 0x1151,
ICAP_AUTOMATICROTATE = 0x1152,
ICAP_AUTODISCARDBLANKPAGES = 0x1134,
ICAP_NOISEFILTER = 0x1148,
ICAP_AUTOBRIGHT = 0x1100,
CAP_AUTOSCAN = 0x1010,
ICAP_THRESHOLD = 0x1123,
ICAP_BRIGHTNESS = 0x1101,
ICAP_GAMMA = 0x1108,
ICAP_HALFTONES = 0x1109,
ICAP_PIXELFLAVOR = 0x111F
}
Then also in the TwainDef.cs near the bottom you'll find this:
public TwCapability(TwCap cap, string sval)
{
Cap = (short)cap;
ConType = (short)TwOn.One;
Handle = Twain.GlobalAlloc(0x42, 6);
IntPtr pv = Twain.GlobalLock(Handle);
Marshal.WriteInt16(pv, 0, (short)TwType.Str32);
Marshal.StringToHGlobalUni(sval);
Twain.GlobalUnlock(Handle);
}
Underneath this stick the following code in:
public TwCapability(TwCap cap, short sval, TwType TheType)
{
Cap = (short)cap;
ConType = (short)TwOn.One;
Handle = Twain.GlobalAlloc(0x42, 6);
IntPtr pv = Twain.GlobalLock(Handle);
if (TheType == TwType.Bool)
{
Marshal.WriteInt16(pv, 0, (short)TwType.Bool);
}
if (TheType == TwType.Fix32)
{
Marshal.WriteInt16(pv, 0, (short)TwType.Fix32);
}
Marshal.WriteInt32(pv, 2, (int)sval);
Twain.GlobalUnlock(Handle);
}
It should all work then
|
|
|
|
|
Have done all of this.
Autoscan didn't work - getting failure on line
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capAutoScan);
If autoscan capability code commented, all works good, except pauses for about a second between each list of paper.
Scanner: Fujitsu fi-5110c.
|
|
|
|
|
My scanner supports the TwCapability (ICAP_HALFTONES = 0x1109) = "TextEnhancement Technology".
So i've added into TwainLib.cs the following code:
TwCapability capHalfTones = new TwCapability(TwCap.ICAP_HALFTONES, "TextEnhancement Technology");
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capHalfTones);
if (rc != TwRC.Success)
{
CloseSrc();
return;
}
and created an overload in TwainDefs.cs:
public TwCapability(TwCap cap, string sval)
{
Cap = (short)cap;
ConType = (short)TwOn.One;
Handle = Twain.GlobalAlloc(0x42, 6);
IntPtr pv = Twain.GlobalLock(Handle);
Marshal.WriteInt16(pv, 0, (short)TwType.Str32);
Twain.GlobalUnlock(Handle);
}
My question is how to I use the Marshal object to write the string?
Many Thanks in advance
modified on Monday, September 21, 2009 7:07 AM
|
|
|
|
|
DSuserif will cause exception AccessViolationException: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
IS this project not compatiable with .Net framework 2.0?
Thank you!
|
|
|
|
|
Having the same problem and just want to shoot myself! Did you manage to solve it?
|
|
|
|
|
Hello,
I am trying to implement it to a user control but it in scan but doesn't works. Do I have to make specific changes or I am missing something can you please tell me.
Thank you
|
|
|
|
|
I am having a problem because twain returns control only after all documents in feeder are scanned. This leads to high memory usage as reported by an earlier poster.
I am setting cap_xfercount to 1 but this does not seem to help:
TwCapability cap = new TwCapability(TwCap.XferCount, 1);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, cap);
What do I need to do to scan just one document from the feeder?
Any suggestions appreciated.
|
|
|
|
|
Please i study this program and not found a solution for this problem.
My Scanner support duplex mode, but i not think to get the images from scanner.
I need update the TransferPictures function?
|
|
|
|
|
In TwainLib.cs before "TwUserInterface guif = new TwUserInterface();" at around line 108 add the following:
TwCapability capDuplex = new TwCapability(TwCap.CAP_DUPLEXENABLED, 1, true);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capDuplex);
if (rc != TwRC.Success)
{
CloseSrc();
return;
}
Then in TwainDef.cs add the following overloaded method at line 311:
public TwCapability(TwCap cap, short sval, bool blnType)
{
Cap = (short)cap;
ConType = (short)TwOn.One;
Handle = Twain.GlobalAlloc(0x42, 6);
IntPtr pv = Twain.GlobalLock(Handle);
Marshal.WriteInt16(pv, 0, (short)TwType.Bool);
Marshal.WriteInt32(pv, 2, (int)sval);
Twain.GlobalUnlock(Handle);
}
Then in the enum TwCap add
CAP_DUPLEXENABLED = 0x1013
|
|
|
|
|
I think you should set it up as a short so change
Marshal.WriteInt32(pv, 2, (int)sval);
into
Marshal.WriteInt16(pv, 2, (int)sval);
|
|
|
|
|
Hi,
I have spent about two days trying to get the duplex to work with a fijitsu f1-5110C scanner. Have any of you got this working?
Morke
|
|
|
|
|
The following code worked for me (I did not check it with a Fujitsu scanner)
irc = SetCapability(TwCap.CAP_DUPLEXENABLED, (int)TwType.Bool, Value);
1st Note:
For setting TwType.Bool I used 2 bytes:
<br />
case TwType.Bool:<br />
case TwType.Int16:<br />
case TwType.UInt16:<br />
Int16 i16 = (Int16)Value;<br />
cap = new TwCapability(CapType, i16, 2, (short)ItemType);<br />
break;<br />
2nd Note
When you transfer the images, you need to loop
do<br />
{<br />
.<br />
.<br />
rc = DSpxfer( appid, srcds, TwDG.Control, TwDAT.PendingXfers, TwMSG.EndXfer, pxfr );<br />
.<br />
.<br />
}<br />
while( pxfr.Count != 0 );<br />
Good Luck
|
|
|
|
|
First Thanks for the code. After a long day +1 of attempting to get the duplex mode running using the instructions in the below message thread
" Use Duplex with Driver Twain - Help URGENT " I finally got the duplex mode by incorporating the code from that message thread along with setting TwCapability cap from 1 to 2 in TwainLib.cs (line 102 +-)
TwCapability cap= newTwCapability( TwCap.XferCount, 2 );
Again Thanks for the assist!
Morke
|
|
|
|
|
when i scanning multi-page documents, then cancel it to pause the operation, the program run into dead.
pclion
|
|
|
|
|
You need to handle cancel event/return message.
VB.Net Sample:
In TwainLib
Declarations:
Public Event TransferReady()
Public Event AbortTranfer()
PassMessage:
RaiseEvent TransferReady()...
Or other events declared...
Heavy givers are light complainers
|
|
|
|
|
when you not have a scanner device you use a file image to process . But when use this the twain dsm took the image from the default folder on the machine. I need to make a form to change the source of the image to be acquire. How can I do this. Plese help me !!!!!!
|
|
|
|
|
|
Hi!
I have a problem scanning from minolta microfilm scanner.
Pixel type returned is 1bpp, i get pointer to dib, but then when trying to aquire BITMAPINFOHEADER it sets all fields to -1, and image cannot be converted from DIB to Bitmap.
Anyone has any idea how to fix that?
Thanks
public static IntPtr GetPixelInfo(IntPtr bmpptr, ref Rectangle bmprect)
{
BITMAPINFOHEADER bmi = new BITMAPINFOHEADER();
Marshal.PtrToStructure(bmpptr, bmi); <<------ bmi fields set to -1
bmprect.X = bmprect.Y = 0;
bmprect.Width = bmi.biWidth;
bmprect.Height = bmi.biHeight;
if (bmi.biSizeImage == 0)
bmi.biSizeImage = ((((bmi.biWidth * bmi.biBitCount) + 31) & ~31) >> 3) * bmi.biHeight;
int p = bmi.biClrUsed;
if ((p == 0) && (bmi.biBitCount <= 8))
p = 1 << bmi.biBitCount;
p = (p * 4) + bmi.biSize + (int)bmpptr;
return (IntPtr)p;
}
|
|
|
|
|
Hi Nasharu,
have you found any solution for this?
Cheers,
G.
|
|
|
|
|
|
Hello,
Everything works fine when i'am using WIA driver's
But when using the Twain drivers for the most scanners i use (also network scanners), the dsuerif method to open the userinterface does NOT wait the user response on the user interface (scan or cancel) and returns a SUCCESS call ..... and my app loops.
Do i something wrong? why the method does not wait and returns directly SUCCESS ?
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 0;
}
Thanks a lot for your help .....
|
|
|
|
|
How can I get two or more image in one click?
what function shoould I use? or Import from the Library?
Thanks Before,
|
|
|
|
|
hum... it is very easy with a scanner like DR-1210 from canon, you just has to change this line :
TwCapability cap = new TwCapability(TwCap.XferCount, 1);
with
TwCapability cap = new TwCapability(TwCap.XferCount, -1);
|
|
|
|
|
One word - GENIUS!!!
This one line saved me a day's trawl on google!!!!
Thanks a million!
Heavy givers are light complainers
|
|
|
|
|
I'm using a canon dr-1210. I tried your suggestion and it worked. But now it is freezing when all the papers finish in the feeder. After I press the stop button on the scanner then I can see the images.
What should I do about this freezing problem. It is very important for me.
|
|
|
|
|