# How to Switch an Image in pictureBox?

Page 1 of 1

## 7 Replies - 430 Views - Last Post: 23 June 2017 - 11:07 AM

### #1 hexagod

Reputation: 5
• Posts: 242
• Joined: 29-October 16

# How to Switch an Image in pictureBox?

Posted 23 June 2017 - 09:03 AM

Happy friday to all you working coderz,
Quick question from a n00blet:
anyone know how to switch the image in a pictureBox when you're using VC++/.NET?

There are a ton of answers on how to do it with C# but I tried doing syntax conversions and unlike some things, this one doesn't seem there's cross syntaxability (not a word, I know).

I tried:
```pictureBox1->Image = Image->FromFile("C:/mypicture.jpg")

```

.. didn't work.

Also tried my usual: which is to just take the code that was generated by the forms designer and mod it. But that didn't work either:

```pictureBox1->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"pictureBox2.Image")));

```

compiler error:

"identifier 'resources' is undefined"

halp?

Is This A Good Question/Topic? 0

## Replies To: How to Switch an Image in pictureBox?

### #2 modi123_1

• Suitor #2

Reputation: 13390
• Posts: 53,439
• Joined: 12-June 08

## Re: How to Switch an Image in pictureBox?

Posted 23 June 2017 - 09:14 AM

Didn't work as in nothing happened even if you minimized/restored the window, or didn't work as in threw an exception?

### #3 hexagod

Reputation: 5
• Posts: 242
• Joined: 29-October 16

## Re: How to Switch an Image in pictureBox?

Posted 23 June 2017 - 09:33 AM

Exceptions for days... no compile.

I had a few techniques I tried yesterday night that just didn't change the image but I can't remember the lines for those.

both of those throw exceptions, though.

I'm guessing the second thing I tried didn't work because it's not in the right scope. But usually I can just take the designer code and change it then throw it inside a button EventArg. Not in this case.

### #4 modi123_1

• Suitor #2

Reputation: 13390
• Posts: 53,439
• Joined: 12-June 08

## Re: How to Switch an Image in pictureBox?

Posted 23 June 2017 - 09:36 AM

M'kay.. care to share the exceptions?

Also have you looked at the documentation? Say anything from Image::FromFile ?
https://msdn.microso...y/0t3sakh9.aspx

Don't forget \\ and not just \.

### #5 hexagod

Reputation: 5
• Posts: 242
• Joined: 29-October 16

## Re: How to Switch an Image in pictureBox?

Posted 23 June 2017 - 09:58 AM

I did read that guide. I'll try it again and post the exceptions. Also, I DO remember the \\ !!! yes that was a very nice tip. Use it daily.

### #6 hexagod

Reputation: 5
• Posts: 242
• Joined: 29-October 16

## Re: How to Switch an Image in pictureBox?

Posted 23 June 2017 - 10:37 AM

Modi, actually I didn't read that one.... it's different than the one I found. Ty

### #7 hexagod

Reputation: 5
• Posts: 242
• Joined: 29-October 16

## Re: How to Switch an Image in pictureBox?

Posted 23 June 2017 - 11:03 AM

```private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
{
Image^ image = Image::FromFile("C:\\YOURFILEPATHHERE\\2.png");
pictureBox1->Image = image;

}

```

Modi!!! yourethemannowdog.com

THANKS!!!!!!!!!!!!!!!!!!

### #8 modi123_1

• Suitor #2

Reputation: 13390
• Posts: 53,439
• Joined: 12-June 08

## Re: How to Switch an Image in pictureBox?

Posted 23 June 2017 - 11:07 AM

Good ol' scope resolution operators.
https://en.wikipedia...perator#Example