|
Hi everyone,
I solved the puzzle. This is the code contains duplex, pixeltype, deskew, scan ahead, border detect, resolution.
It works successfully.
For Acquire method (TwainLib) :
// MAIN CAP AND MULTI SCAN
TwCapability cap = new TwCapability(TwCap.XferCount, -1);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, cap);
if (rc != TwRC.Success)
{
CloseSrc();
MessageBox.Show("MAIN CAP FAILED");
return;
}
// DUPLEX
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();
MessageBox.Show("DUPLEX FAILED");
return;
}
//// 0 = BW | 1 = Gray | 2 = Colour
TwCapability capPixelType = new TwCapability(TwCap.IPixelType, 1, TwType.UInt16);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capPixelType);
if (rc != TwRC.Success)
{
CloseSrc();
MessageBox.Show("PIXEL TYPE FAILED");
return;
}
// BORDER DETECTION
TwCapability capBorder = new TwCapability(TwCap.ICAP_AUTOMATICBORDERDETECTION, 1, TwType.UInt16);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capBorder);
if (rc != TwRC.Success)
{
CloseSrc();
MessageBox.Show("BORDER DETECT FAILED");
return;
}
//// Set the X resolution
TwCapability capResX = new TwCapability(TwCap.ICAP_XRESOLUTION, 100, TwType.Fix32);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capResX);
if (rc != TwRC.Success)
{
CloseSrc();
MessageBox.Show("XRES FAILED");
return;
}
//// Set the Y resolution
TwCapability capResY = new TwCapability(TwCap.ICAP_YRESOLUTION, 100, TwType.Fix32);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capResY);
if (rc != TwRC.Success)
{
CloseSrc();
MessageBox.Show("YRES FAILED");
return;
}
// DESKEW
TwCapability capDeskew = new TwCapability(TwCap.ICAP_AUTOMATICDESKEW, 1, TwType.Fix32);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capDeskew);
if (rc != TwRC.Success)
{
CloseSrc();
MessageBox.Show("DESKEW FAILED");
return;
}
For TwCapability internal class (TwainDefs):
internal class TwCapability
{ // TW_CAPABILITY
public TwCapability(TwCap cap)
{
Cap = (short)cap;
ConType = -1;
}
public TwCapability(TwCap cap, short sval)
{
Cap = (short)cap;
ConType = (short)TwOn.One;
Handle = Twain.GlobalAlloc(0x42, 6);
IntPtr pv = Twain.GlobalLock(Handle);
Marshal.WriteInt16(pv, 0, (short)TwType.Int16);
Marshal.WriteInt32(pv, 2, (int)sval);
Twain.GlobalUnlock(Handle);
}
public TwCapability(TwCap cap, short sval, TwType twType)
{
Cap = (short)cap;
ConType = (short)TwOn.One;
Handle = Twain.GlobalAlloc(0x42, 6);
IntPtr pv = Twain.GlobalLock(Handle);
Marshal.WriteInt16(pv, 0, (short)twType);
Marshal.WriteInt32(pv, 2, (int)sval);
Twain.GlobalUnlock(Handle);
}
~TwCapability()
{
if (Handle != IntPtr.Zero)
Twain.GlobalFree(Handle);
}
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);
}
public short Cap;
public short ConType;
public IntPtr Handle;
}
For Twcap ( TwainDefs) :
internal enum TwCap : short
{
XferCount = 0x0001, // CAP_XFERCOUNT
ICompression = 0x0100, // ICAP_...
IPixelType = 0x0101,
IUnits = 0x0102,
IXferMech = 0x0103,
CAP_AUTOSCAN = 0x1010,
CAP_DUPLEXENABLED = 0x1013,
ICAP_XRESOLUTION = 0x1118,
ICAP_YRESOLUTION = 0x1119,
ICAP_SUPPORTEDSIZES = 0x1122,
ICAP_AUTOMATICBORDERDETECTION = 0x1150,
ICAP_AUTOMATICDESKEW = 0x1151
}
|
|
|
|
|
can you tell me how get the capability of the scanner?
Although i know how to get the desired Item , but I don't know how to read the value.
|
|
|
|
|
Hi guys, I'm in the process of creating a simple wrapper class based on NETMaster's code. I'm having a lot of trouble following the code though.
You can follow a thread I'm writing on as I work on the project here:
http://www.dreamincode.net/forums/topic/203020-c%23-open-source-twain-wrapper/
I can't understand how to implement the code correctly so it returns a System.Drawing.Image.
I was wondering if an expert can come in and shed some knowledge. If we can build a decent wrapper class for this a lot of developer world wide will benefit. I kind of regret that NETMaster mixed GUI with core functionality, but at least he gave us a head start!
Please let me know if you'd like to help! Send me an email at stapia.gutierrez [AT] gmail [DOT] com
Hopefully we can create something recognized world wide!
|
|
|
|
|
Hello,
I used the code from the example. My windows form application can scan directly inside the code and save images to local path successfully, but I've a major problem about this scanning process. I have to set pageSize,dpi,scanning side,deskew vs. settings inside the code.
I read all the messages at this forum but I couldn't figure out how to set these settings. For example someone said that we can add the code below for the resolution setting.
I added "IXResolution=0x1118" under the TwCap and also created a overload for TwCapability. When I try this code or similar codes like this, I am getting an failure from "rc". Also there is a capability order for these settings but I couldn't figure out. Can anyone send me a working code with these settings ?
TwCapability capResX = new TwCapability(TwCap.IXResolution, 100, TwType.Fix32);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capResX);
if (rc != TwRC.Success)
{
CloseSrc();
MessageBox.Show("IXResolution");
return;
}
|
|
|
|
|
Can you Please post Your Scan and save image code to localpath directly.
I am searching from last few days .please post this code to tiru.jogu123@yahoo.com
|
|
|
|
|
Hi,
Is possible compile this on x64? I know that it throw exception BadImageFormatException because twain_32.dll is only for x86. But what if I use TWAINDSM.dll? I try it. If I compiled it on x86, everything is ok, but it I compiled it on x64 it doesn't throw any exception but it doesn't work too.
I need compile my application on x64. Any idea?
|
|
|
|
|
Hi,
When I just download the .exe from this article it works fine. But when I compile the source code, it compiles fine, but when I try to run it stops at the following line (ln 59 TwainLib.cs):
TwRC rc = DSMparent( appid, IntPtr.Zero, TwDG.Control, TwDAT.Parent, TwMSG.OpenDSM, ref hwndp );
And the output shows:
The thread 'vshost.LoadReference' (0x1730) has exited with code 0 (0x0).
'TwainGui.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Users\xxxxx\Desktop\twaingui_src\TwainGui\bin\Debug\TwainGui.exe', Symbols loaded.
A first chance exception of type 'System.BadImageFormatException' occurred in TwainGui.exe
'TwainGui.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The program '[2472] TwainGui.vshost.exe: Program Trace' has exited with code 0 (0x0).
The program '[2472] TwainGui.vshost.exe: Managed (v2.0.50727)' has exited with code 0 (0x0).
It is working on Windows 7 32 bit, I can compile and run.
I tryed to download and install both 32 and 64 bits versions found in www.twain.org, compiled only to 32 bit, tryed to change the reference to twain_32.dll to twain.dll from dllimport, since both dlls exists, but it still doesn´t work.
Thank you,
Edgar
|
|
|
|
|
Change the setting of your project and set the platform target as 'x86'.
Michela
|
|
|
|
|
Hello everyone:
I want to show UI only,do not transfer the image to program.
I try that:
TwUserInterface guif = new TwUserInterface();
guif.ShowUI = 1;
guif.ModalUI = 0;
guif.ParentHand = hwnd;
rc = DSuserif(appid, srcds, TwDG.Control, TwDAT.UserInterface, TwMSG.EnableDSUIOnly, guif);
but it always returns failure.
I want to know why? does driver not support?or other problem?
how can I do?
thank you
|
|
|
|
|
I use your code but I want to insert imagelist ....
case TwainCommand.TransferReady:
{
ArrayList pics = tw.TransferPictures();
EndingScan();
tw.CloseSrc();
picnumber++;
for( int i = 0; i < pics.Count; i++ )
{
IntPtr img = (IntPtr) pics[ i ];
PicForm newpic = new PicForm( img );
newpic.MdiParent = this;
int picnum = i + 1;
imageList1.ImageSize= new Size(200, 200);
newpic.Text = "ScanPass" + picnumber.ToString() + "_Pic" + picnum.ToString();
newpic.Show();
}
break;
}
in that peace of code I want to create from the pics = tw.TransferPictures(); an ImageList , but I dnt know how to convert using the GDI library from the IntPtr to Bitmap ...
please help me....
|
|
|
|
|
Hello every one,
I need to get duplex capability i used these couple of methods but i get "Success" with canon 2200 and Failure with hp laserjet1522 (its actually supports duplex scanning ) so i couldn't identified problem that is it device dependent or what ..please help me what should i do...?
//for duplex scanning capability
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);
//Marshal.WriteInt16(pv, 2, (int)sval);
Twain.GlobalUnlock(Handle);
}
//This is for duplex setting
TwCapability capDuplex = new TwCapability(TwCap.CAP_DUPLEXENABLED, 0, true);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capDuplex);
if (rc != TwRC.Success)
{
CloseSrc();
return;
}
//For capability
public TwCapability( TwCap cap, short sval )
{
Cap = (short) cap;
ConType = (short) TwOn.One;
Handle = Twain.GlobalAlloc( 0x42, 6 );
IntPtr pv = Twain.GlobalLock( Handle );
Marshal.WriteInt16(pv, 0, (short)TwType.Int16);//original
//Marshal.WriteInt16(pv, 0, (short)TwType.Fix32);
Marshal.WriteInt32( pv, 2, (int) sval );
Twain.GlobalUnlock( Handle );
}
please anybody help...
|
|
|
|
|
Hello ,
I have develop an application using twain API and create an application in both platform 32 and 64 bit machine .
but when i installed 64bit exe it gives some error so i have tried 32 exe and its working but there is some problem with ShowUI ,if i set it to true application hang but if i dnt try to open showUI its working. what is the problem what i need to do to work my application in both platform ?
please any help me . i have big issue on this topic ..
|
|
|
|
|
Hi,
i think you have to compile your app for 32-bit. A 64-bit app can't use a 32-bit dll.
Michael
|
|
|
|
|
hello ,,thanks for reply but i did it ..but problem is not about platform but shoUI not working ..
|
|
|
|
|
Trying this on xerox scanner
With ADf, say I am scanning 1..n pages, with xfercount set to -1 and using adf but disabling the twain gui from scanner, only page 1 is seen, the rest of the pages in the viewer show up as garbage.
Any ideas?
|
|
|
|
|
hi, i need to scan an image then show it in picturebox but this project is professional then i cant change it, i worked on it but i couldnt
please put easier sample for beginners
modified on Tuesday, September 14, 2010 4:51 PM
|
|
|
|
|
error in memory with framework 2 and win 7 professional
|
|
|
|
|
Hi there. I am just having the same exact problem. Have you manage to solve it?
|
|
|
|
|
Hi,
how can i add twain ui into my applicaiton. I want to open it in my applicaiton panel.
|
|
|
|
|
Hi,
My program is working good with ICAP_XFERMETH = TWSX_NATIVe and Duplex = TRUE.
My problem is that I need to acquire more then 300 documents and my memory always is full.
How do I change to save the images automaticaly to a file?
Sorry my bad english and thank you advice.
|
|
|
|
|
I need Twain ActiveX thats not required UI for Source Selection and Other
Just direct scan at one Click
|
|
|
|
|
How can i read the image directly into a picture box using vb.net
|
|
|
|
|
Hi, i want to take images by time from my camera automatically, for example every 5 seconds, but i dont know how to send take snapshot message to the twain interface of my camera ! is it possible to send messages from my application to cameras twain interface or do i have to write that interface myself ? And How ? Thank you
|
|
|
|
|
Hi,
I have been looking through the twain spec and am not too sure how to return a lets of all possible values for a capability.
Using the code below I can get the currnent value:
Dim XRes As TwCapability = New TwCapability(TwCap.ICAP_XRESOLUTION, 0, TwType.Fix32)
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.GetCurrent, XRes)
If rc = TwRC.Success Then
Dim inptr As IntPtr = DirectCast(GlobalLock(XRes.Handle), IntPtr)
Dim scanvalue As Integer = Marshal.ReadInt32(inptr, 2)
GlobalUnlock(cap.Handle)
GlobalFree(cap.Handle)
MsgBox(scanvalue)
End If
What I want to be able to do is get the range of values that are possible. I just am unsure how to use the specification to make the datatypes and do the conversions.
If anyone could help I would be extremely appreciated.
James
ps. I dont mind C# code as I can work through that to understand.
|
|
|
|
|
Hallo,
can you say me, how is the syntax for AUTODISCARDBLANKPAGES?
The code:
Dim capDiscardBlank As TwCapability = New TwCapability(TwCap.ICAP_AUTODISCARDBLANKPAGES, 1)
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capDiscardBlank)
has no effect. By using the original driver (guif.ShowUI = 1), the empty sides are removed. My scanner is a CANON DR-2580C document scanner.
There is in the meantime a possibility to makes PDFs?
My code:
Dim img As IntPtr = IntPtr.Zero
Dim st As Integer = GdipCreateBitmapFromGdiDib(bminfo, pixdat, img)
If (st <> 0) Or (Equals(img, IntPtr.Zero)) Then
Return False
End If
picname = "c:\temp\" + picname
st = GdipSaveImageToFile(img, picname, clsid, IntPtr.Zero)
GdipDisposeImage(img)
Return st = 0
Thanks for an answer .
Norbert
|
|
|
|
|