Title: Using ADO with JPEG's stored in a MS Access database
Question: In trying to use an Access database to store JPEG's I found several 'tips' out there, but none seemed to work properly. So I figured I would post my complete solution here.
Answer:
When working with pictures in a database, at least in my case, there are 3 things I want to be able to do: Store the pictures in the database, retrieve the picture for use in my application, and save the picture back to a file if needed.
I will discuss the functions I created to do this and have also included the Delphi 5 source code for a working test project. (This may work in prior Delphi versions as well though I haven't tested it.)
Also I am assuming your BLOB (Binary Large Object) field to use for the pictures was created in MS Access as an OLE Object field. Again this will probably work using different databases but I haven't tested that either. Finally, I wanted to keep things simple so there is no error checking or exception handling in these functions.
--------------------------------
STORING JPEG'S IN THE DATABASE
--------------------------------
First we need to store the pictures in the database, we are assuming that the picture is to come from a file. Here is the function:
procedure StoreJpeg(FilePath: string; Field: TBlobField);
begin
Field.LoadFromFile(FilePath);
end;
'FilePath' is the path to the source JPEG file.
'Field' is the database field you want to store the picture in. NOTE: This is a TBlobField so you may have to typecast your field in the function call like this:
StoreJpeg('C:\MyPic.jpg', TBlobField( MyTable.FieldByName('MyField') ));
-----------------------------------
RETRIEVING JPEG'S FROM THE DATABASE
-----------------------------------
Now that we have pictures stored in the database, we want to be able to retrieve those for use in our application. In this case I want to have a TImage on the form and load the JPEG into that. If we were using Bitmaps this would be very easy, but since JPEG's are smaller in size I prefer those. The TImage doesn't like JPEG's very much so we have to trick it into loading our image. (Had we been using Bitmaps, we could just assign it the data in the BlobStream).
procedure GetJpeg(Image: TImage; Field: TBlobField);
var
Jpeg: TJpegImage;
BS: TADOBlobStream;
begin
// Create a JpegImage
Jpeg := TJpegImage.Create;
// Read the picture's data into the BlobStream
BS := TADOBlobStream.Create(Field,bmRead);
// Load the data into the JpegImage
Jpeg.LoadFromStream(BS);
// Assign the JpegImage to the TImage
Image.Picture.Assign(Jpeg);
BS.Free;
Jpeg.Free;
end;
'Image' is the TImage we are using on the form, the simpliest method I found was to pass that into this procedure and do the assignment there. I could have created a function and returned the TJpegImage, then assigned that to the TImage elsewhere in the code, but this makes things a bit cleaner.
'Field' is the database field where the picture is stored. Again you may need to typecast your procedure call as a TBlobField.
Essentially what this is doing is reading the data in the field into the TBlobStream, then loading that data into a TJpegImage (who knows how to deal with JPEG'S), and finally assigning that to the TImage.
This implementation is much simplier than some of the other examples I found out there, which makes me think I may have missed something here, but it seems to work well for me.
----------------------------------------------
SAVING A JPEG STORED IN THE DATABASE TO A FILE
----------------------------------------------
Lastly, now that we can store and use JPEG's, there may be a call to recreate the JPEG in a file. This is the inverse of what we did to store the file the first time.
function SaveJpeg(Field: TBlobField; FilePath: string): boolean;
begin
Field.SaveToFile(FilePath);
end;
'Field' is the database field where the JPEG is stored.
'FilePath' is the location and filename where you would like the file created.
Pretty straight forward here, and now we've come full circle from our original JPEG file which we stored in the database, then used in the application, and finally we are able to recreate the file.
-------
I hope this article is useful.
Jeff Guidotti