6

In my Firebird database I have a Blob field that contain a Bitmap. I'll have to load and display in a TImage located on my Form. Subsequently I'll have to save in the same field the image selected by a OpenDialog.

4
  • It is generally a bad idea to store images in the database. Instead, you should store only the path to your image on the file system.
    – LightBulb
    Dec 13, 2012 at 19:39
  • @LightBulb the client may just not have access to that file system. So while RDBMS are not media storage by design - sometimes it is simplifies system design in general. Of course for some cost. Dec 14, 2012 at 7:57
  • 2
    @LightBulb In general, making sweeping generalizations is a bad idea ;), it really depends on the application and limitations. Pros: files are backed up together with DB, applications (fat clients) that access your db don't also need access to a central file location, the file and associated db-record are guarded by the integrity access control of the database, cons: your db (and its backup) gets really fat, serving files from a webserver gets more complicated etc. Dec 14, 2012 at 9:26
  • @MarkRotteveel, thank you for clearing that up because I forgot to mention pros and cons. I was hoping that OP would do some research himself :)
    – LightBulb
    Dec 14, 2012 at 10:15

1 Answer 1

13
Procedure LoadBitmapFromBlob(Bitmap: TBitmap; Blob: TBlobField);
var
  ms, ms2: TMemoryStream;
begin
  ms := TMemoryStream.Create;
  try
    Blob.SaveToStream(ms);
    ms.Position := 0;
    Bitmap.LoadFromStream(ms);
  finally
    ms.Free;
  end;
end;

example usage

procedure TForm4.Button1Click(Sender: TObject);
var
  bmp: TBitmap;
begin
  bmp := TBitmap.Create;
  try
    LoadBitmapFromBlob(bmp, TBlobField(Dataset.FieldByName('Image')));
    Image1.Picture.Assign(bmp);
    bmp.SaveToFile(OpenDialog.FileName);
  finally
    bmp.Free;
  end;

end;
8
  • Thanks, now I'll try to implement my code with these examples
    – stacker
    Dec 13, 2012 at 15:57
  • Hey, explore class TBlobStream :-) As far as i remember you can avoid intermediate creation of TMemoryStream and get the stream directly from the field! Dec 14, 2012 at 7:58
  • @bummi does above code work with Bitmap only? What about JPG, JPEG images stored in the database? How to display them using dataset? Sep 6, 2013 at 19:01
  • 1
    @NinadAvasare you will have to know which kind of TGraphic is stored in the Blob and create a suitable Class. This information is known if you only want to store JPG images, could be stored in another field in the database, or stored with the Graphic in the blob. An example for the last way is shown here. stackoverflow.com/a/14726934/1699210
    – bummi
    Sep 6, 2013 at 19:32
  • 1
    You should add it as own question, instead of asking in comments. I can't see where IStream and jpg are created, just that they are free. #53 sound like a corrupted or invalid JPEG, e.g. a Bitmap an unsupported JPG a wrong saved (to BLOB) JPG. You could try (for debugging) to save the stream for debugging to a file and inspect it and try to load it to a TImage and/or a graphic program. Image1.Picture would be overritten in every loop(while not rs.EOF).
    – bummi
    Sep 7, 2013 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.