|
Oh, I missed your hints. Will try the examples mentioned and will get back with the solution if I succeed.
|
|
|
|
|
Hi to all!
I work with digital photocamera. I want to get preview image stream without using standart window. How can I do this? Or, if it is imposible, how can I disable setting buttons on standart window?
Thanks for any answer.
-- modified at 9:42 Monday 6th November, 2006
|
|
|
|
|
This is an amazing example, thank you for posting it!
Would it be possible to programmatically capture the image coming in from the twain device? Your code demonstrates that you can set the guif.ShowUI to be set to 0.
Gratefully yours,
Alex
http://www.lifesabirch.org
|
|
|
|
|
Thank you, this is all very helpful!
One quiestion: How can I display the scanner settings dialog without starting the scan process?
|
|
|
|
|
|
Hey love the code, but I have to crop the images I'm uploading. I'm definetly a newbie to TWAIN, and am using the VB.Net version. Right now Im just saving it and doing it in another program, is there anyway to crop it rogramatically... right now I just need any black space to be cropped out. Thanks for any suggestions.
|
|
|
|
|
FYI, the twain driver that came with my panasonic scanner has that feature. You might be able to set that programmatically using the capabilities get/set methods. However, since the twain dialog pops up anyway, I just set it then.
Good luck!
|
|
|
|
|
Ya I figure there has to be a way, but I'm kinda running on a deadline so my solution was to convert it to a bmp, and display in a picturebox, then have the user simlpy draw a rectangle and crop it. (I had to have the default gui disabled) But if anyone finds a way to do it, I'd still be very interested. Thanks for the info though.
|
|
|
|
|
Imports System
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Namespace ScanSoft
<StructLayout(LayoutKind.Sequential, Pack:=2)> Friend Class BITMAPINFOHEADER
Public biSize As Integer
Public biWidth As Integer
Public biHeight As Integer
Public biPlanes As Short
Public biBitCount As Short
Public biCompression As Integer
Public biSizeImage As Integer
Public biXPelsPerMeter As Integer
Public biYPelsPerMeter As Integer
Public biClrUsed As Integer
Public biClrImportant As Integer
End Class
Public Class scanToImage
<DllImport("gdi32.dll", ExactSpelling:=True)> Friend Shared Function SetDIBitsToDevice(ByVal hdc As IntPtr, ByVal xdst As Integer, ByVal ydst As Integer, ByVal width As Integer, ByVal height As Integer, ByVal xsrc As Integer, ByVal ysrc As Integer, ByVal start As Integer, ByVal lines As Integer, ByVal bitsptr As IntPtr, ByVal bmiptr As IntPtr, ByVal color As Integer) As Integer
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 GlobalFree(ByVal handle As IntPtr) As IntPtr
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto)> Public Shared Sub OutputDebugString(ByVal outstr As String)
End Sub
Dim bmi As BITMAPINFOHEADER
Dim bmprect As Rectangle
Dim dibhand As IntPtr
Dim bmpptr As IntPtr
Dim pixptr As IntPtr
Public Sub New(ByVal dibhandp As IntPtr)
bmprect = New Rectangle(0, 0, 0, 0)
dibhand = dibhandp
bmpptr = GlobalLock(dibhand)
pixptr = GetPixelInfo(bmpptr)
End Sub
Protected Function GetPixelInfo(ByVal bmpptr As IntPtr) As IntPtr
bmi = New BITMAPINFOHEADER
Marshal.PtrToStructure(bmpptr, bmi)
bmprect.X = bmprect.Y = 0
bmprect.Width = bmi.biWidth
bmprect.Height = bmi.biHeight
If (bmi.biSizeImage = 0) Then
bmi.biSizeImage = Int((((bmi.biWidth * bmi.biBitCount) + 31) & Hex(Not (31))) / 2 ^ 3) * bmi.biHeight
End If
Dim p As Integer = bmi.biClrUsed
If ((p = 0) And (bmi.biBitCount <= 8)) Then
p = Int(1 * 2 ^ bmi.biBitCount)
End If
p = (p * 4) + bmi.biSize + CType(bmpptr.ToInt32, Integer)
Return New IntPtr(p)
End Function
Public Function ImgToBitmap(ByVal dibhandp As IntPtr) As Bitmap
bmprect = New Rectangle(0, 0, 0, 0)
dibhand = dibhandp
bmpptr = GlobalLock(dibhand)
pixptr = GetPixelInfo(bmpptr)
Dim TempBMP As Bitmap = New Bitmap(bmprect.Width, bmprect.Height)
Dim TempGrap As Graphics = Graphics.FromImage(TempBMP)
Dim hdc As IntPtr = TempGrap.GetHdc
SetDIBitsToDevice(hdc, 0, 0, bmprect.Width, bmprect.Height, 0, 0, 0, bmprect.Height, pixptr, bmpptr, 0)
TempGrap.ReleaseHdc(hdc)
TempGrap.Dispose()
GlobalFree(dibhand)
dibhand = IntPtr.Zero
Return (TempBMP)
End Function
End Class
End Namespace
'Put this code in a new class. Change the following code in the mainform
Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
Dim cmd As TwainCommand = tw.PassMessage(m)
If (cmd = TwainCommand.Not) Then
Return False
End If
Select Case cmd
Case TwainCommand.CloseRequest
EndingScan()
tw.CloseSrc()
Case TwainCommand.CloseOk
EndingScan()
tw.CloseSrc()
Case TwainCommand.DeviceEvent
Case TwainCommand.Failure
EndingScan()
tw.CloseSrc()
Case TwainCommand.TransferReady
Dim pics As ArrayList = tw.TransferPictures()
EndingScan()
tw.CloseSrc()
picnumber += 1
Dim i As Integer
For i = 0 To pics.Count - 1 Step 1
Dim img As IntPtr = CType(pics(i), IntPtr)
Dim scan As New scanToImage(img)
PictureBox1.Image = scan.ImgToBitmap(img)
GetPictureInfo()
Next
End Select
Return True
End Function
|
|
|
|
|
it is not working for me.
|
|
|
|
|
i set it but it is not working for me.can you help me regarding this.
|
|
|
|
|
In your code you have explained how to set a capability.You are creating an instance of TwCapability class and place data for Countainer on Unmanaged Memory by using Marshal Class.
In a similar way, i want to retrieve capability's supported values,like in my application i want to populate combobox containing values for brightness,contrast,resolution.Which user can set before scanning.For this i am using MSG_GET but wont be able to retrieve the capabilities supported values in appropriate container.
Can u pls help me....
--Bijul Soni
|
|
|
|
|
"Application.AddMessageFilter() " doesn't work in ASP.net
this fn is only supported by windows form
wat is the alternative of this function for web page
sssssss
|
|
|
|
|
I've tried to scan with my scanner locked for transport (switch button on the device) and in this case
<br />
TwainCommand cmd = tw.PassMessage(ref m);<br />
sets cmd to null and program falls into infinite loop.
The solution is to capture null TwainCommand and close cource:
<br />
EndingScan();<br />
tw.CloseSrc();<br />
Bartosz Węgielewski
|
|
|
|
|
The message-handling doesn't seem to work on Windows 2000. The messages that is supposed to be handled by IMessageFilter.PreFilterMessage newer turns up, and it is not possible to scan anything.
At least not on my HP Scanjet 5530.
Is this a generel Win2000 problem, or is it related to my scanner?
On Xp everything works fine.
Regards
Jørn Esbensen
|
|
|
|
|
Try to update your TWAIN_32.dll
|
|
|
|
|
good day! i'm new with scanning in .net
using your library, how can i get multiple images using the multi scanning capability of the scanner?
|
|
|
|
|
Search for
TwCapability cap = new TwCapability( TwCap.XferCount, 1 );
change it to
TwCapability cap = new TwCapability( TwCap.XferCount, -1 );
This also works for duplex scanning. You must stop the scanning manually however. In my case, this is done through the twain dialog.
Good luck!
|
|
|
|
|
How can I set a particular resolution(200dpi,500dpi,etc)?
I'm trying to change these lines:
TwCapability capResx = new TwCapability(TwCap.IXResolution, 72, TwType.Fix32);
TwCapability capResy = new TwCapability(TwCap.IYResolution, 72, TwType.Fix32);
but I don't founded documentation about the parameters TwCap cap, short sval and TwType twtype.
Can you comment its?
Thanks you very much,
Marcel
|
|
|
|
|
'Put this in Acquire
Public Sub Acquire(Optional ByVal XResolution As Integer = 300, Optional ByVal YResolution As Integer = 300)
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
Dim capUnit As TwCapability = New TwCapability(TwCap.IUnits, 0, TwType.Int16)
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capUnit)
If rc <> TwRC.Success Then
CloseSrc()
Return
End If
'Set Resolution
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
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 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 = 0
guif.ModalUI = 0
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
'Now Change this
Friend Enum TwCap As Short
XferCount = &H1
ICompression = &H100
IPixelType = &H101
IUnits = &H102
IXferMech = &H103
BITDEPTH = &H112B
ICAP_IMAGEFILEFORMAT = &H110C
XResolution = &H1118
YResolution = &H1119
ImageWidth = &H1116
ImageLength = &H1117
End Enum
'And this
<StructLayout(LayoutKind.Sequential, Pack:=2)> Friend Class TwCapability
Public Cap As Short
Public ConType As Short
Public Handle As IntPtr
Public Sub TwCapability(ByVal capIn As TwCap)
Cap = CType(capIn, Short)
ConType = -1
End Sub
Public Sub New(ByVal capIn As TwCap, ByVal sval As Short, ByVal TWType As TwType)
Cap = CType(capIn, Short)
ConType = CType(TwOn.One, Short)
Handle = Twain.GlobalAlloc(&H42, 6)
Dim pv As IntPtr = Twain.GlobalLock(Handle)
Marshal.WriteInt16(pv, 0, CType(TWType, Short))
Marshal.WriteInt32(pv, 2, CType(sval, Integer))
Twain.GlobalUnlock(Handle)
End Sub
Public Sub Dispose()
If Not Equals(Handle, IntPtr.Zero) Then
Twain.GlobalFree(Handle)
End If
End Sub
Protected Overrides Sub Finalize()
If Not Equals(Handle, IntPtr.Zero) Then
Twain.GlobalFree(Handle)
End If
End Sub
End Class
'It should work! This code is working in my computer.
'Sorry I just have VB.NET version. May be you can convert it to C# by your own.
MCChin
-- modified at 0:09 Thursday 14th September, 2006
|
|
|
|
|
I used this code in my program. 'rc' allways returns TwRC.Success for every capability, but real image resolution doesn't change. Why can it be?
|
|
|
|
|
If I run the app available in the download, I can scan an image fine the first time. But, if I close the app and try to re-launch it, it just sits there as if it was in an infinite loop of sorts. When tracing, it never emerged from the tw.Init() method call. Within the Init call, it seems to get hung up on the call to DSMparent(...).
I'm running this using VS2005 on WinXP Pro SP2.
Have any of you encountered this issue before?
Thanks
|
|
|
|
|
Hi,
I'm trying to configure the scan area but it's not working. This is what I'm using:
I added these two structs:
<br />
[StructLayout(LayoutKind.Sequential, Pack = 2)]<br />
internal struct TwFrame {<br />
public TwFix32 Left;<br />
public TwFix32 Top;<br />
public TwFix32 Right;<br />
public TwFix32 Bottom;<br />
}<br />
<br />
[StructLayout(LayoutKind.Sequential, Pack = 2)]<br />
internal class TwImageLayout {<br />
public TwFrame Frame;<br />
public int DocumentNumber;<br />
public int PageNumber;<br />
public int FrameNumber;<br />
}<br />
Then I created this imported function:
<br />
[DllImport("twain_32.dll", EntryPoint = "#1")]<br />
private static extern TwRC DSilayout([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwImageLayout imglo);<br />
And finally I try to set the scan area using a call like this:
<br />
TwFrame fr = new TwFrame();<br />
TwFix32 fix32 = new TwFix32();<br />
fix32.FromFloat((float)left);<br />
fr.Left = fix32;<br />
fix32.FromFloat((float)top);<br />
fr.Top = fix32;<br />
fix32.FromFloat((float)right);<br />
fr.Right = fix32;<br />
fix32.FromFloat((float)bottom);<br />
fr.Bottom = fix32;<br />
TwImageLayout layout = new TwImageLayout();<br />
layout.Frame = fr;<br />
layout.DocumentNumber = 0;<br />
layout.PageNumber = 0;<br />
layout.FrameNumber = 0;<br />
TwRC rc = DSilayout(appid, srcds, TwDG.Image, TwDAT.ImageLayout, TwMSG.Set, layout);<br />
rc = DSilayout(appid, srcds, TwDG.Image, TwDAT.ImageLayout, TwMSG.Get, layout);<br />
This call returns TwRC.Succes, but if I call DSilayout with TwMSG.Get, I can see that the frame is not set. And ofcourse when I acquire() I get an image of the whole scan bed.
Can anyone helpme with this matter?
Thanks in advance.
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 2)]
internal struct TwFrame {
public int Left;
public int Top;
public int Right;
public int Bottom;
}
//set the scan area using a call like this
TwImageLayout layout = new TwImageLayout();
//Get the default layout
TwRC rc = DSilayout(appid, srcds, TwDG.Image, TwDAT.ImageLayout, TwMSG.Get, Layout);
Layout.Frame.Top = 0; //Set the top margin
Layout.Frame.Left = 0; //Set left margin
Layout.Frame.Right = 0; //Set right margin
Layout.Frame.Bottom = 0; //Set bottom margin
Layout.FrameNumber = 1;
Layout.PageNumber = 1;
Layout.DocumentNumber = 1;
rc = DSilayout(appid, srcds, TwDG.Image, TwDAT.ImageLayout, TwMSG.Set, layout);
TwStatus s = new TwStatus();
rc = DSstatus(appid, srcds, TwDG.Control, TwDAT.Status, TwMSG.Get, s);
if( rc != TwRC.Success )
{
CloseSrc();
return false;
}
|
|
|
|
|
'VB.NET Code. Please convert by your self to C#. This work in my pc and give better performance.
'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
<StructLayout(LayoutKind.Sequential, Pack:=2)> Friend Structure TwFrame
Public Left As TwFix32
Public Top As TwFix32
Public Right As TwFix32
Public Bottom As TwFix32
End Structure
|
|
|
|
|