2

I am trying to fetch an image from MS Access DB. Data is fetched correctly but when I'm trying to display some error is showing. My code for display the image is,

            ...
            byte[] photoBytes = (byte[])res[11];
            var ms = new System.IO.MemoryStream(photoBytes);
            image.Image = new System.Drawing.Bitmap(ms);
            ...

Error : Additional information: Parameter is not valid. enter image description here

Can anyone tell me where is the error, or probability of error?

my function is

public OleDbDataReader studentInfo(String adm_no)
    {
        OleDbConnection con = new OleDbConnection(ConnStr);
        con.Open();
        OleDbCommand command = new OleDbCommand("SELECT * FROM student_info WHERE adm_no = '"+adm_no+"'", con);
        OleDbDataReader res = command.ExecuteReader();
        return res;
    }
Prashant
  • 394
  • 1
  • 6
  • 18
  • 2
    please don't post screenshots of code, especially when they are **low resolution** and _unreadable_ –  Jan 31 '17 at 15:30
  • 2
    Is the field an Ole object or an attachment? Could you post a sample database somewhere? Also, have you seen that: http://stackoverflow.com/questions/25864092/extracting-files-from-an-attachment-field-in-an-access-database – Simon Mourier Feb 05 '17 at 19:11

3 Answers3

5

As far as I can remember OLE's are beasts. If you know for sure what the datatype is you have some chances if you check the binary structure of it. OLE is a container so it's IMO never just the pure content.

I don't have the code anymore but I remember fishing in the hexdumps of the OLE's of different types (Excel, Word, Textfiles, Images, ...) and ending up with a success rate of maybe 80%. If that was because of the types we decided to support or my very restricted knowledge on the inner structure of OLE's I can't tell anymore.

My recommendation for debugging would be to make absolutely sure you have the raw bitmap data before dealing with the binary data at all:

My approach was to create a small object (in this case a bitmap), store it in the DB, get the BLOB of it and lookup the known pattern in there. I remember I found some structures - like the byte size of the searches object - by reverse- engineering and a somewhat stable offset towards the start of the data.

If you, however, happen to know - or even better - have the exact structure and implementation of an OLE- object and are able to deal with it, I am absolutely confident you'll manage to also store it and open it as a bitmap.

Good luck!

RuDevel
  • 694
  • 3
  • 14
0

Take a look here for an example of what you are trying to do, although that example is for a JPEG not a Bitmap. Since you have a byte[], you will need to do something like this for conversion:

using (MemoryStream ms = new MemoryStream(photoBytes))
{
  Bitmap img = (Bitmap)Image.FromStream(ms);
}
Community
  • 1
  • 1
awh112
  • 1,466
  • 4
  • 22
  • 34
  • you mean like that – Prashant Jan 31 '17 at 15:44
  • byte[] photoBytes = (byte[])res[11]; using (MemoryStream ms = new MemoryStream(photoBytes)) { Bitmap img = (Bitmap)Image.FromStream(ms); image.Image = new System.Drawing.Bitmap(ms); } – Prashant Jan 31 '17 at 15:44
  • Give that a try and see how it goes. If not, you might get away with just doing `image.Image = img` since you already have a Bitmap object. – awh112 Jan 31 '17 at 15:57
  • i think the problem with "MemoryStream(photoBytes)" code. as i see in compile.,, bcoz image.Image=img also showing error. the error shows till when i comment MemoryStream(photoBytes) sentance – Prashant Jan 31 '17 at 16:06
  • That sounds like an issue with the `byte[]` variable. For whatever reason there is an issue getting a `MemoryStream` object from `photoBytes`. – awh112 Jan 31 '17 at 16:09
  • can you give me a batter reference for ms access OLE database – Prashant Jan 31 '17 at 16:14
0

There's something wrong with your byte stream. Normally I'd say to check whether the MemoryStream's .Position is set to 0 before passing it into the Bitmap constructor - if it's at the end of the stream, it's possible that you're effectively passing an empty stream - but that shouldn't be the case here.

There are a few places in the constructor where an argument exception would get thrown, but InvaildParameter should be related to something being wrong with the byte stream that you're retrieving. See here: https://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Bitmap.cs,cbbb65af7f6fafdb,references

And here: https://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Advanced/Gdiplus.cs,4edcade52d698713

You should validate that your byte stream is a suitable format for GDI+ to be able to load as an image - try writing the bytes out to a file for example and opening it in Paint.

Dan Field
  • 20,885
  • 5
  • 55
  • 71