I can't seem to get an answer all together for my real issue Invalid parameter when retrieving image from DB So Imma try piece by piece. Working with Visual Studio 2012 in C# and MS Access 2010. My solution is an app non-web related.
I'm not sure about this part so here my question is on how to correctly get the image of an OLE Object that is in a row from a query into a byte array (byte[]), because certainly it isn't how I'm doing it with the following code. The row I'm talking about is row["FOTO"].
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [APP_Equipamento_Geral] WHERE COD_ETIQ like '%" + codigo + "%'", l);
DataSet ds = new DataSet();
adapter.Fill(ds, "[APP_Equipamento_Geral]");
string s = ds.Tables["[APP_Equipamento_Geral]"].Columns[16].ColumnName;
foreach (DataRow row in ds.Tables["[APP_Equipamento_Geral]"].Rows)
{
eq.NSerie = row["N_SERIE"].ToString();
eq.NInventario = row["Codigo"].ToString();
if (row["FOTO"] != DBNull.Value && row["FOTO"] != null)
{
string str = row["FOTO"].ToString();
byte[] b = stringToByteArray(str);
byte[] imagebyte = GetImageBytesFromOLEField(b); //Error caught here
MemoryStream ms = new MemoryStream();
ms.Write(imagebyte, 0, imagebyte.Length);
}
}
The method GetImageBytesFromOLEField can be found here. The error that it's giving me it's about the length of the index at the line string strVTemp = strTemp.Substring(0, 300);
Again, main question here is how to turn the OLE Object in the DataRow row["FOTO"] into byte[] to then use in that method.
The problem here is that the imbedded image was not a simple
BMP
orJPEG
. It was aMicrosoft Word Picture
and the OLE header information was considerably larger than the 300 byte window of the original
GetImageBytesFromOLEField()
code. (That is, after scanning 300 bytes it just gave up with "Unable to determine header size...".)The following is an updated version of that code in its own class. Cursory testing included the supplied
Microsoft Word Picture
, a simpleBMP
, and a simpleJPEG
.