twain / twain-cs Goto Github PK
View Code? Open in Web Editor NEWA C# interface for TWAIN
A C# interface for TWAIN
Reported by neticous on 2015-10-13 20:12 UTC
I have the latest code and built it.
I changed the ICAP_XFERMECH to native 0.
There is a memory corruption with some scanners.
When I run the software it works fine until you close and then open the same or other.
I am using the Fujitsu fi-7160 and the the Xerox Documate 3220 both exhibit this problem .
To reproduce to problem:
Run TWAIN for C-Sharp preferably from within devstudio 2010-15
Thanks
neticous
Reported by mcdon on 2014-10-27 20:35 UTC
In DsmMemFree (under "// Validate..."):
if (a_intptrHandle != IntPtr.Zero) return;
probably meant to be
if (a_intptrHandle == IntPtr.Zero) return;
to exit early if the pointer is null
If you look at the function TWAIN::NativeToBitmap
, you will find the following code:
// So we make a copy (ick)...
Bitmap bitmap;
switch (bitmapinfoheader.biBitCount)
{
default:
case 24:
bitmap = bitmapStream.Clone(new Rectangle(0, 0, bitmapStream.Width, bitmapStream.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
break;
case 8:
bitmap = bitmapStream.Clone(new Rectangle(0, 0, bitmapStream.Width, bitmapStream.Height), System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
break;
case 1:
bitmap = bitmapStream.Clone(new Rectangle(0, 0, bitmapStream.Width, bitmapStream.Height), System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
break;
}
// Fix the resolution...
bitmap.SetResolution((int)(bitmap.HorizontalResolution + 0.5), (int)(bitmap.VerticalResolution + 0.5));
As you can tell by the comments, it looks like the intention here is to make sure the Bitmap
has the correct PixelFormat
. However, from my testing, it looks like the Bitmap
in the variable bitmapStream
that is created from the byte[]
already has the correct PixelFormat
for what was selected (full color [PixelFormat.Format24bppRgb
], grayscale [PixelFormat.Format8bppIndexed
], or black and white [PixelFormat.Format1bppIndexed
]).
Is there a reason that the PixelFormat
would come back incorrect that the copy is necessary? Or is this a relic of something that was not handled in C++ but is handled in the .NET Framework (GDI+)?
If you look in the method TWAIN::DatIdentity
, you will find that if you are using callbacks that the logging uses the same message passed in instead of using MSG.REGISTER_CALLBACK
. Here is the offending line of code:
Log.LogSendBefore(a_dg.ToString(), DAT.CALLBACK.ToString(), a_msg.ToString(), CallbackToCsv(twcallback));
Just need to replace a_msg.ToString()
with MSG.REGISTER_CALLBACK.ToString()
. Pretty trivial, but it makes me look twice when looking at the log file.
when I try twaincsscan this message com:
Unable to start, the most likely reason is that TWAIN Data Source Manager is not installed on your system.
An internet search for 'TWAIN DSM' will locat it and once installed, you shoul be able to proceed.
You can also try the following link:
http://sourceforge.net/projects/twain-dsm/
I have a program in C# that accesses TwainDSM via twain-cs. I’m scanning with a Kodak i1440 scanner. When I scan 8.5×11 or 8.5×14 pages, it works fine and reliably. The i1440’s automatic page size detection. But when I scan a cashier coupon (long and skinny) my program simply crashes – no error message, no stack trace.
In twaindsm.log, the last message is as follows, which is pretty useless...
[125757452 dsm.cpp 2368 0 000015B4] Quaero Scanner Workstation -> KODAK Scanner: i1410/i1420/i1440
[125757452 dsm.cpp 2373 0 000015B4] DG_CONTROL/DAT_CAPABILITY/MSG_GETCURRENT/ICAP_XFERMECH
[125757452 dsm.cpp 2432 0 000015B4] TWRC_SUCCESS TWON_ONEVALUE
[125757453 dsm.cpp 2368 0 00001DC0] Quaero Scanner Workstation -> KODAK Scanner: i1410/i1420/i1440
[125757453 dsm.cpp 2377 0 00001DC0] DG_IMAGE/DAT_IMAGENATIVEXFER/MSG_GET
[125757605 dsm.cpp 2432 0 00001DC0] TWRC_XFERDONE
TwainDSM 2.4.2
Same problem happening with a Fujitsu fi-6130Z
A scan with a custom size of 4.2×11 (inches) works
A scan with a custom size of 4.125×11 (inches) crashes.
Hi,
how to check if the scanner has a flatbed or no ?
Can you clearly state with a LICENSE.md what the licensing is for twain-cs
In my mind, opening a connection to the TWAIN DSM is similar to the concept of opening a connection to a SQL database, meaning it is a resource that should probably implement IDisposable
. Neither TWAINWorkingGroup.TWAIN
nor TWAINWorkingGroupToolkit.TWAINCSToolkit
implement the IDisposable pattern. I would think at least the main TWAIN
class would implement it.
IsMsgDeviceEvent never returns true
Reported by mcdon on 2014-10-18 17:36 UTC
in TWAIN.cs, If a_msg is OPENDSM, then transitions to state S3. if a_msg is CLOSEDS, then transitions to state S2. looks like a typo, and should be if a_msg is CLOSEDSM, then transition to state S2. see attached patch.
Reported by arykst on 2015-10-19 08:36 UTC
Hi there!
I am trying to make twain cs samples work on mac os 10.8.5, using mono and Epson DS-560. I managed to make it work on windows, but merely the same steps don't work out for Mac. twain lifecycle seems to be fine . I am going open DSM -> open DS -> Enable DS. Enable DS returns success status and literally nothing happens. By this point on win machine i get scanner feeder work, and document gets scanned, but on mac it doesn't. And another thing is if i send enableds with show ui flag being true, i am seeing native epson scan window and if click scan button on this window, feeder is starting. I am really stuck at this point, i was trying to follow call stack and stoped at native call to twain.framework still returning success status. Am i doing something wrong?
Reported by mcdon on 2014-01-08 15:49 UTC
ran twaincsscan.exe and got FileNotFoundException "Could not load file or assembly 'TWAINCSToolkit'
Reported by vin-finn on 2014-02-20 21:47 UTC
Love this module. I'm using it in a Web based scanner service. I needed to modify ShowImage to send the STS param so that I would know the reason for the call into ShowImage when the bitmap is null. In my showimage callback I keep track of numpages scanned and if the a_bitmap is null, numpages is 0, sts != TWAINCSToolkit.STS.SUCCESS I generate an error message based on:
if (sts == TWAINCSToolkit.STS.CANCEL)
{
TS_ProgressMsg = "Paper not loaded, job cancelled, please load paper";
}
else
{
TS_ProgressMsg = "Job terminated with error: "+sts.ToString();
}
If numpages is >0 then I show a cancel message and I post the sts.ToString().
In addition to STS it would be very useful to know the image type that has been stored. I know where the images are stored since I set that location but the scanned image can be TIF, JPG or BMP, and I have to look in the folder to figure that out. It would be useful to be told the image type via a System.Drawing.Imaging.ImageFormat type of parameter.
I know I can take the bitmap, but I don't use that bitmap since I can't display the image at the time of the callback. The GUI has to query for status and upon discovery of a new image call for the image. So the ShowImage callback triggers the event, and later I access the image via the stored image. If it is appropriate to have two showimage types, one as an event without the image as a parameter and more notification information on just what was stored, that would be preferable. I extract metadata from the images and it would be very nice to be provided that metadata at callback time, rather than the image itself.
Thank you for such a great library.
Vinnie
Reported by tanveershahpk on 2014-11-16 05:55 UTC
sts = m_twaincstoolkit.Send("DG_CONTROL", "DAT_CAPABILITY", "MSG_SET", ref szCapability, ref szStatus);
above line of code returning BADVALUE, i am executing code on windows 32 bit machine, kindly let me solve this issue. Thanks
although TWAC_32 is running perfectly on my machine
hello! i'm a develop from chinese.i want to know how to use this ,do u have a document of it about how to use this for scanner. And how can i use this without defaut scanner parameter form.The another question is How to use scanner to sweep paper continuously.Much obliged for your assistance
Reported by mwlo on 2015-09-09 11:53 UTC
Hi,
We would like to use your library. For an easier usage, it would be nice if you provide a nuget package on http://nuget.org.
Regards
Marco
I have done a lot of debugging of TWAINWorkingGroupToolkit.TWAINCSToolkit::ScanCallback
. No matter what I did in my custom form or using the sample applications, I always would get a SEQERR. For native image transfer, it would call into ScanCallback at these places:
ScanCallback: 006
(successful transfer of bitmap)ScanCallback: 052
(Failed to get image info)Looking at the TWAIN 2.4 Specification on page 3-20, the DG_IMAGE / DAT_IMAGEINFO / MSG_GET
and DG_IMAGE / DAT_IMAGELAYOUT / MSG_GET
should only get called in TWAIN State 6 before the image transfer. However, looking at ScanCallback
, it appears that this is always attempting to happen after the image transfer, which is causing the SEQERR.
I'm not sure how long this has been an issue, but it basically causes the toolkit to not be usable at all, making C# developers use the TWAINWorkingGroup.TWAIN
class. This is probably okay in the long-run for developers, but it is extremely frustrating when trying to figure out the finer parts of TWAIN and when stuff should happen.
Reported by propperdx on 2015-11-27 14:23 UTC
With enabled userinterface the userinterface freezes if you try to scan the second time. I tried two different scanners.
e.g. twaincsscan.exe, TWAINCS Version 02.03.02.00
Reported by mcdon on 2014-01-08 15:49 UTC
ran twaincsscan.exe and got FileNotFoundException "Could not load file or assembly 'TWAINCSToolkit'
Hai ,
We bought epson plq 22cs passbook printer , we trying to integrate with our dotnet application using twain-cs.
requirement
From the above requirement, we have done the steps 1 and 3.But print is not happening.We tried with CAP_PRINTERSTRING,CAP_PRINTERSUFFIX messages .We need your support to solve this issue.
Reported by jhmgbl on 2015-08-26 17:46 UTC
Scanning app crashes after scan with Twain Sample Scanner. My Scanners (Nikon Coolscan IV and Microsoft Webcam live) don't work with this library.
Related to this issues: #39
Some scanners have only 32 bit drivers available. So for them I must use 32 bit DSM (correct me if I'm wrong)
And to use 32 bit DSM I need to set platform target to x86 (as said in related issue)
Is it possible to use 32 bit DSM if platform target isn't x86?
My goal is to create an interface where user would select which DSM he wants to use (64 bit or 32 bit version) and then list all drivers (Data Sources) for that version.
Is this possible?
Reported by vin-finn on 2014-02-20 21:47 UTC
Love this module. I'm using it in a Web based scanner service. I needed to modify ShowImage to send the STS param so that I would know the reason for the call into ShowImage when the bitmap is null. In my showimage callback I keep track of numpages scanned and if the a_bitmap is null, numpages is 0, sts != TWAINCSToolkit.STS.SUCCESS I generate an error message based on:
if (sts == TWAINCSToolkit.STS.CANCEL)
{
TS_ProgressMsg = "Paper not loaded, job cancelled, please load paper";
}
else
{
TS_ProgressMsg = "Job terminated with error: "+sts.ToString();
}
If numpages is >0 then I show a cancel message and I post the sts.ToString().
In addition to STS it would be very useful to know the image type that has been stored. I know where the images are stored since I set that location but the scanned image can be TIF, JPG or BMP, and I have to look in the folder to figure that out. It would be useful to be told the image type via a System.Drawing.Imaging.ImageFormat type of parameter.
I know I can take the bitmap, but I don't use that bitmap since I can't display the image at the time of the callback. The GUI has to query for status and upon discovery of a new image call for the image. So the ShowImage callback triggers the event, and later I access the image via the stored image. If it is appropriate to have two showimage types, one as an event without the image as a parameter and more notification information on just what was stored, that would be preferable. I extract metadata from the images and it would be very nice to be provided that metadata at callback time, rather than the image itself.
Thank you for such a great library.
Vinnie
Hi
I get a "There are no Twain drivers installed on this system" error from the FormScan.cs.
Anyone who can hint me to why I get this error?
Thanks
(I have run other Twain test projects and they do find my scanners, so I know I have Twain compatible scanners.)
(I'm a casual programmer with basic knowledge in .net so I might be missing some simple point here:-)
Most of the scanned images are distorted
Reported by tanveershahpk on 2014-11-19 05:55 UTC
sts = m_twaincstoolkit.Send("DG_CONTROL", "DAT_CAPABILITY", "MSG_SET", ref szCapability, ref szStatus);
if (sts != TWAINCSToolkit.STS.SUCCESS)
above line of code is returning the BADVALUE, while scanning is working fine by Twack_32 TOOL
Reported by mcdon on 2014-11-20 17:12 UTC
I suggest organizing the projects under a single solution. I think the projects are easier to work with when organized under a single solution. Some dll references would have to be replaced with project references.
There's alot of noise in the attached patch, but it moves some projects and code files to different folders, updates a solution to include all projects, and changes some dll references to project references.
Reported by diegot1106 on 2016-01-19 16:57 UTC
The **DatImagenativexferWindowsTwainDsm() **method in the **TWAIN **class makes a call to WindowsTwaindsmDsmEntryImagenativexfer that make the amount of ram to increase a lot. I scan in duplex mode (about 30 to 50 sheets/papers) and I always run out of memory.
Additional Details:
The method WindowsTwaindsmDsmEntryImagenativexfer is an external method located in "twaindsm.dll" so I don't know how to proceed and fix it.
When I open the scan app, twaincsscan, and try to create new driver settings by clicking on the "Change driver settings (press this first)..." button, or try to do a scan, I get the following error:
'This application failed to start because it could not find or load the Qt platform "windows"
in "".
Reinstalling the application may fix this problem.'
How can this be solved?
Reported by mcdon on 2014-01-08 15:49 UTC
ran twaincsscan.exe and got FileNotFoundException "Could not load file or assembly 'TWAINCSToolkit'
Some of the images I scan end up somehow corrupted - they are tilted to the side as if the pixels ware alligned incorrectly.
I'm scanning using Fujitsu fi 7180 using adf and duplex, during one scanning sessions some of the pictures come out good, but some are tilted.
Do You have any idea what could be the couse and where should i look for solution of this problem ?
Reported by ahmadalqadi on 2015-10-08 07:04 UTC
How can i send parameters dpi , page size , source type(ADF,FlatBed).
Reported by propperdx on 2015-12-02 11:45 UTC
I found a memory overflow becouse of a missing Dispose()
Method ScanCallback in TWAINCSToolkit.
~Line 1854
msgPendingxfers = ReportImage("ScanCallback: 010", TWAIN.DG.IMAGE.ToString(), TWAIN.DAT.IMAGEFILEXFER.ToString(), TWAIN.MSG.GET.ToString(), CvtSts(sts), bitmap, szFilename, m_twain.ImageinfoToCsv(twimageinfo), null, 0);
bitmap.Dispose(); // !!!
bitmap = null;
Thanks
Tobias
I am trying to use this code on MacOS but not able to figure the correct steps to use. I am able to successfully build and run twaincsscan project code and after running not able to perform any action. Can anyone help me with this?
PS: I am using Macbook Pro 16-inch 2019, 16GB DDR4 Ram, macOD Catalina.
Reported by ad-viper on 2014-08-01 15:20 UTC
Hi Mark,
i've tried to run your sample "twaincsscan.exe" and to scan with it a document, but it's not running. I can't set the options - i'm only able to set a save directory and when i press "scan" the spezific scan-window of the data source is popping up and the whole programm freezes.
I didn't realized that not even the sample is running... Can you please give me some advice how to make it running?
I've tried to implement some scan functions but failed because i don't have a clue how to get the capabilities and how to handle the alrounder "a_szMemref".
Every Get request regarding Capabilities returns something like TW_12314 <- "TW_" + random number - i'm pretty sure it's an adress.
How should i handle this?
How do i get my Picture out of a_szMemref - is it an adress - if yes: how do i convert it into an Int pointer? If this isn't possible then what should i do to get my picture out of it?
a_szMemref = "0,0";
sts = twToolKit.Send("DG_CONTROL", "DAT_USERINTERFACE", "MSG_ENABLEDS", ref a_szMemref, ref status);
Best regards
DonkyKong
Can't use demo project for scanning. Windows Scan from Micorosft Store and Twacker can use my HP LJ M1530 Scan scanner/printer, but TWAINCSScan produces log file below. Driver's GUI is shown, and I can initiate scanning, but when I click on Done (in driver's GUI) method NativeMethods.WindowsTwaindsmDsmEntryUserinterface returns FAILURE. Help..
000000 172336076056 S1 T00000001 V. ts:2020-06-09 17:23:36.078050 os:windows
000001 172336079050 S1 T00000001 V. TWAINCSScan v2.4.10.0
000002 172337448199 S1 T00000001 V.
000003 172337449196 S1 T00000001 V. twn> DG_CONTROL/DAT_PARENT/MSG_OPENDSM
000004 172337507043 S1 T00000001 V. twn> SUCCESS
000005 172337510035 S3 T00000001 V.
000006 172337511032 S3 T00000001 V. twn> DG_CONTROL/DAT_IDENTITY/MSG_GETDEFAULT
000007 172337514025 S3 T00000001 V. twn> 2,1,0,ENG,USA,1.0,1,9,0x3,Hewlett-Packard,HP LJ M1530 Scan,HP LJ M1530 Scan TWAIN
000008 172337515022 S3 T00000001 V. twn> SUCCESS
000009 172337515022 S3 T00000001 V.
000010 172337516020 S3 T00000001 V. twn> DG_CONTROL/DAT_IDENTITY/MSG_GETFIRST
000011 172337516020 S3 T00000001 V. twn> 1,6,4,ENGLISH_USA,USA,6.4.63.22930,2,2,0x40000003,EPSON,EPSON Scan 2,EPSON DS-310
000012 172337517015 S3 T00000001 V. twn> SUCCESS
000013 172337517015 S3 T00000001 V.
000014 172337518013 S3 T00000001 V. twn> DG_CONTROL/DAT_IDENTITY/MSG_GETNEXT
000015 172337518013 S3 T00000001 V. twn> 2,1,0,ENG,USA,1.0,1,9,0x3,Hewlett-Packard,HP LJ M1530 Scan,HP LJ M1530 Scan TWAIN
000016 172337518013 S3 T00000001 V. twn> SUCCESS
000017 172337519011 S3 T00000001 V.
000018 172337519011 S3 T00000001 V. twn> DG_CONTROL/DAT_IDENTITY/MSG_GETNEXT
000019 172337519011 S3 T00000001 V. twn> 2,1,0,ENG,USA,1.0,1,9,0x3,Hewlett-Packard,HP LJ M1530 Scan,HP LJ M1530 Scan TWAIN
000020 172337520008 S3 T00000001 V. twn> ENDOFLIST
000021 172338855683 S3 T00000001 V.
000022 172338856681 S3 T00000001 V. twn> DG_CONTROL/DAT_IDENTITY/MSG_SET
000023 172338856681 S3 T00000001 V. twn> 2,1,0,ENG,USA,1.0,1,9,0x3,Hewlett-Packard,HP LJ M1530 Scan,HP LJ M1530 Scan TWAIN
000024 172338857678 S3 T00000001 V. twn> SUCCESS
000025 172338857678 S3 T00000001 V.
000026 172338857678 S3 T00000001 V. twn> DG_CONTROL/DAT_IDENTITY/MSG_OPENDS
000027 172338858675 S3 T00000001 V. twn> 2,1,0,ENG,USA,1.0,1,9,0x3,Hewlett-Packard,HP LJ M1530 Scan,HP LJ M1530 Scan TWAIN
000028 172339066123 S3 T00000001 V. twn> 2,1,0,ENG,USA,1.0,1,9,0x3,Hewlett-Packard,HP LJ M1530 Scan,HP LJ M1530 Scan TWAIN
000029 172339066123 S3 T00000001 V. twn> SUCCESS
000030 172339092053 S4 T00000001 V.
000031 172339093097 S4 T00000001 V. twn> DG_CONTROL/DAT_CAPABILITY/MSG_SET
000032 172339093097 S4 T00000001 V. twn> ICAP_XFERMECH,TWON_ONEVALUE,TWTY_UINT16,TWSX_MEMORY
000033 172339094048 S4 T00000001 V. twn> ICAP_XFERMECH,TWON_ONEVALUE,TWTY_UINT16,TWSX_MEMORY
000034 172339095047 S4 T00000001 V. twn> SUCCESS
000035 172339096086 S4 T00000001 V.
000036 172339097040 S4 T00000001 V. twn> DG_CONTROL/DAT_CAPABILITY/MSG_SET
000037 172339097040 S4 T00000001 V. twn> CAP_INDICATORS,TWON_ONEVALUE,TWTY_BOOL,TRUE
000038 172339098038 S4 T00000001 V. twn> CAP_INDICATORS,TWON_ONEVALUE,TWTY_BOOL,TRUE
000039 172339098038 S4 T00000001 V. twn> SUCCESS
000040 172339162863 S4 T00000001 V.
000041 172339163862 S4 T00000001 V. twn> DG_CONTROL/DAT_CAPABILITY/MSG_GETCURRENT
000042 172339163862 S4 T00000001 V. twn> CAP_CUSTOMDSDATA,0,0
000043 172340755155 S4 T00000001 V. twn> FAILURE/CAPUNSUPPORTED
000044 172349683575 S4 T00000001 V.
000045 172349684573 S4 T00000001 V. twn> DG_CONTROL/DAT_USERINTERFACE/MSG_ENABLEDS
000046 172349684573 S4 T00000001 V. twn> TRUE,FALSE,1443224
000047 172408083210 S4 T00000001 V. twn> FAILURE/65536
Reported by mlmcl on 2016-09-02 18:29 UTC
TWAIN strings (TW_STR32, TW_STR64, etc) don't correctly handle codepages on Windows, which causes problems when the contents pf those strings are localized. The code was using UTF8 encoding on Windows, and it was using strings when it should have been using byte arrays...
Compress-Archive : A positional parameter cannot be found that accepts argument
1> '2019\Projects\twain-cs-master\twaincs\source\twaincscert\source\data'.
1> At line:1 char:1
1> + Compress-Archive -Force D:\Users\Administrator\Documents\Visual Studi ...
1> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1> + CategoryInfo : InvalidArgument: (:) [Compress-Archive], Paramet
1> erBindingException
1> + FullyQualifiedErrorId : PositionalParameterNotFound,Compress-Archive
1>
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(1330,5): error MSB3073: 命令“call "D:\Users\Administrator\Documents\Visual Studio 2019\Projects\twain-cs-master\twaincs\source\twaincscert\source\version.bat"
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(1330,5): error MSB3073: XCOPY "D:\Users\Administrator\Documents\Visual Studio 2019\Projects\twain-cs-master\twaincs\source\twaincscert\source\data" "D:\Users\Administrator\Documents\Visual Studio 2019\Projects\twain-cs-master\twaincs\source\twaincscert\source\bin\AnyCPU\Debug\data" /i /e /y /q
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(1330,5): error MSB3073: cd "D:\Users\Administrator\Documents\Visual Studio 2019\Projects\twain-cs-master\twaincs\source\twaincscert\source\bin\AnyCPU\Debug\data.."
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(1330,5): error MSB3073: powershell Compress-Archive -Force "D:\Users\Administrator\Documents\Visual Studio 2019\Projects\twain-cs-master\twaincs\source\twaincscert\source\data" "D:\Users\Administrator\Documents\Visual Studio 2019\Projects\twain-cs-master\twaincs\source\twaincscert\source\Properties/data.zip"” quit,code 1。
========== ==========
I used the powershell command line and ran the error script that was prompted by vs. It was successfully executed.
I don't know where the problem is.
Please help me.
Reported by vin-finn on 2014-02-20 21:47 UTC
Love this module. I'm using it in a Web based scanner service. I needed to modify ShowImage to send the STS param so that I would know the reason for the call into ShowImage when the bitmap is null. In my showimage callback I keep track of numpages scanned and if the a_bitmap is null, numpages is 0, sts != TWAINCSToolkit.STS.SUCCESS I generate an error message based on:
if (sts == TWAINCSToolkit.STS.CANCEL)
{
TS_ProgressMsg = "Paper not loaded, job cancelled, please load paper";
}
else
{
TS_ProgressMsg = "Job terminated with error: "+sts.ToString();
}
If numpages is >0 then I show a cancel message and I post the sts.ToString().
In addition to STS it would be very useful to know the image type that has been stored. I know where the images are stored since I set that location but the scanned image can be TIF, JPG or BMP, and I have to look in the folder to figure that out. It would be useful to be told the image type via a System.Drawing.Imaging.ImageFormat type of parameter.
I know I can take the bitmap, but I don't use that bitmap since I can't display the image at the time of the callback. The GUI has to query for status and upon discovery of a new image call for the image. So the ShowImage callback triggers the event, and later I access the image via the stored image. If it is appropriate to have two showimage types, one as an event without the image as a parameter and more notification information on just what was stored, that would be preferable. I extract metadata from the images and it would be very nice to be provided that metadata at callback time, rather than the image itself.
Thank you for such a great library.
Vinnie
Hi,i am confused about some twain sources in use of memory transfer with type compression,such as CCITT GROUP4(for B&W image),what didthe datasources transfered? is all of the compressed data whitout the file header? hope you could explain for me,many thanks!
Reported by jologi on 2014-08-21 07:09 UTC
Hello!
I've tried out twain for c# in the actual version and find out, that the setup-dialog for the scanner (tested with canon and panasonic) freezes when called. It seems that the twain-communication-Thread (function main in TWAIN.cs) jumps in "CallerToThreadWaitOne()" and does not return. If I understood the behavior correctly, than at this point its not time for the application to send something to twain, instead the user must do something in the dialog so the application should "wait" on reaction from twain.
Best regards
Jo
Reported by ishwer on 2015-05-15 12:43 UTC
It showing an error message "There are no TWAIN drivers installed on this system", i also installed Twain DSM (Ver. 2.3.0, Last updated 26/05/2014) and Twain Drivers.
but other Twain based applications are working. I'm closing the ticket because I'm confident that's the problem. Feel free to comment if things still don't work...
I'm using this c# code to get start with the twain. the sample run as expected but if a paperjam occurred the app that I'm using this C# code in it do nothing. How I can handle this issue to let my app alert the user to correct the jam and continue with scanning image in feeder.
Can I expect a quick fix?
I got this exception when I select the duplex feed with the remove blank pages ?
why this could happen ?
I just cloned the repo and the first thing I've tried was to run the twaincsscan.exe.
When I click "Open" I get the following error:
000000 170320678151 S1 T00000001 V. ts:2020-08-08 17:03:20.691174 os:windows
000001 170320691174 S1 T00000001 V. TWAINCSScan v2.4.11.0
000002 170323376261 S1 T00000001 V.
000003 170323376261 S1 T00000001 V. twn> DG_CONTROL/DAT_PARENT/MSG_OPENDSM
000004 170323389259 S1 T00000001 VE crash - Unable to load DLL 'twaindsm.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
000005 170323390270 S1 T00000001 V. twn> FAILURE/BUMMER
I was able to fix this by copying the twain-dsm/Releases/dsm_020403/windows/64/TWAINDSM.dll to the exe folder.
I wonder why this is not documented anyhow, e.g. in TWAIN CS.rtf.
I copied the zip file from github. Tried to rebuild the entire solution. Good except for one error
Severity Code Description Project File Line Suppression State Error The command "call "C:\Users\grimesr\workspace\Github\Apex Camera\twain driver\twaincs\source\twaincscert\source\version.bat" XCOPY "C:\Users\grimesr\workspace\Github\Apex Camera\twain driver\twaincs\source\twaincscert\source\data" "C:\Users\grimesr\workspace\Github\Apex Camera\twain driver\twaincs\source\twaincscert\source\bin\AnyCPU\Debug\data" /i /e /y /q cd "C:\Users\grimesr\workspace\Github\Apex Camera\twain driver\twaincs\source\twaincscert\source\bin\AnyCPU\Debug\data\.." powershell Compress-Archive -Force "C:\Users\grimesr\workspace\Github\Apex Camera\twain driver\twaincs\source\twaincscert\source\data" "C:\Users\grimesr\workspace\Github\Apex Camera\twain driver\twaincs\source\twaincscert\source\Properties/data.zip"" exited with code 1. twaincscert
Ideas how to fix?
Reported by neticous on 2015-10-13 20:27 UTC
Assuming // Windows uses a DIB, the first usigned short is 40...
if (u16Magic == 40)
Is not exactly true.
For instance the Canoscan Lide 90, 100, 200 sends a DIB back but yoour code does not recognize it and sends back a null bitmap.
I added an else to the NativeTobitmap method inorder for this to work.
I also cleaned up a leak in the code that assumes if (u16Magic == 40).
I have pasted the code below.
You can diff to see the changes.
I hope it is OK to paste code here.
///
// Windows uses a DIB, the first usigned short is 40...
if (u16Magic == 40)
{
byte[] bBitmap;
BITMAPFILEHEADER bitmapfileheader;
BITMAPINFOHEADER bitmapinfoheader;
// Our incoming DIB is a bitmap info header...
bitmapinfoheader = (BITMAPINFOHEADER)Marshal.PtrToStructure(a_intptrNative, typeof(BITMAPINFOHEADER));
// Build our file header...
bitmapfileheader = new BITMAPFILEHEADER();
bitmapfileheader.bfType = 0x4D42; // "BM"
bitmapfileheader.bfSize
= (uint)Marshal.SizeOf(typeof(BITMAPFILEHEADER)) +
bitmapinfoheader.biSize +
(bitmapinfoheader.biClrUsed * 4) +
bitmapinfoheader.biSizeImage;
bitmapfileheader.bfOffBits
= (uint)Marshal.SizeOf(typeof(BITMAPFILEHEADER)) +
bitmapinfoheader.biSize +
(bitmapinfoheader.biClrUsed * 4);
// Copy the file header into our byte array...
IntPtr intptr = Marshal.AllocHGlobal(Marshal.SizeOf(bitmapfileheader));
Marshal.StructureToPtr(bitmapfileheader, intptr, true);
bBitmap = new byte[bitmapfileheader.bfSize];
Marshal.Copy(intptr, bBitmap, 0, Marshal.SizeOf(bitmapfileheader));
Marshal.FreeHGlobal(intptr);
intptr = IntPtr.Zero;
// Copy the rest of the DIB into our byte array......
Marshal.Copy(a_intptrNative, bBitmap, Marshal.SizeOf(typeof(BITMAPFILEHEADER)), (int)bitmapfileheader.bfSize - Marshal.SizeOf(typeof(BITMAPFILEHEADER)));
Bitmap bitmap = null;
// Now we can turn the in-memory bitmap file into a Bitmap object...
using (MemoryStream memorystream = new MemoryStream(bBitmap))
{
// Unfortunately the stream has to be kept with the bitmap...
using (Bitmap bitmapStream = new Bitmap(memorystream))
{
// So we make a copy (ick)...
bitmap = new Bitmap(bitmapStream);
bitmapStream.Dispose();
}
// Cleanup...
//bitmapStream.Dispose();
memorystream.Close();
//bitmapStream = null;
//memorystream = null;
}
bBitmap = null;
// Return our bitmap...
return (bitmap);
}
// Linux and Mac OS X use TIFF. We'll handle a simple Intel TIFF ("II")...
else if (u16Magic == 0x4949)
{
int iTiffSize;
ulong u64;
ulong u64Pointer;
ulong u64TiffHeaderSize;
ulong u64TiffTagSize;
byte[] abTiff;
TIFFHEADER tiffheader;
TIFFTAG tifftag;
// Init stuff...
tiffheader = new TIFFHEADER();
tifftag = new TIFFTAG();
u64TiffHeaderSize = (ulong)Marshal.SizeOf(tiffheader);
u64TiffTagSize = (ulong)Marshal.SizeOf(tifftag);
// Find the size of the image so we can turn it into a memory stream...
iTiffSize = 0;
tiffheader = (TIFFHEADER)Marshal.PtrToStructure(a_intptrNative, typeof(TIFFHEADER));
for (u64 = 0; u64 < 999; u64++)
{
u64Pointer = (ulong)a_intptrNative + u64TiffHeaderSize + (u64TiffTagSize * u64);
tifftag = (TIFFTAG)Marshal.PtrToStructure((IntPtr)u64Pointer, typeof(TIFFTAG));
// StripOffsets...
if (tifftag.u16Tag == 273)
{
iTiffSize += (int)tifftag.u32Value;
}
// StripByteCounts...
if (tifftag.u16Tag == 279)
{
iTiffSize += (int)tifftag.u32Value;
}
}
// No joy...
if (iTiffSize == 0)
{
return (null);
}
// Copy the data to our byte array...
abTiff = new byte[iTiffSize];
Marshal.Copy(a_intptrNative, abTiff, 0, iTiffSize);
// Move the image into a memory stream...
MemoryStream memorystream = new MemoryStream(abTiff);
// Turn the memory stream into an in-memory TIFF image...
Image imageTiff = Image.FromStream(memorystream);
// Convert the in-memory tiff to a Bitmap object...
Bitmap bitmap = new Bitmap(imageTiff);
// Cleanup...
abTiff = null;
memorystream = null;
imageTiff = null;
// Return our bitmap...
return (bitmap);
}
else
{
IntPtr dibhand;
IntPtr bmpptr;
IntPtr pixptr;
dibhand = a_intptrNative;
if (dibhand != IntPtr.Zero)
{
bmpptr = GlobalLock(dibhand);
pixptr = GetPixelInfo(bmpptr);
IntPtr img = IntPtr.Zero;
int st = GdipCreateBitmapFromGdiDib(bmpptr, pixptr, ref img);
if ((st != 0) || (img == IntPtr.Zero))
return (null);
MethodInfo mi = typeof(Bitmap).GetMethod("FromGDIplus", BindingFlags.Static | BindingFlags.NonPublic);
Bitmap bmp = null;
if ((st == 0) && (img != IntPtr.Zero)) // success
bmp = (Bitmap)mi.Invoke(null, new object[] { img });
//GlobalFree(dibhand);
GlobalUnlock(dibhand);
dibhand = IntPtr.Zero;
Bitmap b = new Bitmap(bmp);
bmp.Dispose();
bmp = null;
return b;
}
}
// Uh-oh...
return (null);
}
Thanks
neticous
PS: you all have done some very handle work here. Thank you.
public IntPtr DsmMemAlloc(uint a_u32Size)
{
IntPtr intptr;
// Use the DSM...
...
// Do it ourselves, Windows...
if (ms_platform == Platform.WINDOWS)
{
intptr = (IntPtr)NativeMethods.GlobalAlloc((uint)a_u32Size, (UIntPtr)0x0042 /* GHND */);
The first parameter should be the flag, as defined below
[DllImport("kernel32.dll")]
internal static extern IntPtr GlobalAlloc(uint uFlags, UIntPtr dwBytes);
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.