Introduction
Hello friends, in this article, I will tell you how to store images in Windows Desktop to a Database as byte format, and also fetch data from database and display in an image picture box. So let's start!
Step 1
Open your Visual Studio and create a new project From File Menu > New > Project
Step 2
Select Windows Desktop App from the Menu
Step 3
Enter your Project Name and the path of your location where you want to save your project
Step 4
Now click on the Create button
Step 5
Create a design as you want. As you see in the below image, here I only added a Text Box, Picture Box, and two buttons for browsing the images and saving.
Step 6
Now add the database file. Right-click on your Project Name from the solution explorer, Add > New File, or you can simply use the shortcut key: Ctrl+Shift+A
Step 7
Select Service-Based Database, give it the name you want and click on Add
Step 8
Now create a new table, to create table double click on your database file it will open server explorer then right-click on the table, Add > New Table, and Add Fields to the table as you want, but make sure you use image datatypes for your image field. Below is an image of the table that I created for this project:
Step 9
Now copy your connection string by right-clicking on Database name > Modify Connection > Advance .
Step 10
Now add a new SQL connection on your form load event as shown in the below image.
Step 11
Create a dialog for selecting an image as shown below. Create an object of OpenFileDialog, set filters if you want and give your selected image a picture box
Step 12
Now create a function for creating an image file, in row format, as shown in the below image.
Step 13
Now add code on your save button. Here, I only store name and image in a database table, as you can see in the below image
Step 14
Run your application by entering data in all fields and selecting the image by clicking on browse button and save data by clicking the save button.
In the below image, you can see data of the image store as Row Data in the table
Now we get data from the Database table by ID.
Step 15
Create a design as you want to fetch data from the table by record ID.
Step 16
Create a function for the display image in a picture box from row data, as shown in the below image.
Step 17
On search the button or textbox. Leave the image and add the following code, as shown in below image.
I hope you find this article helpful. If you learn anything from this article kindly share it with your friends. Thank you.
Below is the full source code of page:
- 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;
- using System.Data.SqlClient;
- using System.IO;
-
- namespace SaveAndDisplayImageFromDatabase
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- SqlConnection cn;
- SqlCommand cmd;
-
- private void Form1_Load(object sender, EventArgs e)
- {
- cn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=G:\Tutorial\SaveAndDisplayImageFromDatabase\SaveAndDisplayImageFromDatabase\Database1.mdf;Integrated Security=True");
- cn.Open();
- }
-
- private void button2_Click(object sender, EventArgs e)
- {
- OpenFileDialog d = new OpenFileDialog();
- d.Title = "Select Image";
- d.Filter = " (*.jpg;*.png;*.jpeg) | *.jpg;*.png;*.jpeg";
- DialogResult dr = new DialogResult();
- dr = d.ShowDialog();
- if (dr == DialogResult.OK)
- {
- pictureBox1.Image = new Bitmap(d.FileName);
- }
- }
-
- public byte[] savePhoto(PictureBox pb)
- {
- MemoryStream ms = new MemoryStream();
- pictureBox1.Image.Save(ms, pb.Image.RawFormat);
- return ms.GetBuffer();
- }
-
-
- public Image displayImage(byte[] photo)
- {
- MemoryStream ms = new MemoryStream(photo);
- return Image.FromStream(ms);
- }
-
-
- private void button1_Click(object sender, EventArgs e)
- {
- cmd = new SqlCommand("insert into Table1 values(@name,@image)",cn);
- cmd.Parameters.AddWithValue("name", txtname.Text);
- cmd.Parameters.AddWithValue("image", savePhoto(pictureBox1));
- cmd.ExecuteNonQuery();
- MessageBox.Show("Data Save in Database ", "Data Save", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
-
- private void button3_Click(object sender, EventArgs e)
- {
- cmd = new SqlCommand("select * from Table1 where id="+textBox1.Text+"",cn);
- SqlDataReader dr = cmd.ExecuteReader();
- while(dr.Read())
- {
- txtname.Text = dr["name"].ToString();
- pictureBox1.Image = displayImage((byte[])dr["image"]);
-
- }
- dr.Close();
- }
- }
- }