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.
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; }
3 Answers
Answers 1
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!
Answers 2
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); }
Answers 3
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
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.
0 comments:
Post a Comment