|
Hi,
The Acquire process crashes when a scanner is not connected. How can I prevent this? Any solution ? I tried some error checks, but nothing works
Smitha
Whenever I see an old lady slip and fall on a wet sidewalk, my first instinct is to laugh. But then I think, what if I was an ant, and she fell on me. Then it wouldn't seem quite so funny.
|
|
|
|
|
I have found the TWAIN drivers to be VERY inconsistent with respect to reporting on the availability of a device. I have had that issue corroborated by other people that develop TWAIN drivers. I believe the device detection is an optional feature of the drivers and, therefore, it isn't consistent between drivers.
I have found some drivers that report that the device is connected all the time even when it isn't and I have found drivers that will never report that the device is connected even when it is. Some of the better behaved drivers will pop up a message box telling the user that the device isn't connected then send the CloseRequest message.
For a while I was trying a workaround of watching the messages coming into the callback interface and monitoring how long it had been since the last non-NULL message but the drivers are too inconsistent to make that work. Some drivers constantly send NULL and NOT messages when the device is connected and the user is selecting the image to scan but other drivers don't send back any messages while the user is making the selecting. Some drivers send back no messages at all if the device is disconnected while other drivers send a constant stream of NULL messages when there's no device. I found no consistent way to terminate the acquire based on the messages received without terminating valid acquires for certain cameras (I'm working with web cameras primarily).
I have finally settled on making sure I don't disable my app while waiting for the scan (unlike the sample app). I also avoid initiating scans from modal dialog boxes since that blocks the UI thread and basically locks the app if the driver never returns a message to tell your app that the device isn't connected.
So far this method seems to work OK for my application - the downside is that the user can select a database record, ask to acquire an image, switch back to my app and select a different record, then go back to the TWAIN interface and finish the acquire. The end result is that the acquired image will be applied to the most recently selected record rather than the record that initiated the acquire. I figure if users do that then they deserve to be confused.
If anyone figures out a better solution I really would like to hear about it. Sorry I couldn't be of more help.
Steve
|
|
|
|
|
Hi.
Thanks for the great ap!
I have a semi-related question
When I try to sabve an image which has been opened and displayed in a picturebox back to the same file name, I get ageneric GDI error.
I have disposed all refferences - any ideas?
|
|
|
|
|
I have faced the same problem too. If i try to load an image into a picturebox and then try to save/edit it, I get a gdi error. I then had to make a copy of the file to load into the picturebox, so that I won't have a problem in saving. That workaround was ok for me
Smitha
Whenever I see an old lady slip and fall on a wet sidewalk, my first instinct is to laugh. But then I think, what if I was an ant, and she fell on me. Then it wouldn't seem quite so funny.
|
|
|
|
|
Thanks Smitha.
Yeah - it turns out that a direct "from file" (on the loading side) is the issue. If I just turn it into a filestream to a bitmap and put the bitmap in, life is good.
Merci,
Mike "MC5" C
For every save there is a load, for every problem there is a code... oh hell, that was stupid.
|
|
|
|
|
Hi.
Thanks for the great ap!
I have a semi-related question
|
|
|
|
|
Hello,
First of all I want to thank the author for providing the only one TWAIN-C# article available in the whole Internet.
I want TransferPictures() to give me a bitmap as its result. How will I be able to get it ? Or will I be able to convert the result of TransferPictures() to a bitmap ?
Thanks,
Smitha
It's easy to sit there and say you'd like to have more money. And I guess that's what I like about it. It's easy. Just sitting there, rocking back and forth, wanting that money.
|
|
|
|
|
Kindly Be patient to read other Questions and Answers posted for this article
it's already addressed several times
|
|
|
|
|
I'm developing a student project which makes use a scanner.
i surf to the web over and over and download many codes about handling a scanner.
some of them use Twain, some of them use the kodak image control (i'm developing the project with vb.net)
I'm confused with the difference of TWAIN and Kodak Image Control. What's the difference?
I'm trying to use KIC to handle the scanner without the UI. But I find that I can't micro-control the scanner. Without UI, the scanner always scan as letter size but not A4 size. How can i set the region?
I sincerely hope that someone can give me a hint and, most importantly, how to control the scanning region.
Thousand thx!
|
|
|
|
|
Reply me your e-mail, and I'll give you a advanced version of TWAIN controler.
Hope that can help you.
BTW, where do u live in?
————————————————————————————————————————
Sumtec宇宙技术中心 VB技术支持
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利,并保持和微软所提供的条款一致。具体É
|
|
|
|
|
Hi,
I'm can't set the resolution of the scanner. This is what i'm trying to use:
TwCapability cap2 = new TwCapability(TwCap.ICAP_XRESOLUTION, 0x004b);
rc = DScap( appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, cap2 );
if( rc != TwRC.Success )
{
CloseSrc();
return;
}
This code is in Acquire() after the line:
"rc = DScap( appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, cap );"
As this line works and it's in state 4, my code should work too.
But I always receive "TwRC.Failure" and, in CloseSrc(), I get this message:
"General failure in response to MSG_DISABLEDS. DG DAT MSG is out of expected sequence."
Anybody can help-me please?
Thanks!
Edgar
|
|
|
|
|
Use this:
Resolution = 300 //Resolution in dpi.
// Define this.
ICAP_XRESOLUTION = 0x1118
ICAP_YRESOLUTION = 0x1119.
// Set X resolution.
TwCapability capX = new TwCapability(TwCap.ICAP_XRESOLUTION, Resolution);
rc = DScap( appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capX );
if( rc != TwRC.Success ) {
CloseSrc();
return;
}
// Set Y resolution.
TwCapability capY = new TwCapability(TwCap.ICAP_YRESOLUTION, Resolution);
rc = DScap( appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capY );
if( rc != TwRC.Success ) {
CloseSrc();
return;
}
Anybody know how setting X and Y dimension of rectangle of acquisition?
Thanks
|
|
|
|
|
Thank you for this so useful application...
I would like to use feeder while scanning. How can I use feeder?
|
|
|
|
|
I suggest you to read the TWAIN document. With a bit changes with these sources the author supplied, you can use the feeder while scanning. As I don't need to use feeder, I didn't make my version of twain scanning component have the ability of feeding. So, I'm very sorry to say I can not help you directly with my advanced component. However, my source contained some new structure you may have to use, such as TW_ENUMERATION/ TW_ARRAY/ TW_RANGE, etc.
As a tip, read about the content of "Capabilities" in the TWAIN document. The keywords you need are:
CAP_SUPPORTEDCAPS
CAP_FEEDERENABLE
CAP_FEEDERLOADED
CAP_AUTOFEED
CAP_CLEARPAGE
CAP_REWINDPAGE
DG_CONTROL/ DAT_CAPABILITY/ MSG_GET
DG_CONTROL/ DAT_CAPABILITY/ MSG_SET
TW_ENUMERATION
TW_RANGE
TW_ARRAY
Good luck!
————————————————————————————————————————
Sumtec宇宙技术中心 VB技术支持
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利,并保持和微软所提供的条款一致。具体É
|
|
|
|
|
Hmm.. anyone have more information on this subject? I cannot seem to get the scanners to feed multiple pages, without running a loop checking for out of paper.. Seems to me there should be a way to do it inside of the twain specs..
|
|
|
|
|
|
Take a look at my reply to another thread:
http://www.codeproject.com/dotnet/twaindotnet.asp?msg=923006&target=netmaster#xx923006xx
A simple code change will allow multiple scans.
|
|
|
|
|
Hi,
Is there a way to hide the user interface and scan with predefined settings (resolution, region, color, ...)
Thanks.
|
|
|
|
|
Yes, u can. By reading the document "TWAIN specification", you can find that by fixing the TW_USERINTERFACE.ShowUI to false(short 0), the Source's (the device) predefined UI will not show. This can be found in the author's source code in the form of "guif.ShowUI = 1". By fixing the "1" into "0", you can hide the user interface. Some scanner prevent you from predefining settings of resolution, region, color, etc, if you show the Source's User Interface. However, when you set them, the DSM (Device Source Manager) will report the State "Success". (For example, Acer's scanners do so. In fact, it's the problem of "Mira Scan".)
If what you want is not only to hide the UI but also to set the settings, I suggest you:
1. To read TWAIN's documents which can be downloaded from HTTP://www.twain.org
The title of the document you need is "TWAIN specification version 1.9". I should notice you that the document is very long. It contains 9 chapters and 2 appendixes. And there is still a bit problems in using the TWAIN API in .NET. Maybe you will use two day to find what's wrong and to fix them.
2. Give me your mail address and I will send you my work. It can set resolution, region, color and ShowUI. And I should tell you that I didn't fixed all the problem I met, and even don't know if there is any problem I don't know yet in it. As a part of protection, I won't give all the source codes. However, I will give you the part of TWAIN's like NETMaster did here. And I bug you please, DO NOT fix any thing which completely have nothing to do with scanning, eg: product info.
————————————————————————————————————————
Sumtec宇宙技术中心 VB技术支持
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利,并保持和微软所提供的条款一致。
|
|
|
|
|
Thanks for the c# twain sample program. It saved me at least months of time. Right now I have problem saving image to tiff file with group4 encoding or compression.
I tried to use EncoderParameters with Param[0] = new EncoderParameter( Encoder(Encoder.Compression.Guid), (byte)EncoderValue.CompressionCCITT4 ) in function GdipSaveImageToFile() as the third parameter. But I cannot make it work. Do you have any idea?
Thanks!
Yong
|
|
|
|
|
First of all, thank you for your great works.
I am doing a project with scanner and found your source here. With your help, I've made a simple class for TWAIN scanner. And the class sounds good except ... :
You can't throw any exception correctly! And I don't know if this is my problem. (I guess not.)
By fixing your code at procedure "public void Init( IntPtr hwndp )" without changing any other codes, you can see the problem:
public void Init( IntPtr hwndp )
{
Finish();
TwRC rc = DSMparent( appid, IntPtr.Zero, TwDG.Control, TwDAT.Parent, TwMSG.OpenDSM, ref hwndp );
if( rc == TwRC.Success )
{
rc = DSMident( appid, IntPtr.Zero, TwDG.Control, TwDAT.Identity, TwMSG.GetDefault, srcds );
throw new Exception("Throw any exceptions here will finally popup StackOverflowException but not want you want.");
if( rc == TwRC.Success )
hwnd = hwndp;
else
rc = DSMparent( appid, IntPtr.Zero, TwDG.Control, TwDAT.Parent, TwMSG.CloseDSM, ref hwndp );
}
}
As I trace the disassembly code, I found that the exception object can be generate correctly but after that, when throwing it, the code will unexpectly generate another exception "NullReferenceException". What's more interesting is, even the "NullReferenceException" can't be thrown -- when throwing the "Null...Exception" the code will generate a new "Null...Exception". This causes the stack overflowed and finally generate an exception "StackOverflowException".
My questions are:
1. What causes this? Especially, why you can throw an exception normally before requesting TwMessage.GetDefault?
2. How to avoid this? I mean how to make throwing exception correctly?
'Cause I want to use exceptions to tell user the problem.
And another reason, if some other unexpecting problems happen, like "Divided by zero" or "ArgumentException", which are not the programmer's willing, it will shutdown the program without showing the correct information. This make my debugging extremely difficult.
Lately I found another thing: If you put the exception-throwing-code or the problem-generating-code(I mean the codes generate exception not by my willing) into a try...catch structure, you will find that you can catch an exception instead of poping up an "Null...Exception" or an "Stack...Exception". But unfortunately, you will find that the exception you catch is not what really causes the problem. For example:
During the TransferPics stage, I convert the DIB into a Bitmap and save into disk. But if the file I want to save is occupied by another object like 'pictureBox1.Image = Image.FromFile(@"C:\temp.bmp");', the system should throw an exception to indicate that the file is occupied. Disappointing me, the system throws an exception called "General GDI+ Exception" instead of what I want.
I don't know if this information can help you to solve the problem.
I guess if there is some problem with interoperation? Because the only different between TwMessage.OpenDSM and TwMessage.GetDefault is the DSM will fill some info into the parameter/variable "srcds".
Expecting for any one's answer, especially NETMaster's.
————————————————————————————————————————
Sumtec宇宙技术中心 VB技术支持
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利,并保持和微软所提供的条款一致。
|
|
|
|
|
Null reference.
————————————————————————————————————————
Sumtec宇宙技术中心 VB技术支持
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利,并保持和微软所提供的条款一致。具体É
|
|
|
|
|
I seem to be running into this, or very similar, issue.
As a sample (which I accept is what the provided code is) it runs fine, and even with a slight expansion, it seems to run fine.
Bt when I start to add calls to servers and the like, it falls over, always in the same place, when I'm starting the acquire method.
The message is always that there's a null reference of an object exception, but no hint whatsoever as to where the null refeence is, or what the object might be.
Everything in the current module appears to be fine.
This looks to me like some form of memory leakage issue, and, if I slowly step through the code in the debugger, it can go away. I've tried implementing some GC calls, but with no satisfaction.
Does anybody have any clues on this?
TIA.
g.
Gary Stark
gstark@RedbacksWeb.com
http://www.RedbacksWeb.com
|
|
|
|
|
I had similar problems. I believe that the leak is related to the Acquire dialog. I modified the sample to scan into the file without opening the dialog and it always works fine. If I open the Acquire dialog, it scans OK, but after using application for a while I usually get some resource contamination and NullReferenceException thrown from a text box...
|
|
|
|
|
I am trying to port this to VB.NET, but am having trouble with the message handling. Is there a simple way to just compile this into a DLL and then use it in VB.NET?
|
|
|
|
|