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.
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]:
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.
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
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
-
- namespace UploadControl
- {
- public partial class Form1 : Form
- {
- public static string l1;
- public static string l2;
-
- public Form1()
- {
- InitializeComponent();
-
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- OpenFileDialog opnfd = new OpenFileDialog();
- opnfd.Filter = "Image Files (*.jpg;*.jpeg;.*.gif;)|*.jpg;*.jpeg;.*.gif";
- if (opnfd.ShowDialog()== DialogResult.OK)
- {
- pictureBox1.Image = new Bitmap(opnfd.FileName);
- }
-
-
-
-
- }
-
- private void button2_Click(object sender, EventArgs e)
- {
-
- l1 = textBox1.Text;
- l2 = textBox2.Text;
- Form2 frm2 = new Form2(pictureBox1.Image);
-
- frm2.Show();
- }
- }
- }
Form2.cs - using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
-
- namespace UploadControl
- {
- public partial class Form2 : Form
- {
- public Form2( Image pic1)
- {
- InitializeComponent();
- pictureBox1.Image = pic1;
-
- label1.Text = Form1.l1;
- label2.Text = Form1.l2;
-
-
-
- }
- }
- }
Output chamber 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: Form2: Here I include only Name and Email, you can add many more details. Hope you liked it. Thank you for reading.