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.Dra

ITPro Today

August 18, 2005

1 Min Read
ITPro Today logo in a gray background | ITPro Today

"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.

Workaround:
 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

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like