-
April 21st, 2012, 06:27 AM
#1
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
-
April 21st, 2012, 10:01 AM
#2
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.
-
April 21st, 2012, 10:14 AM
#3
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
-
April 22nd, 2012, 07:44 AM
#4
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
-
April 22nd, 2012, 09:38 AM
#5
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.
-
April 22nd, 2012, 10:13 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|