We have a legacy system in MSAccess which has a "OLE Object" Column to store images. Now, we are trying to create a small executable to convert the "OLE object" back to image file using C#. We came to know the users takes screenshot and paste dierctly to the MSAccess column.
But while retrieving data from access it returns the bytes but throws 'Parameter is not valid.' exception while doing Image.FromStream(ms).
Here is my sample code.
//Connect to MSAccess database
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
try
{
DataTable dt = new DataTable();
using (OleDbCommand cmd = new OleDbCommand(commandText, conn))
{
using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
{
da.Fill(dt);
if (dt != null && dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
Byte[] imageBytes = dr[0] as Byte[];
MemoryStream ms = new MemoryStream(imageBytes);
Image img = Image.FromStream(ms);
img.Save(destinationPath + "Image1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
}
}
finally
{
conn.Close();
}
}
Any help is highly appreciated.