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. Commented 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. Commented 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. Commented 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 :) Commented Dec 14, 2012 at 10:15

1 Answer 1

14
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;
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, now I'll try to implement my code with these examples
Hey, explore class TBlobStream :-) As far as i remember you can avoid intermediate creation of TMemoryStream and get the stream directly from the field!
@bummi does above code work with Bitmap only? What about JPG, JPEG images stored in the database? How to display them using dataset?
@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
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).
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.