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 :

Controls on the interface :
Button Control
- Browse... (Button1)
- Upload Image to Database (Button2)
- Exit (Button3)
- TextBox1 (Used to show the full path of the selected image)
- Image1 (To show the selected Image)
- Label1 (To display the messages/errors)
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.
|
Step 2: Display
the path of file in the TextBox1.
|
|
Step 3: Display the file name
on Label1
|
|
* 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

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

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 
|
|
Otherwise you'll receive a message at Label1 -
"Empty path name is not legal".

Chris StonePosted Feb 5, 2013, 1:52 PM
do you have any source code available for this? thanks