C++ Tutorial: Make Simple Picture Viewer

   In this tutorial we will make a simple program called “Picture viewer”. This program will open a selected picture or image and it will be shown on the main form of the program. In the process of coding of this program we will use Open File Dialog component and Picture box component. Preparation    First of all you need to create a new “Windows Forms Application” project. Then you need to put a Picture box component and Button component on the form. After that you also need to add Open File Dialog to the project. Before writing code you also need to change some default settings of Picture box component. In the properties of this control you need to click on “SizeMode” property and choose “ Zoom” value. Also you should change style of borders of this control by clicking on the “BorderStyle” property and changing it to “FixedSingle”. We did first change to make our picture look well on our form, because you can select a picture, which will not fit the form sizes. Second change is done to make interface of our form better. Code    To make our program alive we need to write code in the Button Click event function. The sample of code is below:
  1. private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
  2. openFileDialog1->ShowHelp=true;
  3. if(openFileDialog1->ShowDialog()==::System::Windows::Forms::DialogResult::OK)
  4. {
  5. pictureBox1->ImageLocation=openFileDialog1->FileName;
  6. }
  7. }
   In the first line we enable a help window of our dialog. Using that window we will choose image we are looking for. Then in the “if” block we catch event of clicking the button “OK” in the Open Dailog. Then it is done, we assign the path, which we got from the dialog, to the path of the image in the Picture Box control. Thanks.

Add new comment