ARTICLE

Save an Image to Database in WPF using Visual Basic

Posted by Deepak Kumar Verma Articles | WPF using VB.NET May 23, 2011
This article demonstrate how to store an image in database in WPF using Visual Basic.
Download Files:
 
Reader Level:

Often, In applications, we require to save images to database (for eg. photograph of employee, students etc.). Here we learn how it comes in effect using WPF and VB.net.

To store an image to database, I created a table named "ImgContainer" which contains three columns -

Column Name Data Type Description
Id Int Defines as Identity and handeled by itself to generate and store the unique Id for each image stored in database
Name Varchar This field holds the name of each image
ImgData Image Image files are stored as binary in this column

Now, come to the front-end : 

SaveImg2DB1.gif

Controls on the interface :

Button Control

  1. Browse... (Button1)
  2. Upload Image to Database (Button2)
  3. Exit (Button3)
Text Box Control
  1. TextBox1 (Used to show the full path of the selected image)
Image Control
  1. Image1 (To show the selected Image)
Label Control
  1. Label1 (To display the messages/errors)
How it works?

Note :
Download source code to learn in better way, code snippets are just used to clear the steps and may not work separately.


Step 1: Select a File from storage disk.

Dim FileDialog As New 
OpenFileDialog
FileDialog.Title = "Select A File"
FileDialog.InitialDirectory = ""            FileDialog.Filter = "Image Files (*.gif,*.jpg,*.jpeg,*.bmp,*.png)|*.gif;*.jpg;*.jpeg;*.bmp;*.png"
FileDialog.FilterIndex = 1
FileDialog.ShowDialog()

Step 2: Display the path of file in the TextBox1.

Label1.Content = FileName(TextBox1.Text.Trim) 

Step 3: Display the file name on Label1

 

TextBox1.Text = FileDialog.FileName()


* File Dialog is a function which takes full path of the file and returns the file name.

Step 4: Display the Image in the Image Control - Image1

Dim bmp As New BitmapImage(New Uri(TextBox1.Text.Trim))
Image1.Source = bmp

SaveImg2DB2.gif

* If you don't select any image file and click on cancel button of OpenFileDialog, you'll receive a message in Label1

 Label1.Content = "You didn't select any image file...."

SaveImg2DB5.gif

Step 5: Save image to database - When you select an image click on "Upload Image to Database" Button.  If you have selected an image, you'll receive the following message as shown in below image

SaveImg2DB3.gif

Dim Stream As FileStream
Dim Reader As StreamReader
Stream = New FileStream(TextBox1.Text, FileMode.Open, FileAccess.Read)
Reader = New StreamReader(Stream)
Dim ImgData(Stream.Length - 1) As Byte
Stream.Read(ImgData, 0, Stream.Length - 1)
Dim str As String = "Data Source=.;uid=sa;pwd=wintellect;database=MYDATABASE"
Dim con As New SqlConnection(str)
Dim sql As String = "INSERT INTO ImgContainer (Name, ImgData) VALUES(@Name, @ImgData)"
Dim cmd As New SqlCommand(sql, con)
cmd.Parameters.Add("@Name", SqlDbType.Text).Value = FileName(TextBox1.Text.Trim)
cmd.Parameters.Add("@ImgData", SqlDbType.Binary, Stream.Length).Value = ImgData
con.Open()
cmd.ExecuteNonQuery()
Label1.Content = FileName(TextBox1.Text.Trim) & " Stored Successfully...."
            con.Close()

Otherwise you'll receive a message at Label1 - "Empty path name is not legal".

 SaveImg2DB6.gif

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Team Foundation Server Hosting
Become a Sponsor