我想从 ms 访问数据库中检索图像,以便使用此代码在 PictureBox 中显示,我的代码如下所示:
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection myConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
myConnection.Open();
OleDbCommand cmd = new OleDbCommand("select * from [Images] where ID=1", myConnection);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
if (dt.Rows[0]["AImage"] != DBNull.Value)
{
pictureBox.Image = ByteArrayToImage((Byte[])dt.Rows[0]["AImage"]);
}
}
myConnection.Close();
}
Bitmap ByteArrayToImage(byte[] b)
{
MemoryStream ms = new MemoryStream();
byte[] pData = b;
ms.Write(pData, 0, Convert.ToInt32(pData.Length));
Bitmap bm = new Bitmap(ms, false);
ms.Dispose();
return bm;
}
但有一个错误,如:“参数无效。” 因为Bitmap bm = new Bitmap(ms, false);
我不知道为什么?