|
-
Feb 10th, 2008, 01:58 AM
#1
Thread Starter
Member
[2008] Fill picturebox with image from Access db
Hi all
I'm having some trouble trying to read an image from an Access Database.
The image is stored as an OLE Object.
This works fine:
txtItemName.Text = itemsReader("Name").ToString
But I get the error "Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image'." with this line:
pbItemImage.Image = itemsReader("Image")
Code:
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & Application.StartupPath & "\t4c.mdb")
Dim query As String
query = "SELECT Name, Enc, Req_Str, Req_End, Req_Agi, Req_Int, Req_Wis, Req_Att, Bonus_AC, Bonus_Dodge_Fix, Bonus_Str, Bonus_End, Bonus_Agi, Bonus_Wis, Bonus_Int, Bonus_Air_Power, Bonus_Dark_Power, Bonus_Earth_Power, Bonus_Fire_Power, Bonus_Light_Power, Bonus_Water_Power, Bonus_Air_Resist, Bonus_Dark_Resist, Bonus_Earth_Resist, Bonus_Fire_Resist, Bonus_Water_Resist, Bonus_AP, Bonus_Arc, Bonus_Att, Bonus_Hide, Bonus_Parry, Bonus_PB, Bonus_RH, Bonus_SB, Bonus_Meditate, Bonus_Dmg FROM Gloves WHERE Name= '" & lstGloves.SelectedValue & "'"
Dim command As New OleDbCommand(query, conn)
conn.Open()
Dim itemsReader As OleDbDataReader = command.ExecuteReader
itemsReader.Read()
txtItemName.Text = itemsReader("Name").ToString
pbItemImage.Image = itemsReader("Image")
conn.Close()
I've searched google and this forum for a little under an hour and can't find anything that works..
Any help?
-
Feb 10th, 2008, 02:08 AM
#2
Re: [2008] Fill picturebox with image from Access db
Follow the appropriate link from my signature.
-
Feb 11th, 2008, 07:14 AM
#3
Thread Starter
Member
Re: [2008] Fill picturebox with image from Access db
Thanks, but using your code adapted to my needs results in an error.
The error is "Parameter is not valid."
On line: picture = Image.FromStream(stream)
Also, how would I change it to be able to use this as the query and not need those extra lines?
Code:
"SELECT Name, Enc, Req_Str, Req_End, Req_Agi, Req_Int, Req_Wis, Req_Att, Bonus_AC, Bonus_Dodge_Fix, Bonus_Str, Bonus_End, Bonus_Agi, Bonus_Wis, Bonus_Int, Bonus_Air_Power, Bonus_Dark_Power, Bonus_Earth_Power, Bonus_Fire_Power, Bonus_Light_Power, Bonus_Water_Power, Bonus_Air_Resist, Bonus_Dark_Resist, Bonus_Earth_Resist, Bonus_Fire_Resist, Bonus_Water_Resist, Bonus_AP, Bonus_Arc, Bonus_Att, Bonus_Hide, Bonus_Parry, Bonus_PB, Bonus_RH, Bonus_SB, Bonus_Meditate, Bonus_Dmg, Image FROM Helms WHERE Name= '" & lstHelms.SelectedValue & "'"
Code:
Dim connpic As New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & Application.StartupPath & "\t4c.mdb")
Dim commandpic As New OleDbCommand("SELECT Image FROM Helms WHERE Name= '" & lstHelms.SelectedValue & "'", connpic)
connpic.Open()
Dim pictureData As Byte() = DirectCast(commandpic.ExecuteScalar(), Byte())
Dim picture As Image = Nothing
Using stream As New IO.MemoryStream(pictureData)
picture = Image.FromStream(stream)
End Using
pbItemImage.Image = picture
connpic.Close()
EDIT: Using
stream.Write(pictureData, 78, pictureData.Length - 78)
Fixed it and is now running well, thanks!
Code:
Using stream As New IO.MemoryStream(pictureData)
stream.Write(pictureData, 78, pictureData.Length - 78)
picture = Image.FromStream(stream)
End Using
Last edited by rhijaen; Feb 11th, 2008 at 07:31 AM.
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
|