My program manages "items" (think of them as stately homes) and associates with each item zero, one, or more images. Until now these are outside the database. The items in the database point to images by file name and path. Images are held in a dedicated folder tree. Now it is required to move the images into the database itself. Image types required to be handled are: jpeg (the most common, sometimes as large as 4 Mb) bmp (also very common, and quite large) png emf wmf gif (static only, rare, but does occur).
Currently, when a file is associated with an Item, nothing is done with the image other than to create an entry in the Images database table. When items are being reviewed by the user, image thumbnails are presented using ShellBrowser to access the file and generate a 96 x 96 thumbnail bitmap that is presented on screen.
My plan is to store the images and thumbnails in the Images table as two new BLOB fields per record, Full Image and Thumbnail, both being bitmaps.
This is my first foray into images in a database so please bear with me as I try to get up to speed.
Question 1: Does the following code segment convert the above 6 file types to a full image bitmap ? It seems to do so.
uses
Graphics, Jpeg, pngimage, GIFImg;
procedure TForm1.Button1Click (Sender: TObject);
var
Picture: TPicture;
Bitmap: TBitmap;
begin
Picture := TPicture.Create;
try
Picture.LoadFromFile('C:\imagedata.dat');
Bitmap := TBitmap.Create;
try
Bitmap.Width := Picture.Width;
Bitmap.Height := Picture.Height;
Bitmap.Canvas.Draw(0, 0, Picture.Graphic);
Bitmap.SaveToFile('C:\test.bmp');
finally
Bitmap.Free;
end;
finally
Picture.Free;
end;
end;
Question 2: How to move the bitmap into the Blob field, using a TStream or a TMemoryStream ? advantages and problems one versus the other. Code moving bitmap to stream and from stream to BLOB ??
Question 3: My feeling is that storing the original files in the Blob field would occupy less space but be more difficult to present on screen. Any thoughts ?
TPicture.LoadFromFile()uses the file extension to know whichTGraphicclass to use for loading the data. Unless you have created a custom class, none of the standard classes use.dat.use
TDataSet.CreateBlobStream()withTGraphic.SaveToStream()/LoadFromStream(). I wouldn't forcibly convert all images to BMP for storage, though. That will bloat the database. JPGs and PNGs are much more compact.images stored in a blob can be displayed in a
TDBImage. Or, you can simply load the image yourself and then assign it toTImage.