Load image into picture box when a button is clicked

Hi i have searched the web for 2 hours trying to find this answer and i couldnt find it! All i want to do is when a button is clicked for it to load a image.
I have tryed this:
pictureBox1.Image = Image.FromFile(2c.gif);


But i get this error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1
1>  stdafx.cpp
1>  AssemblyInfo.cpp
1>  Black Jack.cpp
1>c:\users\christian\documents\visual studio 2010\projects\black jack\black jack\Form1.h(94): error C2059: syntax error : 'bad suffix on number'
1>c:\users\christian\documents\visual studio 2010\projects\black jack\black jack\Form1.h(94): error C2228: left of '.Image' must have class/struct/union
1>          type is 'System::Windows::Forms::PictureBox ^'
1>          did you intend to use '->' instead?
1>c:\users\christian\documents\visual studio 2010\projects\black jack\black jack\Form1.h(94): warning C4832: token '.' is illegal after UDT 'System::Drawing::Image'
1>          c:\program files\reference assemblies\microsoft\framework\.netframework\v4.0\system.drawing.dll : see declaration of 'System::Drawing::Image'
1>c:\users\christian\documents\visual studio 2010\projects\black jack\black jack\Form1.h(94): error C2275: 'System::Drawing::Image' : illegal use of this type as an expression
1>          c:\program files\reference assemblies\microsoft\framework\.netframework\v4.0\system.drawing.dll : see declaration of 'System::Drawing::Image'
1>c:\users\christian\documents\visual studio 2010\projects\black jack\black jack\Form1.h(94): error C2228: left of '.FromFile' must have class/struct/union
1>c:\users\christian\documents\visual studio 2010\projects\black jack\black jack\Form1.h(94): error C2146: syntax error : missing ')' before identifier 'c'
1>c:\users\christian\documents\visual studio 2010\projects\black jack\black jack\Form1.h(94): error C2059: syntax error : ')'
1>  Generating Code...
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========


This looks like some kind of Windows .NET language. You might have better luck on a forum for that.
its microsoft visual c++
you mean C#? that isn't C++, in fact I hear a lot of people saying its more like Java then it is C++.

or is there a GUI library you're using for c++? if so it would help to know what it is, and if you could post the code that errors so we can take a look.

Last edited on
Image.FromFile(2c.gif);
I expect you meant
Image.FromFile("2c.gif");

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

It's not C++, even if it says it is. It's some kind of hideous "managed" language.
im not sure what u mean but im using windows fourm application and i found a way that works and builds but then when i try hitting the button an error popps up saying:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.ArgumentException: Illegal characters in path.
   at System.IO.Path.CheckInvalidPathChars(String path)
   at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength)
   at System.IO.Path.GetFullPathInternal(String path)
   at System.IO.Path.GetFullPath(String path)
   at System.Drawing.IntSecurity.UnsafeGetFullPath(String fileName)
   at System.Drawing.IntSecurity.DemandReadFileIO(String fileName)
   at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
   at System.Drawing.Image.FromFile(String filename)
   at BlackJack.Form1.button1_Click(Object sender, EventArgs e) in c:\users\christian\documents\visual studio 2010\projects\black jack\black jack\form1.h:line 107
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
Black Jack
    Assembly Version: 1.0.4592.9290
    Win32 Version: 
    CodeBase: file:///C:/Users/Christian/Documents/Visual%20Studio%202010/Projects/Black%20Jack/Debug/Black%20Jack.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.


yes moschops i did mean the quotes.


if its not c++ then how in c++ can i change the image of a picturebox by clicking a button
1
2
3
4
5
6
7
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
					pictureBox1->Image = ????


			 }

Last edited on
You can't, c++ doesn't provide you with any sort of buttons, forms, windows, pictures, etc. unless you use a library that does provide you with them, like Qt, wxWidgets, SFML, SDL, etc.

Basically, this is the wrong forum to ask for help, sorry.
Last edited on
To expand on Zephilinox's answer, C++ has no knowledge of images, windows, monitors, buttons (or indeed, any kind of hardware at all, including the keyboard you're using).

To do what you're asking, essentially C++ asks the operating system to do it, via the provided API. There are two ways to do it; directly, or using a library that calls the API for you, leaving you to ask the library.

You have chosen to use a set of libraries provided by Microsoft known generally as .NET

There are very few .NET users here (I don't even use Windows, so I've got no idea about it).

As an aside, here is how I would do such a thing, using a 3rd party library known as CImg.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "CImg.h"
using namespace cimg_library;

int main() {
  CImg<unsigned char> image("lena.jpg");
  CImgDisplay main_disp(image.width(),image.height(),"Click a point");
  while (!main_disp.is_closed())
 {
    main_disp.wait();
    if (main_disp.button()) 
    {
      main_disp.display(image);
    }
  }
  return 0;
}


with a build command of
g++ 264.cpp -lX11 -pthread

This command my compile/linker chain to take the file 264.cpp, compile it, and then link it into an executable using the X11 and pthread libraries for the needed additional functions called by the 3rd party library CImg.

It creates a window, and when I click inside the window, an image is shown there.

Last edited on
Never mind I figured it out myself it's:
 
pictureBox1->Load(2c.gif);

Really? I thought Load took a string; something like
pictureBox1->Load("2c.gif");
closed account (o1vk4iN6)
Microsoft's managed C++ is probably the worst thing you can be learning, as it is not used anywhere. If you want to use the .Net framework just use C# as it was built for that purpose.
Topic archived. No new replies allowed.