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?
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.