MS .Net Controls: The Picture Box


 

A picture box is a rectangular object that can serve different purposes. It can be used to host or hold other controls, it can be used as a drawing board instead of drawing directly on a form, it can be used to display a picture on an application, it can also be used for aesthetic reasons.

To create a picture box, use the PictureBox class.
Picture Box Example Serena Williams: Une Lionne Indomptable

  1. Start Microsoft Visual Studio .NET
  2. On the Start Page, click New Project (alternatively, on the main menu, you can click File -> New -> Project...)
  3. On the New Project dialog box, in the Project Types tree list, click Visual C++ Projects
  4. In the Templates list, click Managed C++ Empty Project
  5. In the Name edit box, replace the <Enter name> content with Picture Box
  6. In the Location combo box, accept the suggestion or type your own. If you don't have any, type C:\Programs\MSVC.NET
  7. Click OK
  8. On the main menu, click Project -> Add New Item...
  9. In the Add New Item dialog box, in the Templates list, click C++ File
  10. In the Name box, replace <Enter name> with Main and click OK
  11. Replace the contents of the empty file with the following:
     
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc class SimpleForm : public Form
    {
    public:
    	SimpleForm();
    private:
    	PictureBox *PictBox;
    };
    
    SimpleForm::SimpleForm()
    {
    	// The caption of the form
    	this->Text = S"Picture Box Example";
    
    	PictBox = new PictureBox;
    	PictBox->BorderStyle = BorderStyle::FixedSingle;
    	PictBox->BackColor = Drawing::Color::White;
    	PictBox->Location = Point(16, 16);
    	PictBox->Size = Drawing::Size(240, 180);
    	// After creating the control, add it to the
    	// group of controls of the form
    	Controls->Add(PictBox);
    }
    
    int __stdcall WinMain()
    {
    	SimpleForm *SF = new SimpleForm();
    	Application::Run(SF);
    
    	return 0;
    }
  12. Test the application:
     
    A Picture Box Example
  13. To prepare a picture to show, from this page, right-click the second picture and save it as serena.jpg. Either save it to the folder that contains the current project or copy and paste it to the folder of the current project.
  14. To display the image in the picture box, change the source file as follows:
     
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc class SimpleForm : public Form
    {
    public:
    	SimpleForm();
    private:
    	PictureBox *PictBox;
    };
    
    SimpleForm::SimpleForm()
    {
    	// The caption of the form
    	this->Text = S"Picture Box Example";
    	// The size of the form
    	this->Size = Drawing::Size(300, 480);
    
    	PictBox = new PictureBox;
    	PictBox->BorderStyle = BorderStyle::FixedSingle;
    	PictBox->BackColor = Drawing::Color::White;
    	PictBox->Location = Point(16, 16);
    	PictBox->Size = Drawing::Size(262, 392);
    	// The following line assumes that the picture is in the same
    	// folder of the application
    	PictBox->Image = Image::FromFile("Serena.jpg");
    	// After creating the control, add it to the
    	// group of controls of the form
    	Controls->Add(PictBox);
    }
    
    int __stdcall WinMain()
    {
    	SimpleForm *SF = new SimpleForm();
    	Application::Run(SF);
    
    	return 0;
    }
    An image in a picture box
  15. Test the application
 

Home Copyright © 2002-2005 FunctionX, Inc.