After the 55th time my code is run, I get the following exception:
The line that triggers this is:An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll
Additional information: Parameter is not valid.
vbnet Code:
capture = New System.Drawing.Bitmap(area.Width, area.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
I basically have a timer running in the background on one of my forms that takes a screenshot of the screen. After the 55th tick of the timer, this line of code causes an exception. This happens no matter the speed of the timer, or anything else I do in the program.
Relevant code:
vbnet Code:
Private Sub tmrRefresh_Tick(sender As Object, e As EventArgs) Handles tmrRefresh.Tick tmrRefresh.Interval = getRedrawSpeed() Me.BackgroundImage = resizeImage(takePicture(), Me.Width, Me.Height) End Sub Private Function takePicture() As System.Drawing.Bitmap Dim area As Rectangle Dim capture As System.Drawing.Bitmap Dim graph As Graphics If getRelativeSetting(relativeSettings.relativeHeight) Then area.Height = ((getGrabHeight() / 100) * Screen.PrimaryScreen.Bounds.Height) Else area.Height = getGrabHeight() End If If getRelativeSetting(relativeSettings.relativeWidth) Then area.Width = ((getGrabWidth() / 100) * Screen.PrimaryScreen.Bounds.Width) Else area.Width = getGrabWidth() End If If getRelativeSetting(relativeSettings.relativeX) Then area.X = ((getXPosition() / 100) * Screen.PrimaryScreen.Bounds.Width) Else area.X = getXPosition() End If If getRelativeSetting(relativeSettings.relativeY) Then area.Y = ((getYPosition() / 100) * Screen.PrimaryScreen.Bounds.Height) Else area.Y = getYPosition() End If capture = New System.Drawing.Bitmap(area.Width, area.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb) graph = Graphics.FromImage(capture) graph.CopyFromScreen(area.X, area.Y, 0, 0, area.Size, CopyPixelOperation.SourceCopy) Return capture End Function Private Function resizeImage(ByVal theImage As System.Drawing.Bitmap, newXPixels As Integer, newYPixels As Integer) As System.Drawing.Bitmap Dim returnImage As New System.Drawing.Bitmap(newXPixels, newYPixels) Dim objResizer As Graphics = Graphics.FromImage(returnImage) objResizer.DrawImage(theImage, 0, 0, returnImage.Width + 1, returnImage.Height + 1) Return returnImage End Function




Reply With Quote