Forum > Databases

Load image from file to database and show later ?

(1/2) > >>

rretamar:
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

balazsszekely:
Try this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);var  St: TStream;begin  SQLQuery1.Edit;  St := SQLQuery1.CreateBlobStream(SQLQuery1.FieldByName('PICTURE'), bmWrite); //PICTURE --> BLOB SUB_TYPE 0 SEGMENT SIZE 80(binary)  try    Image1.Picture.SaveToStream(St);    SQLQuery1.ApplyUpdates;    SQLTransaction.CommitRetaining;  finally    St.Free;  end;end;    
Then with IBExpert is clearly visible that the picture is saved to database(see attachment).

rretamar:

--- Quote from: GetMem on December 10, 2018, 09:16:33 pm ---Try this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);var  St: TStream;begin  SQLQuery1.Edit;  St := SQLQuery1.CreateBlobStream(SQLQuery1.FieldByName('PICTURE'), bmWrite); //PICTURE --> BLOB SUB_TYPE 0 SEGMENT SIZE 80(binary)  try    Image1.Picture.SaveToStream(St);    SQLQuery1.ApplyUpdates;    SQLTransaction.CommitRetaining;  finally    St.Free;  end;end;    
Then with IBExpert is clearly visible that the picture is saved to database(see attachment).

--- End quote ---

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:

--- Quote from: rretamar on December 10, 2018, 11:57:58 pm ---I use this sample code, and assign the field to dbimage component. When try to read te field content, receive a "stream read error".

--- End quote ---

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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{Load Stream from (jpeg) BLOB and then do ...}Image.Picture.LoadFromStreamWithFileExt(Stream, 'jpg');

Jurassic Pork:
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
--- End quote ---

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

Friendly, J.P

Navigation

[0] Message Index

[#] Next page

Go to full version