I know that storing images in a database is oftern a bad idea.
However there can come requirements that force things on you.
For the database end I have used an IMAGE datatype in SQL Server.
The delphi approach is as follows:
You need to use GraphicEx to allow display in the normal controls.
All this requires is adding the GraphicEx unit to the end of your uses clause.
procedure LoadJPEGImageFromStream(const aImage: TImage;
const stream: TStream);
var
aJPEG : TJpegImage;
begin
aJPEG := TJPEGImage.Create;
try
aJPEG.LoadFromStream(stream);
aImage.Picture.Assign(aJPEG);
finally
aJPEG.Free;
end;
end;
procedure LoadFromDB(Image : TImage; aField : TBlobField)
var
aBLOBStream : TStream;
begin
ado.Open;
ado.First;
aBLOBStream := adoFAX.CreateBlobStream(aField,bmRead);
try
LoadJPEGImageFromStream(Image, aBLOBStream );
finally
aBLOBStream.free;
end;
end;
procedure SaveToDB(const filename : string)
var
FS : TFileStream;
begin
FS := TFileStream.Create(filename, fmOpenRead);
try
ado.open;
ado.Insert;
adoIMAGE.LoadFromStream(FS);
ado.Post;
finally
FS.Free;
end;
end;
Just to check that this is not a Delphi-only format I wrote the following test from C#:
sqlDataAdapter1.Fill(dataSet11, “DB”);
// How to get an image from a database in .NET
pictureBox1.Image = new Bitmap( new MemoryStream( dataSet11.DB[0].IMAGE ) );
pictureBox1.Height = pictureBox1.Image.Height;
pictureBox1.Width = pictureBox1.Image.Width;
This loads the image previously stored by a Delphi app.
This ensures that the image format used is language neutral.