|
http://www.smalleranimals.com/dc/dcboard.php?az=show_topic&forum=105&topic_id=627&mode=full&page=5[^]
I found a very nice solution on that address. Thanks Tom. This solution works with references. It use less memory then other ones. It's very important when you works with large pictures.
using System.Drawing;
using System.Reflection;
// . . .
[DllImport("GdiPlus.dll", CharSet=CharSet.Unicode,
ExactSpelling=true)]
private static extern int GdipCreateBitmapFromGdiDib(IntPtr pBIH,
IntPtr pPix, out IntPtr pBitmap);
public static Bitmap BitmapFromDIB(IntPtr pDIB)
{
// verify DIB ...
// get pointer to DIB pixels, e.g.,
IntPtr pPix = Gdip.GetPixelInfo(pDIB);
if (pPix == IntPtr.Zero)
return null; // failure ...
// might want a try-catch here instead
MethodInfo mi = typeof(Bitmap).GetMethod("FromGDIplus",
BindingFlags.Static | BindingFlags.NonPublic);
if (mi == null)
return null; // (permission problem)
IntPtr pBmp = IntPtr.Zero;
int status = GdipCreateBitmapFromGdiDib(pDIB, pPix, out pBmp);
if ((status == 0) && (pBmp != IntPtr.Zero))
// success
return (Bitmap)mi.Invoke(null, new object[] {pBmp});
else
return null; // failure
}
|
|
|
|
|
|
Hi,
Here you have another solution in C#:
///
/// Conviert the pointer of scanning image to bitmap
///
/// <returns>
private Bitmap ImgToBitmap()
{
ArrayList pics = tw.TransferPictures();
EndingScan();
tw.CloseSrc();
if (pics.Count == 0) return null;
IntPtr img = (IntPtr)pics[pics.Count - 1];
IntPtr bmpptr = Twain.GlobalLock(img);
IntPtr pixptr = GetPixelInfo(bmpptr);
Twain.BITMAPINFOHEADER bmi = new Twain.BITMAPINFOHEADER();
Marshal.PtrToStructure(bmpptr, bmi);
int PrWidth = bmi.biWidth;
int PrHeight = bmi.biHeight;
Bitmap TempBMP = new Bitmap(PrWidth, PrHeight);
//TempBMP.SetResolution(resolution,resolution);
Graphics TempGrap = Graphics.FromImage(TempBMP);
TempGrap.Clear(Color.White);
IntPtr hdc = TempGrap.GetHdc();
Twain.SetDIBitsToDevice(hdc, 0, 0, PrWidth, PrHeight,
0, 0, 0, PrHeight, pixptr, bmpptr, 0);
TempGrap.ReleaseHdc(hdc);
TempGrap.Dispose();
Twain.GlobalFree(img);
img = IntPtr.Zero;
pics.Clear();
return (TempBMP);
}
Enjoy it
|
|
|
|
|
to the post above - awsome code, you saved my life...thank you
|
|
|
|
|
Bug Description:
I have a scanner installed on my computer,but the power supply is turned off.
So when I click the Acquire menuitem of the TWAIN.NET image scanner, an error message is shown.After I clike the OK button on it, the mainform is disabled.
A possible correction:
public enum TwainCommand
{
......
NotDeviceEvent = 5//add another command
}
public TwainCommand PassMessage( ref Message m )
{
......
if( rc==TwRC.Failure )
return TwainCommand.Not;
if( rc == TwRC.NotDSEvent )
return TwainCommand.NotDeviceEvent;
......
}
public TwainCommand PassMessage( ref Message m )
{
......
if (cmd == TwainCommand.Not)
{
Application.RemoveMessageFilter(this);
msgfilter = false;
this.Enabled = true;
this.Activate();
return false;
}
if (cmd == TwainCommand.NotDeviceEvent)
{
this.Enabled = false;
return false;
}
......
}
|
|
|
|
|
its a vb 2005 project, and the workspace is marked private becasue I don't know how to un-mark it as private yet, but it should auto accept your application to join.
http://www.gotdotnet.com/workspaces/workspace.aspx?id=e93d10b6-d8f0-4bc0-affe-0c6549e857d7
gabe
|
|
|
|
|
and now its public (don't have to join) at a new address
http://www.gotdotnet.com/workspaces/workspace.aspx?id=dd75a486-e2da-4547-b475-20739327b376
|
|
|
|
|
HI Anyone can scan multi tiff image?????
Please help.
THX
|
|
|
|
|
In the "Acquire" method in TwainLib.cs change:
TwCapability cap = new TwCapability( TwCap.XferCount, 1 );
to
TwCapability cap = new TwCapability( TwCap.XferCount, -1 );
The last parameter specifies the number of pages to scan.
Felix.
|
|
|
|
|
|
This doesn't seem to work for me.
The scanner GUI closes automatically after scanning the first page.
|
|
|
|
|
" This doesn't seem to work for me.
The scanner GUI closes automatically after scanning the first page. "
is the same for me!!!
someone knows another way?!
Please... need much!
|
|
|
|
|
When i set
TwCapability cap = new TwCapability( TwCap.XferCount, -1 );
i can't stop the scanner when it's running (after click a stop button with command: tw.Finish())
Who can help me,
Thanks and Best regard
--
Nguyen Kiem Hieu
FPT Information System Company
Defense-Sercurity-Education sector
Address: 2nd floor, 101 Lang Ha St, Dongda Dis, Hanoi, Vietnam
Mobile: (+84)0902 216 369
Website: http://www.fis.com.vn
Email: hieunk2@fpt.com.vn
|
|
|
|
|
Hello i need a method for setting tonality and dpi .
example
dpi >= 300
tonality >= X=-100 Y=0
Thanks
|
|
|
|
|
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 );
}
TwCapability cap = new TwCapability(TwCap.XferCount,-1);
//rc = GetCapabilityDS( appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Get, cap );
//Pixel Type
cap = new TwCapability(TwCap.IPixelType);
rc = GetCapabilityDS( appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Get, cap );
if (rc==TwRC.Failure)
pixel=true;
i worte this code but it didn't work it gives every time failuare?
why?
Help:---------------
Thanks in advance?
Born to code
|
|
|
|
|
Twain (or my scanner) seems to completely ignore the following code to set the X and Y resolution. I don't get the error messages - but when I look in the Acquire dialog box the resolution is still set to 300 dpi. Any ideas? (Also, most other capability settings fail - I have a Logitech PageScan Color scanner using TWAIN version 1,6,0,6)
<br />
TwCapability cap;<br />
<br />
cap = new TwCapability( TwCap.ICAP_XRESOLUTION, 150 );<br />
rc = DScap( appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, cap );<br />
if( rc != TwRC.Success )<br />
{<br />
MessageBox.Show("ERROR: X");<br />
CloseSrc();<br />
return false;<br />
}<br />
<br />
<br />
<br />
<br />
cap = new TwCapability( TwCap.ICAP_YRESOLUTION, 150 );<br />
rc = DScap( appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, cap );<br />
if( rc != TwRC.Success )<br />
{<br />
MessageBox.Show("ERROR: Y");<br />
CloseSrc();<br />
return false;<br />
}
Drew
|
|
|
|
|
Ahhhhh.... I need to set ShowUI to 0! Makes sense - if you show the Acquire dialog, Twain recalls the users last settings.
<br />
TwUserInterface guif = new TwUserInterface();<br />
guif.ShowUI = 0;<br />
guif.ModalUI = 1;<br />
guif.ParentHand = hwnd;<br />
rc = DSuserif( appid, srcds, TwDG.Control, TwDAT.UserInterface, TwMSG.EnableDS, guif );<br />
<br />
if( rc != TwRC.Success )<br />
{<br />
CloseSrc();<br />
return false;<br />
}<br />
Drew
|
|
|
|
|
It is bug in the constructor of TwCapability.
Code before:
public TwCapability( Capability cap, short sval )<br />
{<br />
Cap = (short) cap;<br />
ConType = (short) TwOn.One;<br />
Handle = Twain32API.GlobalAlloc( 0x42, 6 );<br />
IntPtr pv = Twain32API.GlobalLock( Handle );<br />
Marshal.WriteInt16( pv, 0, (short) TwType.Int16); Marshal.WriteInt32( pv, 2, (int) sval );<br />
Twain32API.GlobalUnlock( Handle );<br />
}<br />
<br />
TwCapability cap = new TwCapability(Capability.ICAP_XRESOLUTION, 150);
Look and try this code:
public TwCapability( Capability cap, short sval, TwType type )<br />
{<br />
Cap = (short) cap;<br />
ConType = (short) TwOn.One;<br />
Handle = Twain32API.GlobalAlloc( 0x42, 6 );<br />
IntPtr pv = Twain32API.GlobalLock( Handle );<br />
Marshal.WriteInt16( pv, 0, (short) type ); Marshal.WriteInt32( pv, 2, (int) sval );<br />
Twain32API.GlobalUnlock( Handle );<br />
}<br />
<br />
<br />
TwCapability cap = new TwCapability(Capability.ICAP_XRESOLUTION, 150, TwType.Fix32);
Regards
Bartek Surowiec
|
|
|
|
|
If you are expanding this work like I am,
the TWAIN.h header file is useful:
http://www.twain.org/devfiles/twain.h
Drew
|
|
|
|
|
Hi,
I am trying to set the paper source via code instead of using the Dialog. Any idea how I can do that?
Thanx
Winfried
BTW: Nice code, keep up the good work!
|
|
|
|
|
papersource... like whether to acquire from the flatbed or from the feeder?
check the following caps:
CAP_AUTOFEED
CAP_CLEARPAGE
CAP_FEEDERLOADED
CAP_FEEDPAGE
CAP_REWINDPAGE
and the 'Feeder Control' article in the appendix.
gabe
|
|
|
|
|
Anyone have success getting CustomDsData Set to change the current value of a capability? CustomDsData Get works well (the values for the capabilites match as expected), but after I run the CustomDsData Set passing in a previously saved CustomDsData (and I receive an RC = success), when i recheck a particular capability, the value doesn't match..
gabe
|
|
|
|
|
DAT_SETUPFILEXFER = Access violation writing location ?
Using the twain toolkit, ‘TWAIN_32 Sample Source 1.603’,
CAP_IXferMech has a default of Native
I can set it to File (Windows enviroment) with an RC = Success
CAP_IImageFileFormat has a default of bmp, and I leave it alone as that’s what I’d like
After I receive a TransferReady from the source, I set up a new TW_SETUPFILEXFER
Structure, and pass it into:
Vb.net
DsSetupFileTransfer(pOrigin, pDest, TwDataGroup.Control, _
TwDat.SetupFileXfer, TwMessage.GetDefault, SetupFile)
C#
DsSetupFileTransfer(pOrigin, pDest, TwDataGroup.Control, TwDat.SetupFileXfer, TwMessage.GetDefault, SetupFile);
But I get an “Access violation writing location”
(I get the same message for MSG_SET, MSF_GET, MSG_GETDEFAULT and MSG_RESET.)
In the meantime, the c++ twack32 code seems to transfer in file mode just fine, but seem to be unable to follow its logic
Has anyone ported file transfer to c# or vb.net?
Various defs that I’m using in vb.net and ShardDevelops convertion to c#:
-------------------------------------------------
Vb.net
<dllimport("twain_32.dll", entrypoint:="#1" )=""> _
Private Shared Function DsSetupFileTransfer(ByVal origin As TwIdentity, _
ByVal dest As TwIdentity, _
ByVal dg As TwDataGroup, _
ByVal dat As TwDat, _
ByVal msg As TwMessage, _
ByVal pSetupFile As TwSetupFileXfer) As TwReturnCode
End Function
C# (Sharp develop convertion)
[DllImport("twain_32.dll", EntryPoint = "#1")]
private static TwReturnCode DsSetupFileTransfer(TwIdentity origin, TwIdentity dest, TwDataGroup dg, TwDat dat, TwMessage msg, TwSetupFileXfer pSetupFile)
-------------------------------------------------
vb.net
<structlayout(layoutkind.sequential, pack:="2," charset:="CharSet.Ansi)"> _
Friend Structure TwSetupFileXfer
<marshalas(unmanagedtype.byvaltstr, sizeconst:="256)"> _
Public FileName As String
Public Format As Short
Public VrefNum As Short
End Structure
C# (Sharp develop convertion)
[StructLayout(LayoutKind.Sequential, Pack = 2, CharSet = CharSet.Ansi)]
internal struct TwSetupFileXfer
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string FileName;
public short Format;
public short VrefNum;
}
-------------------------------------------------
|
|
|
|
|
nevermind.
as is the case, after banging my head on something for a little more than i week - i deciede to ask why i cant get it to work, and within moments after i post the problem makes itself known.
If it matters to anyone else:
replace
ByVal pSetupFile As TwSetupFileXfer
with
ByRef pSetupFile As TwSetupFileXfer
|
|
|
|
|
I have a blog that contains some explanation and code which can be used to transfer a file between a client and a server application, running independently. I have posted the link below, see if it is of any help:
http://haroonalvi.wordpress.com/
|
|
|
|
|