Recent

Author Topic: Load image from file to database and show later ?  (Read 2403 times)

rretamar

  • New Member
  • *
  • Posts: 32
Load image from file to database and show later ?
« on: December 10, 2018, 05:59:35 pm »
Hi.
I can't load a image file (jpg, png...) to a Firebird blob field and show later into form. ¿ Any sample code for this ?.

Thanks, Ramon

GetMem

  • Hero Member
  • *****
  • Posts: 3782
Re: Load image from file to database and show later ?
« Reply #1 on: December 10, 2018, 09:16:33 pm »
Try this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   St: TStream;
  4. begin
  5.   SQLQuery1.Edit;
  6.   St := SQLQuery1.CreateBlobStream(SQLQuery1.FieldByName('PICTURE'), bmWrite); //PICTURE --> BLOB SUB_TYPE 0 SEGMENT SIZE 80(binary)
  7.   try
  8.     Image1.Picture.SaveToStream(St);
  9.     SQLQuery1.ApplyUpdates;
  10.     SQLTransaction.CommitRetaining;
  11.   finally
  12.     St.Free;
  13.   end;
  14. end;    

Then with IBExpert is clearly visible that the picture is saved to database(see attachment).
« Last Edit: December 10, 2018, 09:19:45 pm by GetMem »

rretamar

  • New Member
  • *
  • Posts: 32
Re: Load image from file to database and show later ?
« Reply #2 on: December 10, 2018, 11:57:58 pm »
Try this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   St: TStream;
  4. begin
  5.   SQLQuery1.Edit;
  6.   St := SQLQuery1.CreateBlobStream(SQLQuery1.FieldByName('PICTURE'), bmWrite); //PICTURE --> BLOB SUB_TYPE 0 SEGMENT SIZE 80(binary)
  7.   try
  8.     Image1.Picture.SaveToStream(St);
  9.     SQLQuery1.ApplyUpdates;
  10.     SQLTransaction.CommitRetaining;
  11.   finally
  12.     St.Free;
  13.   end;
  14. end;    

Then with IBExpert is clearly visible that the picture is saved to database(see attachment).

Hi. Thanks for reply.
I use this sample code, and assign the field to dbimage component. When try to read te field content, receive a "stream read error".

Ramon

lucamar

  • Hero Member
  • *****
  • Posts: 3188
Re: Load image from file to database and show later ?
« Reply #3 on: December 11, 2018, 12:45:37 am »
I use this sample code, and assign the field to dbimage component. When try to read te field content, receive a "stream read error".

I'm not sure but IIRC you have to use a class which can read the exact format of file which is saved to the BLOB. If it's a JPEG then a TJPEGImage; if a PNG, a TPortableNetworkGraphic, etc. If this is the case and your BLOBs can be in various graphic formats I'd add a field in the record to save the format.

Or you can try:
  Picture.LoadFromStreamWithFileExt(Stream, FileExt)
for example:
Code: Pascal  [Select][+][-]
  1. {Load Stream from (jpeg) BLOB and then do ...}
  2. Image.Picture.LoadFromStreamWithFileExt(Stream, 'jpg');
« Last Edit: December 11, 2018, 12:52:21 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 855
Re: Load image from file to database and show later ?
« Reply #4 on: December 11, 2018, 06:10:11 am »
hello,
have a look to my database example (coming with Lazarus distribution in Lazarus\examples\Database\Image_Mushrooms ) :

Quote
[MushRoomsDatabase by Jurassic Pork using SQLite - January 2014
Updated for Firebird Embedded - August 2014

Features:
- Use SqlDb and lazreport components.
- Sqlite3 or Firebird embedded database DeadlyMushrooms with 5 mushrooms.
Sqlite3 will be tried first; if no Sqlite library is available, Firebird
embedded will be tried.
- It demonstrates:
- creating a new SQLite3 database with table if the db does not exist
- use of TSQLScript to run multiple SQL statements
- use of FBAdmin to restore Firebird backup (smaller than the live .fdb file)
on first run, useful for keeping your setup file small and compatible with
older Firebird versions
- The images are stored in blob field without extension at the beginning.
With this you can view blob images with database browser editor
(e.g. sqlite2009pro).
- In the database there is also a field with images links (filenames).
- The linked images are stored in the folder images of the project.
- You can see the linked images in a Timage.
- You can change the images in the database:
- for Tdbimage (image in db): double click on the component and choose your
image.
- for Timage (linked image): click on the button near the image filename
(you must be in edit mode).
- Transaction commits when you click on Tdbnavigator refresh button or on close
form.
- Small pictures of the mushrooms are in the sqlite3Database. Largest images are
in files in the folder images.
- Print button to print all the mushrooms (lazreport).
On each page you have:
- a title.
- the field common_name of the mushroom database.
- the field notes of the mushroom database.
- the field picture of the mushroom database (picture picture1).
- the picture of the field image_link (picture picture2).
The report name is Mushroom_Report.lrf

You need to install Lazreport and sqldb packages for the example to work.

Friendly, J.P

Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

rretamar

  • New Member
  • *
  • Posts: 32
Re: Load image from file to database and show later ?
« Reply #5 on: December 11, 2018, 03:17:24 pm »
I need a sample with dbimage component.  :(

GetMem

  • Hero Member
  • *****
  • Posts: 3782
Re: Load image from file to database and show later ?
« Reply #6 on: December 11, 2018, 05:44:42 pm »
Quote
I need a sample with dbimage component.  :(
Why? Drop another TImage to your form, then use the AfterScroll event of the query:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.SQLQuery1AfterScroll(DataSet: TDataSet);
  2.  var
  3.    St: TStream;
  4.  begin
  5.    if SQLQuery1.FieldByName('PICTURE').IsNull then
  6.    begin
  7.      Image2.Picture.Clear;
  8.      Exit;
  9.    end;
  10.  
  11.    St := SQLQuery1.CreateBlobStream(SQLQuery1.FieldByName('PICTURE'), bmRead);
  12.    try
  13.      Image2.Picture.LoadFromStream(St);
  14.    finally
  15.      St.Free;
  16.    end;
  17. end;
  18.  
« Last Edit: December 11, 2018, 08:58:59 pm by GetMem »

 

TinyPortal © 2005-2018