How to Upload Picture Box in C#
In this postal service, you will learn how to upload and download any paradigm in the C# windows application. So permit'south outset.
Uploading Image in C# Windows Awarding
Hither what we will do is write some uploading code that takes our prototype in one form and by uploading it will brandish it on another form.
Stride 1:
Open up Your Visual Studio > Get to File menu > Select New Projects > then under Visual C# > select Windows Class Awarding .
We can change our project name and find that past browsing it on other locations after that click on the Ok push.
Step 2:
Open your Form1.cs file, where we have to create our pattern for Uploading Images. We will have one PictureBox, two labels, two buttons, and two textboxes from the toolbox and elevate them into Form1.cs. After that, we volition see our Course will look like this.
Step 3:
Now we volition add another form in our project that is Form2.cs merely become to> solution explorer >Right Click to your Project > Add New particular >select Window Class and Add it.
Hither we volition add one PictureBox and 2 Labels from the toolbox just by dragging them into the Form2.cs.
Pace 4:
On Form1.cs Correct-click on the bare role and View the Code. Hither we will see nosotros accept entered the coding part of the class. Now we will write the code for Form1.cs.
Form1.cs
using System; using Organization.Collections.Generic; using Organisation.ComponentModel; using System.Information; using System.Drawing; using System.Linq; using System.Text; using Organisation.Threading.Tasks; using Arrangement.Windows.Forms; namespace UploadControl{ public partial class Form1 : Course{ public static cord l1; public static string l2; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs eastward) { OpenFileDialog opnfd = new OpenFileDialog(); opnfd.Filter = "Image Files (*.jpg;*.jpeg;.*.gif;)|*.jpg;*.jpeg;.*.gif"; if (opnfd.ShowDialog()== DialogResult.OK) { pictureBox1.Paradigm = new Bitmap(opnfd.FileName); } } private void button2_Click(object sender, EventArgs e) { l1 = textBox1.Text; l2 = textBox2.Text; Form2 frm2 = new Form2(pictureBox1.Prototype); frm2.Show(); } } } Form2.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using Organisation.Drawing; using Organization.Linq; using Arrangement.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace UploadControl{ public fractional class Form2:Grade{ public Form2(Image pic1) { InitializeComponent(); pictureBox1.Image = pic1; label1.Text = Form1.l1; label2.Text = Form1.l2; } } } Step 5
When nosotros click on the Upload Photograph push then nosotros will see File Dialog will open where we can scan our image in the picture box. We can select any picture and set information technology into the PictureBox, so nosotros have to fill in the residuum data and then press Submit Button.
nosotros will come across a Form2 will open and it will display all the details of Form1 with our image which we have uploaded. The output volition expect like this.
Fetching Images from the database into a motion-picture show box control
We have learned how to insert images into a database and now nosotros will learn how to think or fetch an epitome from our SQL database and display it in a flick box control using the c# windows application.
Follow the simple steps-
Stride 1
Open Your Visual Studio > Get to File menu > Select New Projects > so under Visual C# > select Windows Grade Awarding > and insert a picture box and a push into the form.
Pace two
To retrieve an image from the SQL Server database write the code in the coding section of a form by right click on the form. Here you came on the coding part of the form write the code here.
using System; using System.Collections.Generic; using System.ComponentModel; using Organization.Information; using System.Drawing; using System.Linq; using System.Text; using Arrangement.Windows.Forms; using System.Data.SqlClient; using Organisation.IO; namespace insert_image_in_database { public partial course Form2 : Form{ public Form2() { InitializeComponent(); } SqlConnection con; SqlCommand cmd; private void button1_Click(object sender, EventArgs e) { con = new SqlConnection("Data Source=MCNDESKTOP03; Initial Catalog=nida; User ID=na; Password=khan@123"); con.Open(); cmd = new SqlCommand("select picname from motion picture where id=2", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Make full(ds); if (ds.Tables[0].Rows.Count > 0) { MemoryStream ms = new MemoryStream((byte[])ds.Tables[0].Rows[0]["picname"]); pictureBox1.Image = new Bitmap(ms); } } } } Stride 3
Now nosotros have to run our awarding then click on the Display button to see the image. The paradigm will display in the flick box which is taken by us in a form by giving their Id number in the query from the database.
If you want to choose any other image from your database then yous have to give their Id in SQL query from the column of Id of your database. The query is given beneath.
cmd = new SqlCommand("select picname from moving picture where id=2", con); If you want to cull on runtime which image should exist displayed then you can also take user input on runtime by taking the Id value from the user on runtime in ane TextBox and passing this TextBox value in this query so that the input id number volition be ready in id variable which will show the prototype in movie box whose Id it is.
So in this way, we can hands do the epitome Uploading and downloading job in C# Windows Application.
Source: https://codebun.com/uploading-and-downloading-image-in-c-windows-application/
0 Response to "How to Upload Picture Box in C#"
Post a Comment