|
-
Dec 9th, 2012, 05:17 PM
#1
Thread Starter
New Member
[RESOLVED] Load Image Based on Database Filename
This may sound confusing at first, but is there a way to display an image in a picturebox from a file directory, based on the image's filename in a database?
For example, one of the fields in my database is labeled "image", and it has "happy.jpg" in it.
Is there a way to read "happy.jpg" from the database field, match it to the image called "happy.jpg" in my Debug folder, and display that image in my picturebox?
-
Dec 9th, 2012, 06:16 PM
#2
Re: Load Image Based on Database Filename
Hello, here is a simple example where our data table is loaded statically but that is no different than what is returned from a OleDbConnection and OleDbCommand. There are three rows, only the second row has a image that exists in bin\debug.
Code:
Private Function LoadDataSimilated() As DataTable
Dim dt As New DataTable("YourTable")
dt.Columns.Add("ID", GetType(Int32))
dt.Columns.Add("Image", GetType(String))
dt.Rows.Add(New Object() {1, "Cane_Lilies.gif"})
dt.Rows.Add(New Object() {2, "Candy_Cane_Lilies.gif"})
dt.Rows.Add(New Object() {3, "Candy_Cane_Lilies.gif"})
Return dt
End Function
Private Sub LoadImage()
Dim dt As DataTable = LoadDataSimilated()
Dim FileName As String = ""
For Each row As DataRow In dt.Rows
FileName = IO.Path.Combine(Application.StartupPath, row.Field(Of String)("Image"))
If IO.File.Exists(FileName) Then
Dim bm As New Bitmap(FileName)
PictureBox1.Image = bm
End If
Next
End Sub
-
Dec 9th, 2012, 06:49 PM
#3
Thread Starter
New Member
Re: Load Image Based on Database Filename
Perhaps I need to be more specific.
My database file is named "game.accdb", it contains a table named "tblDecisions", has 7 rows, and a column titled "image" set to type Text.
The image fields all contain an image's full filename, such as happy.jpg and sad.jpg.
When the user clicks a button on my app, the id value changes accordingly; and based on the id's value, I want the picturebox in my app to display a picture depending on what the id is equal to.
For example, when the user clicks the button, id increases by 1. The picturebox should now contain the image from my debug folder based on the filename pulled from the "image" field in row 1.
When the user clicks the button again, id increases to 2. The picturebox should now contain the image from my debug folder based on the filename pulled from the "image" field in row 2.
-
Dec 9th, 2012, 06:56 PM
#4
Thread Starter
New Member
Re: Load Image Based on Database Filename
Well, after messing around a bit I managed to do:
pictureBox.Image = Image.FromFile(Convert.ToString(dsStory.Tables("tblDecisions").Rows(id).Item("image")))
It works, so woohoo.
-
Dec 9th, 2012, 08:58 PM
#5
Re: Load Image Based on Database Filename
 Originally Posted by Webberjo
Well, after messing around a bit I managed to do:
pictureBox.Image = Image.FromFile(Convert.ToString(dsStory.Tables("tblDecisions").Rows(id).Item("image")))
It works, so woohoo.
Pretty much the direction I lead you. To be safe you need to add the path as there may be times where something changes the home path from the app folder to another folder. Also I would suggest using the following which takes a bit more keystroke yet is strongly cast.
Code:
PictureBox.Image = Image.FromFile(Convert.ToString(dsStory.Tables("tblDecisions").Rows(id).Field(Of String)("image")))
Instead of
Code:
pictureBox.Image = Image.FromFile(Convert.ToString(dsStory.Tables("tblDecisions").Rows(id).Item("image")))
Also the method I used allows you to clean up memory by setting the bitmap to nothing after setting the image.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|