我在Visual Studio 2010中有一个C#程序,正在其中访问访问数据库中的数据。除了图像,我可以获得所有信息。我已按照此处的步骤将图片嵌入到Access数据库中。
Right-click the first field in the Image column of the table and click Insert Object.
Click Create from File, and then click Browse.
Browse to one or more Windows Bitmap (.bmp) or Device Independent Bitmap (.dib) images.
You can find a set of BMP files, named Empid1.bmp through Empid9.bmp, at
drive:\Program Files\Microsoft Office\OFFICE11\SAMPLES. Select the first image and click OK.
我虽然使用了位图图像的位置。我有一个包含位图属性的构造函数,但是当它尝试转到表以获取所有信息时,出现错误:“无法将System.Byte []的对象强制转换为System.Drawing.Bitmap类型。” 不知道为什么要说图像存储为系统字节。
找到了这个线程。所以我尝试了内存流,但是同样的问题,无法将系统字节转换为system.io.memorystream。
您在问题中描述的将位图图像插入Access数据库的过程将保存嵌入在OLE对象中的图像。如果你想使用只是在C#程序中的位图图像,你需要从从访问检索到的二进制数据删除OLE“包装”。
例如,如果我从Access数据库中检索它,并尝试将其直接转换为新Bitmap
对象...
private void Form1_Load(object sender, EventArgs e)
{
using (var con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Public\Database1.accdb;"))
{
con.Open();
using (var cmd = new OleDbCommand("SELECT LastName, FirstName, Photo FROM Clients WHERE ID=3", con))
{
OleDbDataReader rdr = cmd.ExecuteReader();
rdr.Read();
this.textBox1.Text = rdr["FirstName"].ToString();
this.textBox2.Text = rdr["LastName"].ToString();
byte[] photoBytes = (byte[])rdr["Photo"];
var ms = new System.IO.MemoryStream(photoBytes);
this.pictureBox1.Image = new System.Drawing.Bitmap(ms);
ms.Close();
}
con.Close();
}
}
...我收到“参数无效”错误:
但是,如果我在这里的其他答案中使用该类的GetImageBytesFromOLEField
方法删除了OLE“包装” ...OleImageUnwrap
var ms = new System.IO.MemoryStream(OleImageUnwrap.GetImageBytesFromOLEField(photoBytes));
...然后工作:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句