从.NET中的MS Access数据库读取OLE图像时出现ArgumentException [英] ArgumentException on reading an OLE image from an MS Access database in .NET
问题描述
我正在尝试从Access数据库读取图像.当我尝试使用Image.FromStream
时,它读取字节,但给出未处理的System.ArgumentException.
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
.
这是我的代码:
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;
}
}
推荐答案
我下载了示例文件,当我在Access中打开表时,[Img]列中的项目表示位图图像",而不是长二进制数据" .
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".
因此,该行中的[Img]项目是"OLE包装"对象,而不是原始位图图像.如果提取该列的二进制内容(如在C#应用程序中所做的那样),它将在原始二进制图像数据周围包含OLE包装,并且不是该格式中的有效位图图像.
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.
您需要从二进制数据中删除OLE标头信息,然后才能将其识别为有效映像.有关如何执行此操作的信息,请参见以下问题:
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:
这篇关于从.NET中的MS Access数据库读取OLE图像时出现ArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!