Results 1 to 19 of 19

Thread: PixelFormat.Format32bppArgb causing exception

Threaded View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    PixelFormat.Format32bppArgb causing exception

    After the 55th time my code is run, I get the following exception:
    An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll

    Additional information: Parameter is not valid.
    The line that triggers this is:
    vbnet Code:
    1. 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:
    1. Private Sub tmrRefresh_Tick(sender As Object, e As EventArgs) Handles tmrRefresh.Tick
    2.  
    3.         tmrRefresh.Interval = getRedrawSpeed()
    4.         Me.BackgroundImage = resizeImage(takePicture(), Me.Width, Me.Height)
    5.  
    6.     End Sub
    7.  
    8.     Private Function takePicture() As System.Drawing.Bitmap
    9.  
    10.         Dim area As Rectangle
    11.  
    12.         Dim capture As System.Drawing.Bitmap
    13.  
    14.         Dim graph As Graphics
    15.  
    16.         If getRelativeSetting(relativeSettings.relativeHeight) Then
    17.             area.Height = ((getGrabHeight() / 100) * Screen.PrimaryScreen.Bounds.Height)
    18.         Else
    19.             area.Height = getGrabHeight()
    20.         End If
    21.  
    22.         If getRelativeSetting(relativeSettings.relativeWidth) Then
    23.             area.Width = ((getGrabWidth() / 100) * Screen.PrimaryScreen.Bounds.Width)
    24.         Else
    25.             area.Width = getGrabWidth()
    26.         End If
    27.  
    28.         If getRelativeSetting(relativeSettings.relativeX) Then
    29.             area.X = ((getXPosition() / 100) * Screen.PrimaryScreen.Bounds.Width)
    30.         Else
    31.             area.X = getXPosition()
    32.         End If
    33.  
    34.         If getRelativeSetting(relativeSettings.relativeY) Then
    35.             area.Y = ((getYPosition() / 100) * Screen.PrimaryScreen.Bounds.Height)
    36.         Else
    37.             area.Y = getYPosition()
    38.         End If
    39.  
    40.         capture = New System.Drawing.Bitmap(area.Width, area.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
    41.  
    42.         graph = Graphics.FromImage(capture)
    43.  
    44.         graph.CopyFromScreen(area.X, area.Y, 0, 0, area.Size, CopyPixelOperation.SourceCopy)
    45.  
    46.         Return capture
    47.  
    48.     End Function
    49.  
    50.     Private Function resizeImage(ByVal theImage As System.Drawing.Bitmap, newXPixels As Integer, newYPixels As Integer) As System.Drawing.Bitmap
    51.  
    52.         Dim returnImage As New System.Drawing.Bitmap(newXPixels, newYPixels)
    53.  
    54.         Dim objResizer As Graphics = Graphics.FromImage(returnImage)
    55.  
    56.         objResizer.DrawImage(theImage, 0, 0, returnImage.Width + 1, returnImage.Height + 1)
    57.  
    58.         Return returnImage
    59.  
    60.     End Function
    Last edited by Slyke; Oct 27th, 2013 at 12:04 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width