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.FromHbitmap] I get an error.
Extracts of my code and the error statement from the Visual Studio Debug bellow:
Select Case cmd
Case TwainCommand.CloseRequest
EndingScan()
tw.CloseSrc()
Case TwainCommand.CloseOk
EndingScan()
tw.CloseSrc()
Case TwainCommand.DeviceEvent
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 newpic As PicForm = New PicForm(img)
'newpic.MdiParent = Me
Dim picnum As Integer = i + 1
Dim imgScan As Image
imgScan = Image.FromHbitmap(pics(0)) '!!! Here is where I get the error statement: A generic error occured in GDI+
imgScanPreview.Image = imgScan
'newpic.Text = "ScanPass" + picnumber.ToString() + "_Pic" + picnum.ToString()
'newpic.Show()
Next
End Select
Here is where the image(s) are put in the "pics" arraylist:
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
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
Here is the exception stacktrace:
System.Runtime.InteropServices.ExternalException was unhandled
ErrorCode=-2147467259
Message="A generic error occurred in GDI+."
Source="System.Drawing"
StackTrace:
at System.Drawing.Image.FromHbitmap(IntPtr hbitmap, IntPtr hpalette)
at System.Drawing.Image.FromHbitmap(IntPtr hbitmap)
at PMSDAWOE.MainFrame.PreFilterMessage(Message& m) in C:\NRK\Utvikling\PMS DAW OE\PMSDAWOE\PMSDAWOE\MainFrame.vb:line 133
at System.Windows.Forms.Application.ThreadContext.Pro cessFilters(MSG& msg, Boolean& modified)
at System.Windows.Forms.Application.ThreadContext.Pre TranslateMessage(MSG& msg)
at System.Windows.Forms.Application.ThreadContext.Sys tem.Windows.Forms.UnsafeNativeMethods.IMsoComponen t.FPreTranslateMessage(MSG& msg)
at System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationCo ntext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsF ormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsF ormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsF ormsApplicationBase.Run(String[] commandLine)
at PMSDAWOE.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context( Object state)
at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Thanks for any help!
- Jørn Jensen