Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to retrieve a picture from an Access database using C# code of visual studio 2008. I took a picture in access file of Bitmap Image format.

I used the following code:
C#
private void load_picturebox_Click(object sender, EventArgs e)
        {
            byte[] ImageByte = null;
            MemoryStream MemStream = null;
            PictureBox PicBx = new PictureBox();
            object OB;
 
            vcon.Open();
 
            int ImageID = 1;
            string sql = "SELECT picture FROM dictionary WHERE serial_no = " + ImageID + "";
            OleDbCommand vcom = new OleDbCommand(sql, vcon);
 
            ImageByte = (byte[])vcom.ExecuteScalar();
 
            MemStream = new MemoryStream(ImageByte);
            PicBx.Image = Image.FromStream(MemStream);
        }

But I couldnt run this code with the below line in the code:
PicBx.Image = Image.FromStream(MemStream);


The error is:
"Parameter is not valid"
.
Posted
Updated 24-Mar-12 14:14pm
v2
Comments
[no name] 24-Mar-12 11:55am    
Well? Is ImageByte a valid byte array?
Member 8454009 24-Mar-12 12:49pm    
Yes, ImageByte is a valid byte array. It's have no error.
Ilham Israfilov 24-Mar-12 17:23pm    
Doubtful. Because with the command you executed you get not image, but table with 1 field and that's not valid byte array. You could get arraylike access to that field and grab the image.
[no name] 24-Mar-12 22:56pm    
Then the image in the database is not a valid image format.

1 solution

There are two possibilities:

1) The data you saved in the database was not saved in a valid image format.
2) The data you read out is wrong.

Since the data read out code should work, I would check the load data method.
 
Share this answer
 

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