将位图图像从Access数据库导入C#程序 [英] Importing a bitmap image from an Access database into a C# program
问题描述
我在Visual Studio 2010中有一个C#程序,正在其中访问访问数据库中的数据.除了图像,我可以获得所有信息.我已按照步骤此处将图片嵌入到访问数据库中.
I have a C# program in visual studio 2010 where I am accessing the data from my access database. I can get all of the information, except for the image. I have followed the steps here to embed the pictures in the access database.
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类型."不知道为什么说图像存储为系统字节.
I used the location of my bitmap image though. I have a constructor that contains a bitmap attribute, but when it tries to go to the table to get all the information, I get the error: "Unable to cast object of System.Byte[] to type System.Drawing.Bitmap." Not sure why it is saying the image is stored as a system byte.
找到此线程.所以我尝试了内存流,但是同样的问题,无法将系统字节转换为system.io.memorystream.
Found this thread. So I tried Memory streams, but same problem, can't convert system byte to system.io.memorystream.
推荐答案
您在问题中描述的将位图图像插入Access数据库的过程将保存嵌入在OLE对象中的图像.如果要在C#程序中使用 just 位图图像,则需要从从Access检索的二进制数据中删除OLE包装器".
The procedure you describe in your question for inserting bitmap images into an Access database will save the image imbedded in an OLE Object. If you want to use just the bitmap image in your C# program you need to remove the OLE "wrapper" from the binary data that is retrieved from Access.
例如,如果我从Access数据库中检索它,并尝试将其直接转换为新的Bitmap
对象...
For example, if I retrieve it from the Access database and try to convert it directly to a new Bitmap
object...
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();
}
}
...我收到参数无效"错误:
...I get a "Parameter is not valid" error:
但是,如果我在其他答案的此处使用OleImageUnwrap
类的GetImageBytesFromOLEField
方法删除OLE包装" ...
However, if I remove the OLE "wrapper" using the GetImageBytesFromOLEField
method of the OleImageUnwrap
class in my other answer here...
var ms = new System.IO.MemoryStream(OleImageUnwrap.GetImageBytesFromOLEField(photoBytes));
...然后起作用:
这篇关于将位图图像从Access数据库导入C#程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!