CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2012
    Posts
    2

    help with picturebox in winform

    hi

    I want to show several pictures in my picturebox.
    not simultaneously, but according to the user's choise in the listbox.
    when I try to define the path of the pictures with:
    Code:
    try
                {
                    pictureBox2.ImageLocation = @"\moskuitoPicture.jpg";
    
                    if (AnimalsList.SelectedItem.ToString() == "moskuito")
                    pictureBox2.Image = @"\moskuitoPicture.jpg";
                }
                catch
                {
                    MessageBox.Show("failed to load image");
                }
    I get an error " Cannot implicitly convert type 'string' to 'System.Drawing.Image"/
    the pictures are at library called resources that I added to my project.

    I defined a location in the designer, and I guess I do not have to pic any picture or initial picture.
    can someone help?
    I am using visual studio 2010 express framework 4 on windows 7.

    thanx
    Last edited by BioPhysEngr; April 22nd, 2012 at 12:08 AM. Reason: add code tags

  2. #2
    Join Date
    Oct 2011
    Posts
    97

    Re: help with picturebox in winform

    Please use code tags from now on. Also, the error code is a dead giveaway of what's wrong. It says it can't convert from String to Image. Well, if you went to the MSDN documentation page, you would see this:

    http://msdn.microsoft.com/en-us/libr...box.image.aspx

    The Image property is indeed an Image object, not a string. So you have to find a method that takes a string and gets the image. Scroll down the documentation page and you find this:

    http://msdn.microsoft.com/en-us/library/f6ak7was.aspx

    That Load method will take a path and load the image.

    I realize that the documentation doesn't have many examples, kind of like PHP does, but it is very good documentation, and you won't get anywhere without it.

  3. #3
    Join Date
    Feb 2012
    Posts
    13

    Re: help with picturebox in winform

    Hi Shay640 - I have just been playing around with a PictureBox on a Form so perhaps this may help you :-

    I used a ComboBox to hold the names of the pictures which I may wish to display but this operate in much the same way as the the ListBox you mentioned. I have two method which I use - one to select the picture folder name which I require to show and a secon one to actually load the picture into the PictureBox. :-
    Code:
                             private void PhotoNames_SelectedIndexChanged(object sender, EventArgs e)
                               {
                                   photofile = Convert.ToString(PhotoNames.SelectedItem);
                               }
    
                            private void pictureViewerToolStripMenuItem_Click(object sender, EventArgs e)
                              {
                                  photofile = pictureBox1.ImageLocation + photofile; 
                                  pictureBox1.Visible = true; 
                                  pictureBox1.Load(photofile);
                              }
    PhotoNames is the name of the Combobox and has an event ' SelectedIndexChanged ' set up.

    Hope this is of some help.
    Last edited by BioPhysEngr; April 22nd, 2012 at 12:07 AM. Reason: added code and /code tags

  4. #4
    Join Date
    Apr 2012
    Posts
    2

    Re: help with picturebox in winform

    what is photofile? is it a string?
    because I get an error
    Error The name 'photofile' does not exist in the current context

  5. #5
    Join Date
    Oct 2011
    Posts
    97

    Re: help with picturebox in winform

    Literally two posts above your I posed the documentation for the Load function. Looking at that would tell you that yes, photofile is a String.

  6. #6
    Join Date
    Feb 2012
    Posts
    13

    Re: help with picturebox in winform

    Yes 'photofile is a string variable which is declared as a global variable at the start just inside the Class declaration eg:-

    string photofile;

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

Featured