问题
I am trying to read an image from an Access database. It reads the bytes but it gives an unhandled System.ArgumentException when I try to use Image.FromStream
.
Here is my code :
private Image ReadImageFromDB()
{
Image fetchedImg;
if (rownumber >= 0)
{
byte[] fetchedimgbytes = (byte[])localDataTable.Rows[5]["Object"];
MemoryStream stream = new MemoryStream(fetchedimgbytes);
fetchedImg= Image.FromStream(stream);
return fetchedImg;
}
else
{
MessageBox.Show("no image");
return null;
}
}
回答1:
I downloaded your sample file and when I opened the table in Access the item in the [Img] column said "Bitmap Image", not "Long Binary Data".
Therefore the [Img] item in that row is an "OLE wrapped" object, not a raw bitmap image. If you extract the binary contents of that column (as you did from your C# application) it will include the OLE wrapper around the raw binary image data and will not be a valid bitmap image in that form.
You need to remove the OLE header information from the binary data before it will be recognized as a valid image. For information on how to do that see this question:
Convert Access image OLE Object into raw image byte array in C#
来源:https://stackoverflow.com/questions/32508117/argumentexception-on-reading-an-ole-image-from-an-ms-access-database-in-net