Read a picture from Access DB into PictureBox

限于喜欢 提交于 2019-12-20 02:32:47

问题


I have been trying to read a picture saved in Access DB as a OLE object in a PictureBox in a C# windows Application.

The code that does this is presented below:

        string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Rajesh\SampleDB_2003.mdb;";
        OleDbConnection oConn = new OleDbConnection(connString);
        oConn.Open();
        string commandString = "select * from employee where id = " + id + "";
        OleDbCommand oCmd = new OleDbCommand(commandString, oConn);
        OleDbDataReader oReader = oCmd.ExecuteReader(CommandBehavior.SequentialAccess);

        while (oReader.Read())
        {
            txtID.Text = ((int)oReader.GetValue(0)).ToString();
            txtName.Text = (string)oReader.GetValue(1);
            txtAge.Text = ((int)oReader.GetValue(2)).ToString();
            txtType.Text = (string)oReader.GetValue(3);
            byte[] imageBytes = (byte[])oReader.GetValue(4);

            MemoryStream ms = new MemoryStream();
            ms.Write(imageBytes, 0, imageBytes.Length);
            Bitmap bmp = new Bitmap(ms);
            pbPassport.Image = bmp;
        }

When I execute the above code, an 'Parameter is not valid' exception is thrown at the line:

Bitmap bmp = new Bitmap(ms)

From the exception message, it is clear that 'ms' is in a format that is not recognisable. Any suggestion to get past this?


回答1:


Your bytestream is corrupted somehow, becouse I tried the exact method of yours but filled the byte array with PNG data from a file instead.

I would suggest creating two streams, one from the database, and one from the file that was the source of the image in the database. Then compare them byte by byte. If there is even one byte of diffrence, the database image data is corrupt.




回答2:


Unfortunately I have no good answer for you, but I can tell you that when I tried, I got the same results. Sometimes skipping the first 78 bytes of the byte array worked, sometimes it didn't.

This is because the OLE Object datatype stores some kind of header in the field, so that Access knows what type of OLE Object it is. I could not find a reliable way to work out exactly where this header stopped and real data started, but I also gave up, so good luck :)




回答3:


Do a google search for AccessHdr. You'll find references to AccessHdr.cpp and AccessHdr.h. These will illustrate what is need to extract the streams without the header.




回答4:


You can't read OLE objects so easily. In fact, it is bad practice to keep pictures as OLE objects in database.

It is preferred to have em as BLOB objects or path and filename at some storage. AccessImagine can handle both scenarios for MS Access and C#. You can download it here - http://access.bukrek.net




回答5:


You can try:

pbPassport.Image = Image.FromStream(ms);


来源:https://stackoverflow.com/questions/217928/read-a-picture-from-access-db-into-picturebox

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