I have some code that takes a screen capture of my monitor. This code seems to work perfectly OK and can take multiple screen captures, apart from when I do a Print Screen using the keyboard or open SnagIT 8, when I then try to run my code again I get an error "A generic error occured in GDI+"
I am at a bit of a loss, I have had a look on the internet, but everything I seem to find appears to be related to web apps. and access permissions. This is a window based application and therefore can not seem to find an answer.
Below is the code I am running:
The line of code that gives the error is:
Below is also the stack trace:
at System.Drawing.Image.FromHbitmap(IntPtr hbitmap, IntPtr hpalette)
at System.Drawing.Image.FromHbitmap(IntPtr hbitmap)
at ReviewInsight.ScreenCapture.CaptureWindow(IntPtr handle) in E:\Ticodi Software\ReviewInSight\ScreenCapture.vb:line 38
at ReviewInsight.ScreenCapture.CaptureScreen() in E:\Ticodi Software\ReviewInSight\ScreenCapture.vb:line 10
at ReviewInsight.ScreenCapture.CaptureDeskTopRectangle(Rectangle CapRect, Int32 CapRectWidth, Int32 CapRectHeight) in E:\Ticodi Software\ReviewInSight\ScreenCapture.vb:line 59
at ReviewInsight.frmCommManager.captureScreen() in E:\Ticodi Software\ReviewInSight\frmCommManager.vb:line 2824
at ReviewInsight.frmMain.btnCapture_Click(Object sender, EventArgs e) in E:\Ticodi Software\ReviewInSight\frmMain.vb:line 1963
at DevComponents.DotNetBar.BaseItem.RaiseClick(eEventSource source)
at DevComponents.DotNetBar.BaseItem.InternalMouseUp(MouseEventArgs objArg)
at DevComponents.DotNetBar.PopupItem.InternalMouseUp(MouseEventArgs objArg)
at DevComponents.DotNetBar.ButtonItem.InternalMouseUp(MouseEventArgs objArg)
at DevComponents.DotNetBar.BaseItem.InternalMouseUp(MouseEventArgs objArg)
at DevComponents.DotNetBar.ItemContainer.InternalMouseUp(MouseEventArgs objArg)
at DevComponents.DotNetBar.ItemControl.OnMouseUp(MouseEventArgs e)
at DevComponents.DotNetBar.RibbonBar.OnMouseUp(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at DevComponents.DotNetBar.ItemControl.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at ReviewInsight.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.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Any help that anyone can give me would be really apreshiated
Thanks in advance
Simon
I am at a bit of a loss, I have had a look on the internet, but everything I seem to find appears to be related to web apps. and access permissions. This is a window based application and therefore can not seem to find an answer.
Below is the code I am running:
VB.NET:
Public Function CaptureWindow(ByVal handle As IntPtr) As Image
Dim SRCCOPY As Integer = &HCC0020
' get te hDC of the target window
Dim hdcSrc As IntPtr = User32.GetWindowDC(handle)
' get the size
Dim windowRect As New User32.RECT
User32.GetWindowRect(handle, windowRect)
Dim width As Integer = windowRect.right - windowRect.left
Dim height As Integer = windowRect.bottom - windowRect.top
' create a device context we can copy to
Dim hdcDest As IntPtr = GDI32.CreateCompatibleDC(hdcSrc)
' create a bitmap we can copy it to,
' using GetDeviceCaps to get the width/height
Dim hBitmap As IntPtr = GDI32.CreateCompatibleBitmap(hdcSrc, width, height)
' select the bitmap object
Dim hOld As IntPtr = GDI32.SelectObject(hdcDest, hBitmap)
' bitblt over
GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY)
' restore selection
GDI32.SelectObject(hdcDest, hOld)
' clean up
GDI32.DeleteDC(hdcDest)
User32.ReleaseDC(handle, hdcSrc)
' get a .NET image object for it
Dim img As Image = Image.FromHbitmap(hBitmap)
' free up the Bitmap object
GDI32.DeleteObject(hBitmap)
Return img
End Function 'CaptureWindow
The line of code that gives the error is:
VB.NET:
Dim img As Image = Image.FromHbitmap(hBitmap)
Below is also the stack trace:
at System.Drawing.Image.FromHbitmap(IntPtr hbitmap, IntPtr hpalette)
at System.Drawing.Image.FromHbitmap(IntPtr hbitmap)
at ReviewInsight.ScreenCapture.CaptureWindow(IntPtr handle) in E:\Ticodi Software\ReviewInSight\ScreenCapture.vb:line 38
at ReviewInsight.ScreenCapture.CaptureScreen() in E:\Ticodi Software\ReviewInSight\ScreenCapture.vb:line 10
at ReviewInsight.ScreenCapture.CaptureDeskTopRectangle(Rectangle CapRect, Int32 CapRectWidth, Int32 CapRectHeight) in E:\Ticodi Software\ReviewInSight\ScreenCapture.vb:line 59
at ReviewInsight.frmCommManager.captureScreen() in E:\Ticodi Software\ReviewInSight\frmCommManager.vb:line 2824
at ReviewInsight.frmMain.btnCapture_Click(Object sender, EventArgs e) in E:\Ticodi Software\ReviewInSight\frmMain.vb:line 1963
at DevComponents.DotNetBar.BaseItem.RaiseClick(eEventSource source)
at DevComponents.DotNetBar.BaseItem.InternalMouseUp(MouseEventArgs objArg)
at DevComponents.DotNetBar.PopupItem.InternalMouseUp(MouseEventArgs objArg)
at DevComponents.DotNetBar.ButtonItem.InternalMouseUp(MouseEventArgs objArg)
at DevComponents.DotNetBar.BaseItem.InternalMouseUp(MouseEventArgs objArg)
at DevComponents.DotNetBar.ItemContainer.InternalMouseUp(MouseEventArgs objArg)
at DevComponents.DotNetBar.ItemControl.OnMouseUp(MouseEventArgs e)
at DevComponents.DotNetBar.RibbonBar.OnMouseUp(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at DevComponents.DotNetBar.ItemControl.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at ReviewInsight.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.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Any help that anyone can give me would be really apreshiated
Thanks in advance
Simon