如何使用C#代码从Access数据库检索图片

问题描述:

我想使用Visual Studio 2008的C#代码从Access数据库中检索图片.我在位图图像格式的访问文件中拍摄了一张图片.

我使用了以下代码:

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:

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"

.

有两种可能:

1)您保存在数据库中的数据未以有效的图像格式保存.
2)您读出的数据是错误的.

由于数据读取代码应该可以工作,因此我将检查加载数据方法.
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.