Results 1 to 3 of 3

Thread: Display Error Image in PictureBox

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Display Error Image in PictureBox

    How do you display an error image in a picturebox if no image is located to display

    Me.PictureBox1.ImageLocation = (String.Format("{0}\{1}.jpg", Subject_Values.ToolStripStatusLabel3.Text, "Subject Front"))
    Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

    Thanks

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Display Error Image in PictureBox

    vb.net Code:
    1. Try
    2.             PictureBox1.Image = Image.FromFile("C:\test.txt") 'or whatever
    3.         Catch
    4.             PictureBox1.Image = PictureBox1.ErrorImage
    5.         End Try
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Display Error Image in PictureBox

    Prevention is generally better than cure. You can first check that there is a value using String.IsNullOrEmpty and, assuming there is a value, use File.Exists to check whether there is a such a file. If either of those fail then don't even try to load the image. You should still have the Try...Catch block anyway though, in case the file the user selects is not a valid image file.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Display Error Image in PictureBox

    Thanks for th insight John

    I am attemtipng to load/display 3 images in the new form and if 1 or 2 images is missing i still would like to be able to load the form
    thats why i was interested in having the error image be displayed for an image the was missing a picture

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