Creating Picture Viewer Using C# in .NET 4.5

Introduction

 
In this article, you will learn how to create a Picture Viewer using C# in .NET 4.5.
 
Step 1
 
Create a new Windows Application by selecting Windows Form Application from the starting page of the Visual Studio. Do the following changes in the Property section of the form:
  1. Change its size to 550,350.
  2. Set its Text to Picture Viewer.
PicView1.jpg
 
Step 2
 
Select a Table Layout Panel from the Toolbox. Change its dock Property to Fill.
 
PicView2.jpg
 
Now click on the Black small triangle given on the Table Layout Panel and select "Edit Rows and Columns".
 
PicView3.jpg
 
Step 3
 
Now click on the Percent of Column1 and change it to 15% then change the Percent of Column2 to 85%.
 
PicView4.jpg
 
Now click on the Drop Down Button and select the Rows. Change the Percent of Row1 to 90% and Row2 to 10% and then click "Ok".
 
PicView5.jpg
 
Now your Form will look something like this:
 
PicView6.jpg
 
Step 4
 
Add a Picture box to your form by choosing it from the Toolbox. Change its Dock Property by clicking on the Black triangle and selecting "Dock in Parent Container".
 
PicView7.jpg
 
Make the Picture Box span both the columns by changing its Column Span Property. Change the Column Span Property of Picture Box to 2, also set the Border Style Property to Fixed3D so that when no image is selected then it will show a blank frame.
 
PicView8.jpg
 
Step 5
 
Now add a check Box from the Toolbox, it would be added to the next free cell and rename it "Stretch". It will be used to stretch your image on just a single click. Then again go to the Toolbox and pick a Flow Layout Panel, change its Dock property to Fill.
 
PicView9.jpg
 
Step 6
 
Select a Button control from the Toolbox and copy it four times. Change the Text Property of each button as follows:
  1. Select
  2. Clear
  3. Set the Background Color
  4. Close
Also, change their name as follows:
  1. selectbtn
  2. clearbtn
  3. bckgrndbtn
  4. closebtn
Click on any of the buttons, hold the CTRL button, and select all four buttons. After that go to the Property and change the Autosize Property to True, this will make the buttons adjust themselves automatically so that all the Text can be seen. Now your form will look like this:
 
PicView10.jpg
 
Step 7
 
Now you must select two Dialog Components for your Form. First, select the OpenFileDialog and then select the ColorDialog from the Toolbox.
 
PicView11.jpg
 
Now you must make some changes in the Property of openFileDialog like:
  1. Change its Filter Property to "JPEG Files (*.jpg)|*.jpg|PNG Files (*.png)|*.png|BMP Files (*.bmp)|*.bmp|All files (*.*)|*.*". You can just copy this one from here.
  2. Change its Title Property to "Select a Picture".
PicView12.jpg
 
Step 8
 
Now the main part is the code. You must write the following code in the "Select" button so that it can use OpenFileDialog to browse a file and open it.
  1. private void selectbtn_Click(object sender, EventArgs e)  
  2. {  
  3.     if (openFileDialog1.ShowDialog() == DialogResult.OK)  
  4.     {  
  5.         pictureBox1.Load(openFileDialog1.FileName);  
  6.     }  
  7. }  
Double-click on the "Clear" button and write this code:
  1. private void clearbtn_Click(object sender, EventArgs e)  
  2. {  
  3.     pictureBox1.Image = null;  

This will be used to clear the selected Image.
 
Double-click on the "Close" button and write this code:
  1. private void closebtn_Click(object sender, EventArgs e)  
  2. {  
  3.     this.Close();  

This will be used to close the output window.
 
Double-click on the "Set the Background Color" button and write the following code:
  1. private void bckgrndbtn_Click(object sender, EventArgs e)  
  2. {  
  3.     if (colorDialog1.ShowDialog() == DialogResult.OK)  
  4.         pictureBox1.BackColor = colorDialog1.Color;  

This will be used to change and set the Background color.
 
Double-click on "Check Box Stretch" and write the following code:
  1. private void checkBox1_CheckedChanged(object sender, EventArgs e)  
  2. {  
  3.     if (checkBox1.Checked)  
  4.         pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;  
  5.     else  
  6.         pictureBox1.SizeMode = PictureBoxSizeMode.Normal;  

Step 9
 
So, complete coding will be as follows:
  1. public partial class Form1: Form {  
  2.     public Form1() {  
  3.         InitializeComponent();  
  4.     }  
  5.   
  6.     private void selectbtn_Click(object sender, EventArgs e) {  
  7.         if (openFileDialog1.ShowDialog() == DialogResult.OK) {  
  8.             pictureBox1.Load(openFileDialog1.FileName);  
  9.         }  
  10.     }  
  11.   
  12.     private void clearbtn_Click(object sender, EventArgs e) {  
  13.         pictureBox1.Image = null;  
  14.     }  
  15.   
  16.     private void bckgrndbtn_Click(object sender, EventArgs e) {  
  17.         if (colorDialog1.ShowDialog() == DialogResult.OK)  
  18.             pictureBox1.BackColor = colorDialog1.Color;  
  19.     }  
  20.   
  21.     private void closebtn_Click(object sender, EventArgs e) {  
  22.         this.Close();  
  23.     }  
  24.   
  25.     private void checkBox1_CheckedChanged(object sender, EventArgs e) {  
  26.         if (checkBox1.Checked)  
  27.             pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;  
  28.         else  
  29.             pictureBox1.SizeMode = PictureBoxSizeMode.Normal;  
  30.     }  

Step 10
 
Now run your program and select the color of your choice by clicking on "Set The BackGround Color".
 
PicView13.jpg
 
Select an image by clicking on the "Select" button. Your image will look like this:
 
PicView14.jpg
 
As you click on "Stretch your Image" it will look like this:
 
PicView15.jpg