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.