In this article I will explain with an example, how to display Binary Image from SQL Server database in PictureBox control in Windows Forms (WinForms) Application using C# and VB.Net.
The Binary data will be fetched from SQL Server database and then will be converted into an Image object which ultimately will be displayed in PictureBox control in Windows Forms (WinForms) Application using C# and VB.Net.
Database
This article makes use of a table named tblFiles whose schema is defined as follows.
Note: You can download the database table SQL by clicking the download link below.
Form Design
The below Form consists of a ComboBox and a PictureBox control.
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
VB.Net
Imports System.IO
Imports System.Data
Imports System.Drawing
Imports System.Data.SqlClient
Populating ComboBox with Image records from Database
Inside the Form Load event handler, the ComboBox control is populated with Image records from the database table.
The Name column is assigned to the DisplayMember property while the ID column is assigned to the ValueMember property.
C#
private void Form1_Load(object sender, EventArgs e)
{
string constr = @"Data Source=.\SQL2014;Initial Catalog=dbFiles;Integrated Security=true";
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter("SELECT Id, Name FROM tblFiles", conn))
{
DataTable dt = new DataTable();
sda.Fill(dt);
DataRow row = dt.NewRow();
row[0] = 0;
row[1] = "Select Image";
dt.Rows.InsertAt(row, 0);
cmbImages.DataSource = dt;
cmbImages.DisplayMember = "Name";
cmbImages.ValueMember = "Id";
}
}
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim constr As String = "Data Source=.\SQL2014;Initial Catalog=dbFiles;Integrated Security=true"
Using conn As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter("SELECT Id, Name FROM tblFiles", conn)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Dim row As DataRow = dt.NewRow()
row(0) = 0
row(1) = "Select Image"
dt.Rows.InsertAt(row, 0)
cmbImages.DataSource = dt
cmbImages.DisplayMember = "Name"
cmbImages.ValueMember = "Id"
End Using
End Using
End Sub
Displaying Binary Images from database in PictureBox
The ComboBox has been assigned SelectionChangeCommitted event handler. Inside this event handler, the Binary data of the selected Image file is fetched using its ID and then the Byte Array is converted into MemoryStream object.
Then the MemoryStream object is converted into an Image object using the FromStream function of the Image class.
Finally the Image object is assigned to the PictureBox control.
C#
private void cmbImages_SelectionChangeCommitted(object sender, EventArgs e)
{
string constr = @"Data Source=.\SQL2014;Initial Catalog=dbFiles;Integrated Security=true";
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Data FROM tblFiles WHERE Id = @Id", conn))
{
cmd.Parameters.AddWithValue("@Id", cmbImages.SelectedValue);
conn.Open();
byte[] bytes = (byte[])cmd.ExecuteScalar();
conn.Close();
pictureBox1.Image = Image.FromStream(new MemoryStream(bytes));
}
}
}
VB.Net
Private Sub cmbImages_SelectionChangeCommitted(sender As System.Object, e As System.EventArgs) Handles cmbImages.SelectionChangeCommitted
Dim constr As String = "Data Source=.\SQL2014;Initial Catalog=dbFiles;Integrated Security=true"
Using conn As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT Data FROM tblFiles WHERE Id = @Id", conn)
cmd.Parameters.AddWithValue("@Id", cmbImages.SelectedValue)
conn.Open()
Dim bytes As Byte() = CType(cmd.ExecuteScalar(), Byte())
conn.Close()
pictureBox1.Image = Image.FromStream(New MemoryStream(bytes))
End Using
End Using
End Sub
Screenshot
Downloads