In the end I want to grab a OLE type image from an Access Database and put it into a picture box. Working with Visual Studio 2012 in C# and MS Access 2010. My solution is an app non-web related.
So this is the query code. I'm constructing an object (Equipamento
) with among others an System.Drawing.Image
attribute that is the focus of the issue.
OleDbConnection l = OleDbConnectionDAO.createConnection();
Equipamento eq = new Equipamento();
try
{
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.NInventario = row["Codigo"].ToString();
eq.Modelo = row["MODELO"].ToString();
eq.Marca = row["Marca_"].ToString();
eq.GamaMedida = row["Gama Medida"].ToString();
if (row["FOTO"] != DBNull.Value && row["FOTO"] != null)
{
byte[] b = new byte[0];
b = (byte[])row["FOTO"];
eq.Img = getImageFromBytes(b);//Error caught here
}
//if (row["FOTO"] != DBNull.Value && row["FOTO"] != null)
//{
// byte[] b = (byte[])row["FOTO"];
// MemoryStream ms = new MemoryStream(b);
// eq.Img = Image.FromStream(ms); //Error caught here
//}
}
}
And here is the auxiliary method:
private Image getImageFromBytes(byte[] myByteArray)
{
System.IO.MemoryStream newImageStream
= new System.IO.MemoryStream(myByteArray, 0, myByteArray.Length);\
return Image.FromStream(newImageStream, true);
}
That last commented piece of code was another of my attempts that also gave the
Invalid parameter
error. Any solutions?
Note: If I take out the image part everything works fine.
You can't convert a image or string to arraybite only using the cast.
If you want convert an image to arrayBite use this function
to reconvert arraybite to image use this
An image stored as an
OLE object
has a different format from a serializedSystem.Drawing.Image
. That's why I asked how the images were stored.While I cannot vouch for this, never having used it personally, the following code is much recommended. Supposedly, it uses the GDI+ lib from MS (included in Win standard installation) to import/export pics to/from Access OLE.
http://www.access-im-unternehmen.de/index1.php?BeitragID=337&id=300
You can find other suggestions (including a utility to extract your images from Access) at this link:
Converting MS Access "OLE Objects" back to plain JPEGs - best way?