ArgumentException on reading an OLE image from an MS Access database in .NET

…衆ロ難τιáo~ 提交于 2019-12-06 09:47:46

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!