How to Save Images in Row Format/Bytes Into a Database and Fetch Them in a C# Windows Desktop Applications

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
 
save image in row format / bytes into database and fetch from database in c# windows desktop application
 
Step 2
 
Select Windows Desktop App from the Menu
 
save image in row format / bytes into database and fetch from database in c# windows desktop application
 
Step 3
 
Enter your Project Name and the path of your location where you want to save your project
 
save image in row format / bytes into database and fetch from database in c# windows desktop application
 
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.
 
save image in row format / bytes into database and fetch from database in c# windows desktop application
 
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
 
save image in row format / bytes into database and fetch from database in c# windows desktop application
 
Step 7
 
Select Service-Based Database, give it the name you want and click on Add
 
save image in row format / bytes into database and fetch from database in c# windows desktop application
 
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:
 
save image in row format / bytes into database and fetch from database in c# windows desktop application
 
Step 9
 
Now copy your connection string by right-clicking on Database name > Modify Connection > Advance .
 
save image in row format / bytes into database and fetch from database in c# windows desktop application
 
Step 10
 
Now add a new SQL connection on your form load event as shown in the below image.
 
save image in row format / bytes into database and fetch from database in c# windows desktop application
 
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
 
save image in row format / bytes into database and fetch from database in c# windows desktop application
 
Step 12
 
Now create a function for creating an image file, in row format, as shown in the below image.
 
save image in row format / bytes into database and fetch from database in c# windows desktop application
 
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
 
save image in row format / bytes into database and fetch from database in c# windows desktop application
 
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
 
save image in row format / bytes into database and fetch from database in c# windows desktop application
 
save image in row format / bytes into database and fetch from database in c# windows desktop application
 
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.
 
save image in row format / bytes into database and fetch from database in c# windows desktop application
 
Step 16
 
Create a function for the display image in a picture box from row data, as shown in the below image.
 
save image in row format / bytes into database and fetch from database in c# windows desktop application
 
Step 17
 
On search the button or textbox. Leave the image and add the following code, as shown in below image.
 
save image in row format / bytes into database and fetch from database in c# windows desktop application
 
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:
  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. using System.Data.SqlClient;  
  11. using System.IO;  
  12.   
  13. namespace SaveAndDisplayImageFromDatabase  
  14. {  
  15.     public partial class Form1 : Form  
  16.     {  
  17.         public Form1()  
  18.         {  
  19.             InitializeComponent();  
  20.         }  
  21.         SqlConnection cn;  
  22.         SqlCommand cmd;  
  23.   
  24.         private void Form1_Load(object sender, EventArgs e)  
  25.         {  
  26.             cn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=G:\Tutorial\SaveAndDisplayImageFromDatabase\SaveAndDisplayImageFromDatabase\Database1.mdf;Integrated Security=True");  
  27.             cn.Open();  
  28.         }  
  29.   
  30.         private void button2_Click(object sender, EventArgs e)  
  31.         {  
  32.             OpenFileDialog d = new OpenFileDialog();  
  33.             d.Title = "Select Image";  
  34.             d.Filter = " (*.jpg;*.png;*.jpeg) | *.jpg;*.png;*.jpeg";  
  35.             DialogResult dr = new DialogResult();  
  36.             dr = d.ShowDialog();  
  37.             if (dr == DialogResult.OK)  
  38.             {  
  39.                 pictureBox1.Image = new Bitmap(d.FileName);  
  40.             }  
  41.         }  
  42.         //save image    
  43.         public byte[] savePhoto(PictureBox pb)  
  44.         {  
  45.             MemoryStream ms = new MemoryStream();  
  46.             pictureBox1.Image.Save(ms, pb.Image.RawFormat);  
  47.             return ms.GetBuffer();  
  48.         }  
  49.   
  50.         //display Image  
  51.         public Image displayImage(byte[] photo)  
  52.         {  
  53.             MemoryStream ms = new MemoryStream(photo);  
  54.             return Image.FromStream(ms);  
  55.         }  
  56.   
  57.   
  58.         private void button1_Click(object sender, EventArgs e)  
  59.         {  
  60.             cmd = new SqlCommand("insert into Table1 values(@name,@image)",cn);  
  61.             cmd.Parameters.AddWithValue("name", txtname.Text);  
  62.             cmd.Parameters.AddWithValue("image", savePhoto(pictureBox1));  
  63.             cmd.ExecuteNonQuery();  
  64.             MessageBox.Show("Data Save in Database ""Data Save", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  65.         }  
  66.   
  67.         private void button3_Click(object sender, EventArgs e)  
  68.         {  
  69.             cmd = new SqlCommand("select * from Table1 where id="+textBox1.Text+"",cn);  
  70.             SqlDataReader dr = cmd.ExecuteReader();  
  71.             while(dr.Read())  
  72.             {  
  73.                 txtname.Text = dr["name"].ToString();  
  74.                 pictureBox1.Image = displayImage((byte[])dr["image"]);  
  75.   
  76.             }  
  77.             dr.Close();  
  78.         }  
  79.     }  
  80. }  


Similar Articles