If you are reading data directly from MS Access, you do not need to strip header information.
Assuming the image is stored as a BLOB , which is the most common, here is the code to read in an array of bytes from the database and store as an image file (sorry, VB instead of C #):
Dim varBytes() As Byte Using cn As New OleDbConnection(myConnectionString) cn.Open() sqlText = "SELECT [myColumn] " _ & "FROM [myTable] " _ & "WHERE ([mySearchCriteria] = '" & mySearchTerm & "')" Using cm As New OleDbCommand(sqlText, cn) Dim rdr As OleDbDataReader rdr = cm.ExecuteReader rdr.Read() varBytes = rdr.GetValue(0) End Using End Using My.Computer.FileSystem.WriteAllBytes(myPath & "\myFile.emf", varBytes, True)
In this example I laid out, I know that the files in the database were .emf images. If you know the extension, you can put it in the file name. If you do not, you can leave it blank and then open the result using the image viewer; he must begin. If you need to find the extension or file type as soon as it is saved as a file, you can open it using any hex editor and the file type will be available from the header information.
Your question is a bit unclear, so I'm not sure if the above code is exactly what you want, but it should get a lot closer.
EDIT:
This is VB code that takes an array of bytes, loads it into a MemoryStream object, and then creates an Image object from Stream. This bit of code worked very well and displayed the image in the image box in my form.
Dim img As Image Dim str As New MemoryStream(varBytes) img = Image.FromStream(str) PictureBox1.Image = img
If the C # equivalent of this does not work for you, the problem is how the image is stored in the MS Access database.
EDIT:
If the image in your database is stored as a “Package” and not “Long binary data”, you will need to delete the header information that MS Access adds. I play with the "Image" image storage type with a simple .jpg file. The header in this case is much longer than 78 bytes. In this case, it is actually 234 bytes, and MS Access also added some information to the end of the source file; about 292 bytes in this case.
It looks like your original approach was right, you just need to determine how many bytes the leading and trailing byte array will remove for your situation.
I defined it for my file by comparing the source image file and the file exported from the database (not for the Stream object, see my first code) in a hex editor. As soon as I found out what information (header and footer) was added by MS Access, I then knew how many bytes to delete.
EDIT:
The size of the header added by MS Access when the image is stored as a “Package” varies depending on the file type and the source location (full path information) of the image when it was dumped into the MS Access database. Thus, even for the same type of file, you may have a different number of bytes to cut from the header for each file. This complicates the task because you have to scan the byte array until you find the normal file start information for this type of file, and then split everything in front of it.
All this headache is one of the reasons why it is better to store images in the form of "Long binary BLOB data" in a database. The search is much easier. I do not know if you have the opportunity to do this, but if so, it would be nice.