Importing a bitmap image from an Access database into a C# program

狂风中的少年 提交于 2019-12-02 12:34:55

问题


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.

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.

Found this thread. So I tried Memory streams, but same problem, can't convert system byte to system.io.memorystream.


回答1:


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.

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:

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));

...then it works:




回答2:


Memory stream can be created from byte array and Image can be made from memory stream. The following code will compile:

byte[] bytes = new byte[0];  
MemoryStream ms = new MemoryStream(bytes);  
Image img = Image.FromStream(ms);


来源:https://stackoverflow.com/questions/19992148/importing-a-bitmap-image-from-an-access-database-into-a-c-sharp-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!