• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 148
  • Last Modified:

Reading images from acess database and writing them to a disk

I have a table named Employees that has an Ole Column named Picture in MS Access 03.  I want to take that image (it has varying types such as .jpg and .bmp) read it from the database and write it to a folder named "Pictures" in the Application root directoy.  How do I do this?  
0
CDFA_Inc
Asked:
CDFA_Inc
  • 2
1 Solution
 
TheAvengerCommented:
Open a connection to the database, create a command with a text like SELECT PIC FROM TABLE and execute it using XxxDataReader reader = command.ExecuteReader() method (Xxx is OleDb or ODBC, depending on your connection). Then go over the results using while (reader.Read()) and read the picture using the method

byte[] image = reader.GetValue(0) as byte[];

Then if image is != null, save it to a file in the folder you want.
0
 
CDFA_IncAuthor Commented:
I understand the conversions.  I am curious as to how to write the byte array to file.  First of all the pictures in the database are of varying types.  How do you account for the title and extension of the pics.  Please post an example.
0
 
TheAvengerCommented:
The fact that you pictures have different types means that you have to read the data and examine it to find out what kind of file this is. Every picture file type has a specific structure, so if you know there will be JPEG and BMP files, you should check if the format is what JPEG expects and if not check for BMP. You can find more info about the file formats online, using google. The application AcdSee succeeds to do this - make a test by changing the extention of a JPEG to BMP and see that AcdSee will still show that the file is a JPEG file.

This is a link how to write byte array to a file. There are some additional links about file operations on this page:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiofilestreamclasswritetopic.asp
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now