Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I'm trying to retrieve an image stored in an Access Database to a Picturebox. I have successfully managed to store the image into the database using OLE Object, I'm not very fluent with this language, but my current code is as follows:

What I have tried:

VB
Private Sub lstUsers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstUsers.SelectedIndexChanged

    Dim dt As New DataTable

    dt = runSQL("Select * from tblUser where UserName = '" & lstUsers.SelectedItem.ToString & "'")

    txtForename.Text = dt.Rows(0)(2)
    txtSurname.Text = dt.Rows(0)(3)
    txtCode.Text = dt.Rows(0)(6)
    txtFinish.Text = dt.Rows(0)(7)
    PictureBox1.Image = dt.Rows(0)(8)

End Sub



When I try to run this and I select a username I get the following error message:

VB
Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image'.


Any help would be appreciated.
Posted
Updated 8-Jan-17 23:00pm

You can not directly assign byte array to image object. First you have convert byte array to image object and then you can.
following is the solution: C# vresion
C#
byte[] data = (byte[]) dt.Rows(0)(8);
using (MemoryStream ms = new MemoryStream(data ))
{
pictureBox1.Image = Image.FromStream(ms);
}


Vb.Net version:
Dim data As Byte() = DirectCast(dt.Rows(0)(8), Byte())
Using ms As New MemoryStream(data)
	pictureBox1.Image = Image.FromStream(ms)
End Using

Regards,
Imdadhusen
 
Share this answer
 
v2
Comments
Sh.Zakria 9-Jan-17 5:00am    
When I paste this code on my program right below txtFinish.Text = dt.Rows(0)(7), the words byte and memoryStream are underlined in blue. Do I have to translate your code into a visual basic statement?
Sunasara Imdadhusen 9-Jan-17 5:04am    
i have added vb.net code style, you can use this one. if reference is not available in the project then you must have to add approvpriate reference from the project.
Sh.Zakria 9-Jan-17 5:08am    
PictureBox1.Image = Image.FromStream(ms)---Parameter is not valid.
Sunasara Imdadhusen 9-Jan-17 5:15am    
Check this link for more info http://net-informations.com/q/faq/imgtobyte.html
You cannot place the binary image data directly picturebox, you first need to create the image object from the data.

There's a nice article about this. See: Save and Retrieve Image from a SQL Server Database using VB.NET[^]

Don't worry if the database is SQL Server, the idea for the image is the same.
 
Share this answer
 
There are a number of problems here: first off never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
Second, never use SELECT * FROM - always specify exactly which columns you want to retrieve, and the order in which you want them. That way, you don't transfer data you don't need - which saves bandwidth - and your code becomes more immune to changes on the database causing it to crash because the column order is different, or worse corrupt data. This is particularly important if you use numeric indexes to retrieve column content instead of string based column names.
Thirdly, don't use a DataTable to retrieve a single user details - use a DataReader instead, it's a lot more memory efficient - and check the return to make sure there is data before you try to access it! If there is no user with that name, your code crashes and burns...
Fourthly, do not use the code in Solution 1: the documentation for Image.FromStream Method (Stream) (System.Drawing)[^] is very specific that the stream must remain open for the life of the image, which the using block does not allow.
VB
Dim ms As New MemoryStream(bytes)
Dim useThisImage As Image = Image.FromStream(ms)
 
Share this answer
 
Comments
Sh.Zakria 9-Jan-17 7:02am    
Thank you very much for your answer, now I understand it better. What will be the final code to display an image from the database if my image is stored in (0)(8) in a picturebox?
I have tried:
Dim data As Byte() = DirectCast(dt.Rows(0)(8), Byte())
Using ms As New MemoryStream(data)
PictureBox1.Image = Image.FromStream(ms)
End Using
But it gives me the error----Parameter is not valid.
OriginalGriff 9-Jan-17 7:17am    
Ah.
That means that you have concatenated strings to form an SQL command when you saved the images. That's bad - very bad. It's what I warned you about as the first thing above - and it means that all the images you have saved in your DB are total garbage.
See here:
https://www.codeproject.com/Tips/465950/Why-do-I-get-a-Parameter-is-not-valid-exception-wh
The code is in C# not VB, but it's easy to understand or use an online converter.
Sh.Zakria 9-Jan-17 7:21am    
Its fine as I only have one Username and a profile picture on my database because this is a testing project.
OriginalGriff 9-Jan-17 7:27am    
Doesn't matter - always assume it will "go live" and code appropriately.
That way, you get into the habit of doing it right rather than badly - and it's a lot harder to break a bad habit than from a good one!
Plus, you need to check the whole of the rest of your code - one concatenation and your best mate can delete your DB just by typing in a text box.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900