|
I have an odd issue with device selection through a DLL based on your code. When running though the source proveided on this page I am able to select the pure Twain interface for my current test scanner(Canon 8600f). But when running the code that is based on this in a DLL form, it only allows me to select the Twain over WIA driver for the scanner.
My app that is using the DLL also has a WIA component, but other scanners that I must use are Twain Only.
I have made no changes to the Init() and Select methods, other than to be able to save the information for the selected scanner.
|
|
|
|
|
Found my own answer, the Scanner window seems like it has to be either the main, or the only window in the app.
|
|
|
|
|
palease tell me how to setup the scanner auto scan?
thanks!!!
|
|
|
|
|
please tell me how to check status ?
|
|
|
|
|
it is so cool and professionnal class programming. could you give me the information about the functions which are used in the classes? or is there any documentation about the twain_32?
|
|
|
|
|
I have successfully used your app for scanning apps on a WinXP machine with a Xerox Documate 510 scanner but if I use the same app and scanner on a WIN2K machine the scanner driver that gets installed on WIN2K is old or different for XEROX as a result the code launches the old driver for scanning and the application does not work with this old driver
My question is does this code require WIA capable scan drivers to work successfully ?
specifically if I loop through the code the for certain drivers the DSevent call returns back a
TwRC.NotDSEvent and hence the scanning does not proceed.
Any help would be appreciated.
Thanks
|
|
|
|
|
samething here on ricoh 1515 work fine but on epson gt 2500 have the same error , loop then loop and then loop very weirdo
|
|
|
|
|
how to copy the image to clipboard?
|
|
|
|
|
Clipboard.SetImage(image);
Best regards,
Christian
|
|
|
|
|
After view the file in viewer i cannot delete it. I've the error:
The process cannot access the file 'C:\ny.jpg' because it is being used by another process.
Any solutions?
|
|
|
|
|
I solve de problem. I must dispose and set to nothing all images before try delete the file.
|
|
|
|
|
Hi,All
I sovled the problem by the following codes:
_MODIDocument.Close(false);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_MODIDocument);
System.Runtime.InteropServices.Marshal.ReleaseComObject(_MODIDocument);
GC.Collect();
MODI.Document _NULLMODIDocument = new MODI.Document();
_NULLMODIDocument.Create(string.Empty);
axMiDocView1.Document = _NULLMODIDocument;
File.Delete(_filename);
It was done.Good luck!
1
|
|
|
|
|
Hi '
I am in a process of developing, ASP.NET (C Sharp) Document Scanning application? Kindly guide me how to use this to build such application.
Thanks,
Ali Mustapha
Life is too short to be counted, enjoy life.
|
|
|
|
|
Thanks for sharing!
|
|
|
|
|
I am trying to develop an app in VB.net.. wich
1) scan a doc without user interface
2) changing the resolution,dpi , black&white.. programmaticaly...
3) saving the scan image as a tiff in hard drive..
Anyone can help? EVEN with C#
|
|
|
|
|
|
Hello,
Someone knows if is possible to do invisible the window that appear with a progressbar that indicate scanning...
Thank you for your help.
|
|
|
|
|
Plis!!!
I Need the source code for my applications VB
I Need Really...
I'm New....
Thank You Very Much...
|
|
|
|
|
http://www.codeplex.com/openTwain, it used to be over on gotdotnet, but gotdotnet is closing so it moved.
|
|
|
|
|
Hi!
When I use TWAIN Gui as a stand-alone program, everything works fine, but when I use it as invoked form from my application, Init() method could hang up whole application. I traced into code and found out, that error is generated in Twainlib.cs at line 60:
TwRC rc = DSMparent( appid, IntPtr.Zero, TwDG.Control, TwDAT.Parent, TwMSG.OpenDSM, ref hwndp );
What is wrong here? Sometimes it works, sometimes not!
|
|
|
|
|
I've got the same problem.
All of a sudden it returns always Failure.
thnx.
DiMann wrote: When I use TWAIN Gui as a stand-alone program, everything works fine, but when I use it as invoked form from my application, Init() method could hang up whole application. I traced into code and found out, that error is generated in Twainlib.cs at line 60:
TwRC rc = DSMparent( appid, IntPtr.Zero, TwDG.Control, TwDAT.Parent, TwMSG.OpenDSM, ref hwndp );
What is wrong here? Sometimes it works, sometimes not!
Ronald
|
|
|
|
|
I Need The Source Code for an application VB please...
Thanks. I'm New.
Spanish....
Necesito el Codigo para una aplicacion VB por favor...
Gracias. Soy Nuevo.
PHALCOM2.
Favio.
|
|
|
|
|
|
i converted part of it to VB and am using it, email me if you want the classes
josh at pipeline dot com
|
|
|
|
|
TwainLib.vb
Imports System
Imports System.Collections
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Namespace TwainLib
Public Enum TwainCommand
[Not] = -1
Null = 0
TransferReady = 1
CloseRequest = 2
CloseOk = 3
DeviceEvent = 4
Failure = 5
End Enum
Public Class Twain
Private hwnd As IntPtr
Private appid As TwIdentity
Private srcds As TwIdentity
Private evtmsg As TwEvent
Private winmsg_m As WINMSG_S
<DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSMparent(<[In](), Out()> ByVal origin As TwIdentity, ByVal zeroptr As IntPtr, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, ByRef refptr As IntPtr) As TwRC
End Function
<DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSMident(<[In](), Out()> ByVal origin As TwIdentity, ByVal zeroptr As IntPtr, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, <[In](), Out()> ByVal idds As TwIdentity) As TwRC
End Function
<DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSMstatus(<[In](), Out()> ByVal origin As TwIdentity, ByVal zeroptr As IntPtr, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, <[In](), Out()> ByVal dsmstat As TwStatus) As TwRC
End Function
<DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSuserif(<[In](), Out()> ByVal origin As TwIdentity, <[In](), Out()> ByVal dest As TwIdentity, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, ByVal guif As TwUserInterface) As TwRC
End Function
<DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSevent(<[In](), Out()> ByVal origin As TwIdentity, <[In](), Out()> ByVal dest As TwIdentity, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, ByRef evt As TwEvent) As TwRC
End Function
<DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSstatus(<[In](), Out()> ByVal origin As TwIdentity, <[In]()> ByVal dest As TwIdentity, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, <[In](), Out()> ByVal dsmstat As TwStatus) As TwRC
End Function
<DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DScap(<[In](), Out()> ByVal origin As TwIdentity, <[In]()> ByVal dest As TwIdentity, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, <[In](), Out()> ByVal capa As TwCapability) As TwRC
End Function
<DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSiinf(<[In](), Out()> ByVal origin As TwIdentity, <[In]()> ByVal dest As TwIdentity, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, <[In](), Out()> ByVal imginf As TwImageInfo) As TwRC
End Function
<DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSixfer(<[In](), Out()> ByVal origin As TwIdentity, <[In]()> ByVal dest As TwIdentity, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, ByRef hbitmap As IntPtr) As TwRC
End Function
<DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSpxfer(<[In](), Out()> ByVal origin As TwIdentity, <[In]()> ByVal dest As TwIdentity, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, <[In](), Out()> ByVal pxfr As TwPendingXfers) As TwRC
End Function
<DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSilayout(<[In](), Out()> ByVal origin As TwIdentity, <[In](), Out()> ByVal dest As TwIdentity, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, <[In](), Out()> ByVal imglo As TwImageLayout) As TwRC
End Function
<DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSMlayout(<[In](), Out()> ByVal origin As TwIdentity, <[In](), Out()> ByVal dest As TwIdentity, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, <[In](), Out()> ByVal imglo As IntPtr) As TwRC
End Function
<DllImport("kernel32.dll", ExactSpelling:=True)> Friend Shared Function GlobalAlloc(ByVal flags As Integer, ByVal size As Integer) As IntPtr
End Function
<DllImport("kernel32.dll", ExactSpelling:=True)> Friend Shared Function GlobalLock(ByVal handle As IntPtr) As IntPtr
End Function
<DllImport("kernel32.dll", ExactSpelling:=True)> Friend Shared Function GlobalUnlock(ByVal handle As IntPtr) As Boolean
End Function
<DllImport("kernel32.dll", ExactSpelling:=True)> Friend Shared Function GlobalFree(ByVal handle As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", ExactSpelling:=True)> Private Shared Function GetMessagePos() As Integer
End Function
<DllImport("user32.dll", ExactSpelling:=True)> Private Shared Function GetMessageTime() As Integer
End Function
<DllImport("gdi32.dll", ExactSpelling:=True)> Private Shared Function GetDeviceCaps(ByVal hDC As IntPtr, ByVal nIndex As Integer) As Integer
End Function
<DllImport("gdi32.dll", CharSet:=CharSet.Auto)> Private Shared Function CreateDC(ByVal szdriver As String, ByVal szdevice As String, ByVal szoutput As String, ByVal devmode As IntPtr) As IntPtr
End Function
<DllImport("gdi32.dll", ExactSpelling:=True)> Private Shared Function DeleteDC(ByVal hdc As IntPtr) As Boolean
End Function
Private Const CountryUSA As Short = 1
Private Const LanguageUSA As Short = 13
Public Sub New()
appid = New TwIdentity
appid.Id = IntPtr.Zero
appid.Version.MajorNum = 1
appid.Version.MinorNum = 1
appid.Version.Language = LanguageUSA
appid.Version.Country = CountryUSA
appid.Version.Info = "TWAIN Scanner"
appid.ProtocolMajor = TwProtocol.Major
appid.ProtocolMinor = TwProtocol.Minor
appid.SupportedGroups = CType(TwDG.Image Or TwDG.Control, Integer)
appid.Manufacturer = "Kod Efisien Sdn Bhd"
appid.ProductFamily = "TWAIN 32 Scanner"
appid.ProductName = "ScanSoft"
srcds = New TwIdentity
srcds.Id = IntPtr.Zero
evtmsg.EventPtr = Marshal.AllocHGlobal(Marshal.SizeOf(winmsg_m))
End Sub
Public Sub Dispose()
Marshal.FreeHGlobal(evtmsg.EventPtr)
End Sub
Protected Overrides Sub Finalize()
Marshal.FreeHGlobal(evtmsg.EventPtr)
End Sub
Public Sub Init(ByVal hwndp As IntPtr)
Finish()
Dim rc As TwRC = DSMparent(appid, IntPtr.Zero, TwDG.Control, TwDAT.Parent, TwMSG.OpenDSM, hwndp)
If (rc = TwRC.Success) Then
rc = DSMident(appid, IntPtr.Zero, TwDG.Control, TwDAT.Identity, TwMSG.GetDefault, srcds)
If (rc = TwRC.Success) Then
hwnd = hwndp
Else
rc = DSMparent(appid, IntPtr.Zero, TwDG.Control, TwDAT.Parent, TwMSG.CloseDSM, hwndp)
End If
End If
End Sub
Public Sub [Select](Optional ByRef Model As String = "")
Dim rc As TwRC
CloseSrc()
If Equals(appid.Id, IntPtr.Zero) = True Then
Init(hwnd)
If Equals(appid.Id, IntPtr.Zero) = True Then
Return
End If
End If
rc = DSMident(appid, IntPtr.Zero, TwDG.Control, TwDAT.Identity, TwMSG.UserSelect, srcds)
Model = srcds.ProductName.ToString
End Sub
Public Sub [SelectDefault](Optional ByRef Model As String = "")
Dim rc As TwRC
CloseSrc()
If Equals(appid.Id, IntPtr.Zero) = True Then
Init(hwnd)
If Equals(appid.Id, IntPtr.Zero) = True Then
Return
End If
End If
rc = DSMident(appid, IntPtr.Zero, TwDG.Control, TwDAT.Identity, TwMSG.GetDefault, srcds)
Model = srcds.ProductName.ToString
End Sub
Public Sub Acquire(Optional ByVal PaperSize As TwSS = TwSS.TwSS_A4, Optional ByVal XScale As Single = 1, _
Optional ByVal YScale As Single = 1, Optional ByVal SColor As TwColourType = TwColourType.twRGB, _
Optional ByVal TopMargin As Single = 0.0, Optional ByVal LeftMargin As Single = 0.0, _
Optional ByVal RightMargin As Single = 0.0, Optional ByVal BottomMargin As Single = 0.0, _
Optional ByVal FrameNumber As Integer = 1, Optional ByVal PageNumber As Integer = 1, _
Optional ByVal DocumentNumber As Integer = 1)
Dim rc As TwRC
CloseSrc()
If Equals(appid.Id, IntPtr.Zero) = True Then
Init(hwnd)
If Equals(appid.Id, IntPtr.Zero) = True Then
Return
End If
End If
rc = DSMident(appid, IntPtr.Zero, TwDG.Control, TwDAT.Identity, TwMSG.OpenDS, srcds)
If (rc <> TwRC.Success) Then
Return
End If
'Set Unit to Pixels
Dim Pixels As TwUnit = TwUnit.twPIXELS
Dim capUnit As TwCapability = New TwCapability(TwCap.IUnits, Pixels, TwType.Int16)
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capUnit)
If rc <> TwRC.Success Then
CloseSrc()
Return
End If
''Set X Resolution
'Dim XResolution As Single = 1
'Dim capX As TwCapability = New TwCapability(TwCap.XResolution, XResolution, TwType.Fix32)
'rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capX)
'If rc <> TwRC.Success Then
' CloseSrc()
' Return
'End If
''Set Y Resolution
'Dim capY As TwCapability = New TwCapability(TwCap.YResolution, YResolution, TwType.Fix32)
'rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capY)
'If rc <> TwRC.Success Then
' CloseSrc()
' Return
'End If
Dim xcap As TwCapability = New TwCapability(TwCap.XScaling, XScale, TwType.Fix32)
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, xcap)
If rc <> TwRC.Success Then
CloseSrc()
Return
End If
xcap = New TwCapability(TwCap.YScaling, YScale, TwType.Fix32)
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, xcap)
If rc <> TwRC.Success Then
CloseSrc()
Return
End If
Dim ps As TwCapability = New TwCapability(TwCap.SupportedSizes, PaperSize, TwType.Int16)
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, ps)
If rc <> TwRC.Success Then
CloseSrc()
Return
End If
'Set Layout
Dim Layout As TwImageLayout = New TwImageLayout
rc = DSilayout(appid, srcds, TwDG.Image, TwDAT.ImageLayout, TwMSG.Get, Layout)
If rc <> TwRC.Success Then
CloseSrc()
Return
End If
If TopMargin > 0 Then
Layout.Frame.Top.FromFloat(CSng(TopMargin))
End If
If LeftMargin > 0 Then
Layout.Frame.Left.FromFloat(CSng(LeftMargin))
End If
If RightMargin > 0 Then
Layout.Frame.Right.FromFloat(CSng(RightMargin))
End If
If BottomMargin > 0 Then
Layout.Frame.Bottom.FromFloat(CSng(BottomMargin))
End If
Layout.FrameNumber = 1
Layout.PageNumber = 1
Layout.DocumentNumber = 1
rc = Me.DSilayout(appid, srcds, TwDG.Image, TwDAT.ImageLayout, TwMSG.Set, Layout)
If rc <> TwRC.Success Then
CloseSrc()
Return
End If
'Dim ptr1 As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Layout))
'Marshal.StructureToPtr(Layout, ptr1, False)
'rc = Me.DSMlayout(appid, srcds, TwDG.Image, TwDAT.ImageLayout, TwMSG.Get, ptr1)
'If rc <> TwRC.Success Then
' CloseSrc()
' Return
'End If
''Dim ptr2 As IntPtr = ptr1
'Marshal.PtrToStructure(ptr1, Layout)
'If TopMargin > 0 Then
' Layout.Frame.Top.FromFloat(CSng(TopMargin))
'End If
'If LeftMargin > 0 Then
' Layout.Frame.Left.FromFloat(CSng(LeftMargin))
'End If
'If RightMargin > 0 Then
' Layout.Frame.Right.FromFloat(CSng(RightMargin))
'End If
'If BottomMargin > 0 Then
' Layout.Frame.Bottom.FromFloat(CSng(BottomMargin))
'End If
'Layout.FrameNumber = 1
'Layout.PageNumber = 1
'Layout.DocumentNumber = 1
'Marshal.StructureToPtr(Layout, ptr1, True)
'rc = Me.DSMlayout(appid, srcds, TwDG.Image, TwDAT.ImageLayout, TwMSG.Set, ptr1)
'If rc <> TwRC.Success Then
' CloseSrc()
' Return
'End If
'Scan Colour
Dim Ccap As TwCapability = New TwCapability(TwCap.IPixelType, SColor, TwType.UInt16)
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, Ccap)
If (rc <> TwRC.Success) Then
CloseSrc()
Return
End If
Dim cap As TwCapability = New TwCapability(TwCap.XferCount, 1, TwType.Int16)
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, cap)
If (rc <> TwRC.Success) Then
CloseSrc()
Return
End If
Dim guif As TwUserInterface = New TwUserInterface
guif.ShowUI = 1
guif.ModalUI = 1
guif.ParentHand = hwnd
rc = DSuserif(appid, srcds, TwDG.Control, TwDAT.UserInterface, TwMSG.EnableDS, guif)
If (rc <> TwRC.Success) Then
CloseSrc()
Return
End If
End Sub
Public Function TransferPictures() As ArrayList
Dim pics As ArrayList = New ArrayList
If Equals(srcds.Id, IntPtr.Zero) Then
Return pics
End If
Dim rc As TwRC
Dim hbitmap As IntPtr = IntPtr.Zero
Dim pxfr As TwPendingXfers = New TwPendingXfers
Do
pxfr.Count = 0
hbitmap = IntPtr.Zero
Dim iinf As TwImageInfo = New TwImageInfo
rc = DSiinf(appid, srcds, TwDG.Image, TwDAT.ImageInfo, TwMSG.Get, iinf)
If (rc <> TwRC.Success) Then
CloseSrc()
Return pics
End If
'StartScan
rc = DSixfer(appid, srcds, TwDG.Image, TwDAT.ImageNativeXfer, TwMSG.Get, hbitmap)
If (rc <> TwRC.XferDone) Then
CloseSrc()
Return pics
End If
rc = DSpxfer(appid, srcds, TwDG.Control, TwDAT.PendingXfers, TwMSG.EndXfer, pxfr)
If (rc <> TwRC.Success) Then
CloseSrc()
Return pics
End If
pics.Add(hbitmap)
Loop While (pxfr.Count <> 0)
rc = DSpxfer(appid, srcds, TwDG.Control, TwDAT.PendingXfers, TwMSG.Reset, pxfr)
Return pics
End Function
Public Function PassMessage(ByVal m As Message) As TwainCommand
If Equals(srcds.Id, IntPtr.Zero) Then
Return TwainCommand.Not
End If
Dim pos As Integer = GetMessagePos()
winmsg_m.hwnd = m.HWnd
winmsg_m.message = m.Msg
winmsg_m.wParam = m.WParam
winmsg_m.lParam = m.LParam
winmsg_m.time = GetMessageTime()
winmsg_m.x = pos 'CType(pos, Short)
winmsg_m.y = Int(pos / 2 ^ 16) 'CType(Int(pos / 2 ^ 16), Short)
Marshal.StructureToPtr(winmsg_m, evtmsg.EventPtr, False)
evtmsg.Message = 0
Dim rc As TwRC = DSevent(appid, srcds, TwDG.Control, TwDAT.Event, TwMSG.ProcessEvent, evtmsg)
If (rc = TwRC.NotDSEvent) Then
Return TwainCommand.Not
End If
If (rc = TwRC.Failure) Then
Return TwainCommand.Failure
End If
If (evtmsg.Message = CType(TwMSG.XFerReady, Short)) Then
Return TwainCommand.TransferReady
End If
If (evtmsg.Message = CType(TwMSG.CloseDSReq, Short)) Then
Return TwainCommand.CloseRequest
End If
If (evtmsg.Message = CType(TwMSG.CloseDSOK, Short)) Then
Return TwainCommand.CloseOk
End If
If (evtmsg.Message = CType(TwMSG.DeviceEvent, Short)) Then
Return TwainCommand.DeviceEvent
End If
Return TwainCommand.Null
End Function
Public Sub CloseSrc()
Dim rc As TwRC
If Not Equals(srcds.Id, IntPtr.Zero) Then
Dim guif As TwUserInterface = New TwUserInterface
rc = DSuserif(appid, srcds, TwDG.Control, TwDAT.UserInterface, TwMSG.DisableDS, guif)
rc = DSMident(appid, IntPtr.Zero, TwDG.Control, TwDAT.Identity, TwMSG.CloseDS, srcds)
End If
End Sub
Public Sub Finish()
Dim rc As TwRC
CloseSrc()
If Not Equals(appid.Id, IntPtr.Zero) Then
rc = DSMparent(appid, IntPtr.Zero, TwDG.Control, TwDAT.Parent, TwMSG.CloseDSM, hwnd)
End If
appid.Id = IntPtr.Zero
End Sub
Public Shared ReadOnly Property ScreenBitDepth() As Integer
Get
Dim screenDC As IntPtr = CreateDC("DISPLAY", Nothing, Nothing, IntPtr.Zero)
Dim bitDepth As Integer = GetDeviceCaps(screenDC, 12)
bitDepth *= GetDeviceCaps(screenDC, 14)
DeleteDC(screenDC)
Return bitDepth
End Get
End Property
<StructLayout(LayoutKind.Sequential, Pack:=4)> Friend Structure WINMSG_S
Public hwnd As IntPtr
Public message As Integer
Public wParam As IntPtr
Public lParam As IntPtr
Public time As Integer
Public x As Integer
Public y As Integer
End Structure
End Class
End Namespace
|
|
|
|
|