I have written some code to capture a image of the desktop and preview it within a picturebox, this works fine. The problem comes when I try to repeat the process and the following error is produced
"An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in system.drawing.dll
Additional information: A generic error occurred in GDI+."
I can only presume this is because my picturebox is still keeping the file open. There is not problem in overwritting the file if I close my program and open it again. I have tried pointing the picturebox image to nothing but this does not work.
Thanks
Simon
"An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in system.drawing.dll
Additional information: A generic error occurred in GDI+."
I can only presume this is because my picturebox is still keeping the file open. There is not problem in overwritting the file if I close my program and open it again. I have tried pointing the picturebox image to nothing but this does not work.
Visual Basic:
Private Sub btnCapture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapture.Click
Dim SC As New ScreenShot.ScreenCapture
Me.picPreview.Image = Nothing
Dim MyBitMap As Bitmap = SC.CaptureDeskTopRectangle(New Rectangle(MRCSData.Instance.X1, MRCSData.Instance.Y1, _
MRCSData.Instance.X2, MRCSData.Instance.Y2), Math.Abs(MRCSData.Instance.X1 - MRCSData.Instance.X2), Math.Abs(MRCSData.Instance.Y1 - MRCSData.Instance.Y2))
MyBitMap.Save("c:\desktopregion.jpg", Imaging.ImageFormat.Jpeg)
Dim path As String = "c:\desktopregion.jpg"
Me.picPreview.Image = Image.FromFile(path)
Dim myBMP As Bitmap = New Bitmap(path)
Me.pnlPreview.Size = New Size(520, 420)
If myBMP.Width < Me.pnlPreview.Width Then
Me.pnlPreview.Width = myBMP.Width
End If
If myBMP.Height < Me.pnlPreview.Height Then
Me.pnlPreview.Height = myBMP.Height
End If
Me.pnlPreview.AutoScroll = True
Me.pnlPreview.AutoScrollMinSize = myBMP.Size
End Sub
Thanks
Simon