Hey!
I have an issue with creating bitmap from an IntPtr. The IntPtr is created from a Twain scanner, which seem to work perfectly in it's original state (the app is originally written by somebody else). It's when I add the [Image.FromHbitm ap] I get an error.
Extracts of my code and the error statement from the Visual Studio Debug bellow:
Select Case cmd
Case TwainCommand.Cl oseRequest
EndingScan()
tw.CloseSrc()
Case TwainCommand.Cl oseOk
EndingScan()
tw.CloseSrc()
Case TwainCommand.De viceEvent
Case TwainCommand.Tr ansferReady
Dim pics As ArrayList = tw.TransferPict ures()
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 newpic As PicForm = New PicForm(img)
'newpic.MdiPare nt = Me
Dim picnum As Integer = i + 1
Dim imgScan As Image
imgScan = Image.FromHbitm ap(pics(0)) '!!! Here is where I get the error statement: A generic error occured in GDI+
imgScanPreview. Image = imgScan
'newpic.Text = "ScanPass" + picnumber.ToStr ing() + "_Pic" + picnum.ToString ()
'newpic.Show()
Next
End Select
Here is where the image(s) are put in the "pics" arraylist:
Public Function TransferPicture s() 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
rc = DSixfer(appid, srcds, TwDG.Image, TwDAT.ImageNati veXfer, TwMSG.Get, hbitmap)
If (rc <> TwRC.XferDone) Then
CloseSrc()
Return pics
End If
rc = DSpxfer(appid, srcds, TwDG.Control, TwDAT.PendingXf ers, TwMSG.EndXfer, pxfr)
If (rc <> TwRC.Success) Then
CloseSrc()
Return pics
End If
pics.Add(hbitma p)
Loop While (pxfr.Count <> 0)
rc = DSpxfer(appid, srcds, TwDG.Control, TwDAT.PendingXf ers, TwMSG.Reset, pxfr)
Return pics
End Function
Here is the exception stacktrace:
System.Runtime. InteropServices .ExternalExcept ion was unhandled
ErrorCode=-2147467259
Message="A generic error occurred in GDI+."
Source="System. Drawing"
StackTrace:
at System.Drawing. Image.FromHbitm ap(IntPtr hbitmap, IntPtr hpalette)
at System.Drawing. Image.FromHbitm ap(IntPtr hbitmap)
at PMSDAWOE.MainFr ame.PreFilterMe ssage(Message& m) in C:\NRK\Utviklin g\PMS DAW OE\PMSDAWOE\PMS DAWOE\MainFrame .vb:line 133
at System.Windows. Forms.Applicati on.ThreadContex t.ProcessFilter s(MSG& msg, Boolean& modified)
at System.Windows. Forms.Applicati on.ThreadContex t.PreTranslateM essage(MSG& msg)
at System.Windows. Forms.Applicati on.ThreadContex t.System.Window s.Forms.UnsafeN ativeMethods.IM soComponent.FPr eTranslateMessa ge(MSG& msg)
at System.Windows. Forms.Applicati on.ComponentMan ager.System.Win dows.Forms.Unsa feNativeMethods .IMsoComponentM anager.FPushMes sageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows. Forms.Applicati on.ThreadContex t.RunMessageLoo pInner(Int32 reason, ApplicationCont ext context)
at System.Windows. Forms.Applicati on.ThreadContex t.RunMessageLoo p(Int32 reason, ApplicationCont ext context)
at System.Windows. Forms.Applicati on.Run(Applicat ionContext context)
at Microsoft.Visua lBasic.Applicat ionServices.Win dowsFormsApplic ationBase.OnRun ()
at Microsoft.Visua lBasic.Applicat ionServices.Win dowsFormsApplic ationBase.DoApp licationModel()
at Microsoft.Visua lBasic.Applicat ionServices.Win dowsFormsApplic ationBase.Run(S tring[] commandLine)
at PMSDAWOE.My.MyA pplication.Main (String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.v b:line 81
at System.AppDomai n.nExecuteAssem bly(Assembly assembly, String[] args)
at System.AppDomai n.ExecuteAssemb ly(String assemblyFile, Evidence assemblySecurit y, String[] args)
at Microsoft.Visua lStudio.Hosting Process.HostPro c.RunUsersAssem bly()
at System.Threadin g.ThreadHelper. ThreadStart_Con text(Object state)
at System.Threadin g.ExecutionCont ext.Run(Executi onContext executionContex t, ContextCallback callback, Object state)
at System.Threadin g.ThreadHelper. ThreadStart()
Thanks for any help!
- Jørn Jensen