-1

I've got three images saved in my project, 1.jpg 2.jpg and 3.jpg and a picture box that currently displays nothing. I also have three radio buttons. What I'm trying to do is when I click radio button one, the picture box displays 1.jpg, when I click radio button 2, 2.jpg displays and so on.

I'm trying to get some code that looks something like this ..

private void imageOne_CheckedChanged(object sender, EventArgs e)
        {
            imageContainer.Image = "1.jgp";
        }

But I get an error saying I can't convert a string to a System.Drawing.Image

What is the correct way of approaching this?

oneman
  • 811
  • 1
  • 13
  • 31
  • 2
    You need to refer the image and not the file name. – Muralidharan Deenathayalan Mar 30 '17 at 07:20
  • 1
    why all the down votes? this is literally the third 'program' I've written in c#, I'm new to the language and havent learned the syntax yet – oneman Mar 30 '17 at 07:22
  • 2
    @oneman `.. = Image.FromFile("1.jpg");` – Zein Makki Mar 30 '17 at 07:24
  • 1
    Try to check if these solutions work first: http://stackoverflow.com/questions/17348042/converting-from-string-to-image-in-c-sharp – user2657943 Mar 30 '17 at 07:25
  • 1
    `"1.jpg"` is a `string`. In other words, you are trying to assign a string of characters to `imageContainer.Image`. You should assign an `Image` value instead. – JMadelaine Mar 30 '17 at 07:27
  • I suspect you got the downvotes because you don't show much research effort. For example, you state you got an error, but have you tried searching on that error either on Stack Overflow or on Google? It's good to include what you have found by searching, and why what you have found doesn't give you a solution to your problem. – PJvG Mar 30 '17 at 08:24
  • Also, have you looked at [the page of the Image class](https://msdn.microsoft.com/en-us/library/system.drawing.image(v=vs.110).aspx)? It contains an example how to create an `Image`. A lot of beginner problems can be solved by just reading [the documentation on MSDN](https://msdn.microsoft.com/en-us/library/) and trying out code examples. – PJvG Mar 30 '17 at 08:32

1 Answers1

2

Import the images to resource folder Then load the picture from Resouce

 PictureBox.Image = Properties.Resources._2;

In your code file,

private void imageOne_CheckedChanged(object sender, EventArgs e)
        {
            imageContainer.Image = Properties.Resources._2;
        }
Santhosh
  • 729
  • 7
  • 19