2

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;
        }
    }
Renan Araújo
  • 3,533
  • 11
  • 39
  • 49
mohamed
  • 135
  • 1
  • 14
  • 1
    How about you share what the exception is with us. It will definitely help us debug. Right now it could be anything, `Rows[5]` might not exist. `fetchedimgbytes` might be null, `stream` may not represent an image. You're not really giving us much to go on. – dmeglio Sep 10 '15 at 17:28
  • In which line exactly you get unhandled exception? What do you use to connect to DB? How you populate result set? – jjczopek Sep 10 '15 at 17:29
  • the type of exception is 'System.ArgumentException' occurred in System.Drawing.dll – mohamed Sep 10 '15 at 17:31
  • if it is a bitmap image how can i read it from the database – mohamed Sep 10 '15 at 17:33
  • MSDN says says that ArgumentException for the FromStream method is thrown when "The stream does not have a valid image format" or the "stream" is null. I assume it's the first option. How are you saving image into database? – jjczopek Sep 10 '15 at 17:40
  • i got the file from someone and am asked to extract the images out of it and am sure that it is an image and i added a row from my own and placed a .jpg image and the same problem is still there – mohamed Sep 10 '15 at 17:44
  • Open the database in Access, open the table in Datasheet View (by double-clicking the table name) and look at the column that contains the images. Do the entries in that column of the datasheet say "Bitmap Image"? If not, what do they say? – Gord Thompson Sep 10 '15 at 18:35
  • @GordThompson it says long binary data – mohamed Sep 10 '15 at 18:41
  • I am unable to recreate your issue. Your code works fine for me to extract a raw binary BMP or JPG image from an Access database into a .NET `Image` object. If you can provide a link to a sample database that illustrates the problem then perhaps we can help you, but there is nothing wrong with your code *per se*. – Gord Thompson Sep 10 '15 at 22:18
  • @GordThompson here is a link to the database am using https://www.mediafire.com/?3pkzgeby2c3rwab – mohamed Sep 10 '15 at 22:24

1 Answers1

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".

BitmapImage.png

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#

Community
  • 1
  • 1
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • could you please follow this link :http://stackoverflow.com/questions/32528427/parameter-not-valid-when-using-image-fromstream-in-c-sharp – mohamed Sep 11 '15 at 17:14