Upload Photo Using PictureBox In Windows Application

We write some upload code to set our profile pic with some necessary details and pass/display it to the other form.

Initial chamber

Step 1: Open Your Visual Studio 2010, Go to File, New , Projects, then under Visual C# select Windows.

Window form application

You can change the name of the project and browse your project to different location too. And then press – OK.

Design chamber

Step 2: Now open your Form1.cs file, where we create our design for Upload Image. We will drag PictureBox, two buttons, two labels and two textboxes from toolbox to Form1.cs. You will see your Form look like this.

Form1.cs [Design]:

Form Design

Add another form that is Form2.cs to your Project by going to solution explorer- Right Click to your Project, Add New item, select Window Form and Add it.

Form2.cs [Design]:

Here you have to add PictureBox and two Labels from the toolbox to the Form2.cs. Just Like you see in the following image.

design

Code chamber

Right click on the blank part of Form1.cs and View Code. You will see you entered in the code part of the form. Write the following code for Form1.cs.

Form1.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace UploadControl  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         public static string l1;  
  16.         public static string l2;  
  17.   
  18.         public Form1()  
  19.         {  
  20.             InitializeComponent();  
  21.             
  22.         }  
  23.   
  24.         private void button1_Click(object sender, EventArgs e)  
  25.         {  
  26.             OpenFileDialog opnfd = new OpenFileDialog();  
  27.             opnfd.Filter = "Image Files (*.jpg;*.jpeg;.*.gif;)|*.jpg;*.jpeg;.*.gif";  
  28.             if (opnfd.ShowDialog()== DialogResult.OK)  
  29.             {  
  30.                 pictureBox1.Image = new Bitmap(opnfd.FileName);  
  31.             }  
  32.   
  33.               
  34.   
  35.   
  36.         }  
  37.   
  38.         private void button2_Click(object sender, EventArgs e)  
  39.         {  
  40.   
  41.            l1 = textBox1.Text;  
  42.             l2 = textBox2.Text;  
  43.             Form2 frm2 = new Form2(pictureBox1.Image);  
  44.              
  45.             frm2.Show();  
  46.         }  
  47.     }  
  48. }  
Form2.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace UploadControl  
  12. {  
  13.     public partial class Form2 : Form  
  14.     {  
  15.         public Form2( Image pic1)  
  16.         {  
  17.             InitializeComponent();  
  18.             pictureBox1.Image = pic1;  
  19.   
  20.             label1.Text = Form1.l1;  
  21.             label2.Text = Form1.l2;  
  22.   
  23.          
  24.   
  25.         }  
  26.     }  
  27. }  
Output chamber

Output

So, when you click on Upload Photo button you will see File Dialog will open to browse your image in picture box. You can select any picture and set it to the picturebox, then you have to fill the rest of the thing and press Submit Button, you will see a Form2 will open displaying all the details of Form1 along with your image.

Form1:

Form1

Form2:

Form2

Here I include only Name and Email, you can add many more details. Hope you liked  it. Thank you for reading.

 


Similar Articles