Read image from Access DB in PictureBox

I am trying to read an image saved in Access DB as an OLE object in a PictureBox in a Windows C # 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, the line throws an exception "Parameter is invalid":

Bitmap bmp = new Bitmap(ms)

It is clear from the exception message that 'ms' is in a format that is not recognized. Any suggestion to get around this?

+3
source share
5 answers

Your side stream is corrupted in some way because I tried the exact method, but filled the byte array with PNG data from the file.

: , . . , .

-1

Google AccessHdr. AccessHdr.cpp AccessHdr.h. , .

+1

OLE. , OLE .

, BLOB . AccessImagine MS Access #. - http://access.bukrek.net

+1

Unfortunately, I do not have a good answer for you, but I can tell you that when I tried, I had the same results. Sometimes the first 78 bytes of the byte array were skipped, sometimes it is not.

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

0
source

You can try:

pbPassport.Image = Image.FromStream(ms);
0
source

Source: https://habr.com/ru/post/1698472/


All Articles