Invalid parameter used.- PictureBox

"Invalid parameter used."
When i tried to show an image in a picturebox i got an error as folows:

System.ArgumentException: Invalid parameter used.
   at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
   at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)
   at System.Drawing.Graphics.DrawImage(Image image, Rectangle rect)
   at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Not satisfied with the solutions available on net. I tried a workaround which i want to share..

Reason :
 When you set the Image roperty of a PictureBox which is having width or height grearter than 32767.This the limit of PictureBox image width.

 Create your own PictureBox Class
 Private Class MyPictureBox
         Inherits System.Windows.Forms.PictureBox
         Public Event WrongImage(ByVal Message As String)

         Protected Overrides Sub OnSizeModeChanged(ByVal e As System.EventArgs)
  If Me.Image.Width >= 32767 OrElse Me.Image.Height >= 32767 Then
   Me.Image = Nothing
                  RaiseEvent WrongImage("Image is in incorrect format.Please verify manually.")
  End If
        End Sub
            End Class

Use the above class as your PictureBox control on your form and handle the WrongImage event to take the necessary action.
  Private Sub picSign_WrongImage(ByVal Message As String) Handles picSign.WrongImage
        lblSign.Text = Message
  End Sub

Note: The stacktrace of an exception indicates that the Exception is raised internally , not by the form or picturebox we code.
 As i am overriding OnSizeModeChanged method , do set SizeMode property of the PictureBox on your form

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.