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.
Asked
Active
Viewed 2.7k times
6
-
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 '12 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. – Arioch 'The Dec 14 '12 at 07: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. – Mark Rotteveel Dec 14 '12 at 09: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 '12 at 10:15
1 Answers
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;

bummi
- 27,123
- 14
- 62
- 101
-
-
Hey, explore class TBlobStream :-) As far as i remember you can avoid intermediate creation of TMemoryStream and get the stream directly from the field! – Arioch 'The Dec 14 '12 at 07:58
-
@bummi does above code work with Bitmap only? What about JPG, JPEG images stored in the database? How to display them using dataset? – Ninad Avasare Sep 06 '13 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. http://stackoverflow.com/a/14726934/1699210 – bummi Sep 06 '13 at 19:32
-
@bummi , Images stored in my SQLite database are only JPG, JPEG format. Seems there is no need for Graphic Class to recognize image format as of now as image format is being known. I am getting JPEG #53 with the code below, Is it due to memory problem? Code is below – Ninad Avasare Sep 07 '13 at 03:59
-
`rs.First; j:=1; while not rs.EOF do begin if (j>sg.rowcount) then sg.rowcount := sg.rowcount + 1; for i := 0 to rs.fields.Count - 1 do begin if i = 4 then begin try if ((rs.FieldByName(names['image']).AsString)<>'') then Begin Field :=TBlobField(rs.FieldByName('image')); Field.SaveToStream(IStream); IStream.Position:=0; Jpg.LoadFromStream(IStream); Image1.Picture.Assign(Jpg); End; finally Jpg.Free; IStream.Free; end; end; end; rs.Next; inc(j); end; ` – Ninad Avasare Sep 07 '13 at 04:13
-
1You 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 07 '13 at 07:54
-
@bummi , my entire question is open at [link](http://stackoverflow.com/questions/18655031/how-to-display-blob-image-from-database-in-the-tadvstringgrid-with-the-help-of-d) ... (check last code snippet one) ....Do you want me to write Stream to file using FileStream in order to check output – Ninad Avasare Sep 08 '13 at 04:40