Recent

Author Topic: Display base64 image in DBImage  (Read 570 times)

nikel

  • Full Member
  • ***
  • Posts: 218
Display base64 image in DBImage
« on: October 08, 2024, 08:59:23 pm »
Hello, I have a tif image in my database. I want to display it in DBImage component. Here's my code so far:

Code: Pascal  [Select][+][-]
  1. {$IFDEF WINDOWS}
  2. SQLiteLibraryName:='sqlite3.dll';
  3. {$ENDIF}
  4.  
  5. SQLite3Connection1.DatabaseName:='/home/user/Desktop/test.sqlite';
  6. SQLite3Connection1.Transaction:=SQLTransaction1;
  7. SQLQuery1.Database:=SQLite3Connection1;
  8. SQLQuery1.Transaction:=SQLTransaction1;
  9. SQLTransaction1.DataBase:=SQLite3Connection1;
  10.  
  11. SQLite3Connection1.Open;
  12. SQLTransaction1.Active:=True;
  13.  
  14. SQLQuery2.Database:=SQLite3Connection1;
  15. SQLQuery2.Transaction:=SQLTransaction1;
  16. SQLQuery2.SQL.Text:='SELECT * FROM tbl_main';
  17. SQLQuery2.Open;
  18.  
  19. DataSource1.DataSet := SQLQuery2;
  20. DBNavigator1.DataSource := DataSource1;
  21.  
  22. Name_Dbe.DataSource:=DataSource1;
  23. Name_Dbe.DataField:='txt_name';
  24.  
  25. Image_Dbi.DataSource:=DataSource1;
  26. Image_Dbi.DataField:='blob_image';

How can I display a tif image or its base64 decoded image in a DBImage?
« Last Edit: October 08, 2024, 09:19:53 pm by nikel »

wp

  • Hero Member
  • *****
  • Posts: 12476
Re: Display base64 image in DBImage
« Reply #1 on: October 08, 2024, 10:36:50 pm »
In the attachment a small project which allows images of any type supported by the graphics unit in an sqlite3 database.

nikel

  • Full Member
  • ***
  • Posts: 218
Re: Display base64 image in DBImage
« Reply #2 on: October 09, 2024, 09:32:36 am »
Thanks for the nice example but I need to add images programmatically.

Thaddy

  • Hero Member
  • *****
  • Posts: 16201
  • Censorship about opinions does not belong here.
Re: Display base64 image in DBImage
« Reply #3 on: October 09, 2024, 09:53:48 am »
Then just add base64 to the uses clause and use TBase64EncodingStream and TBase64DecodingStream and store the image as text (base64 is a text format) and decode. That is simple. You should be able to do that yourself, but else report back.Can take some time if I answer this myself with an example, though. It has not my interest.
If I smell bad code it usually is bad code and that includes my own code.

rvk

  • Hero Member
  • *****
  • Posts: 6594
Re: Display base64 image in DBImage
« Reply #4 on: October 09, 2024, 10:05:10 am »
Thanks for the nice example but I need to add images programmatically.
Maybe you can explain it a bit more clear...
Do you want (or have) a base64-encoded .tiff inside the blob?
Or do you want plain unencoded .tiff in the blob? (which is what is normally in a blob)

wp showed you how to program a TDBImage which can be used to display images directly from the database.
But I'm not sure TDBImage (or TImage) can support .tiff (it failed to load a .tiff image for me when I tried to load it in a TImage).
Is that what your problem is?
But then I don't understand your mention of base64. So can you explain what you want base64 has to do with this?

nikel

  • Full Member
  • ***
  • Posts: 218
Re: Display base64 image in DBImage
« Reply #5 on: October 09, 2024, 10:17:34 am »
Thanks for the replies. I want to insert about 4500 images to database. I won't insert them manually by clicking on a field but using code. I thought it was impossible to insert and display "tif" format but wp showed me how to do.

Sorry for my English.

Modify:
I realized that my tif images are broken.
« Last Edit: October 09, 2024, 01:32:31 pm by nikel »

nikel

  • Full Member
  • ***
  • Posts: 218
Re: Display base64 image in DBImage
« Reply #6 on: October 18, 2024, 09:07:17 am »
I tried this:

Code: Pascal  [Select][+][-]
  1. DecodedStream := TStringStream.Create('');
  2. DecodedStream.LoadFromFile(IMAGE_BASE_PATH + FillWithZeros(RepNo.ToString, MAX_DIGITS) + '_' + Num.ToString + '.jpg');
  3. EncodedStream := TStringStream.Create('');
  4. Encoder       := TBase64EncodingStream.Create(EncodedStream);
  5. Encoder.CopyFrom(DecodedStream, DecodedStream.Size);
  6. Output := EncodedStream.DataString;
  7.  
  8. SQLQuery1.ParamByName('SHEET').AsBlob:=EncodedStream.Bytes;
  9.  
  10. SQLQuery1.ExecSQL;
  11. SQLTransaction1.Commit;
  12. SQLQuery1.Clear;
  13.  
  14. DecodedStream:=nil;
  15. EncodedStream:=nil;
  16. Encoder:=nil;

But I can't view the image. The image is empty but 96Kb.

rvk

  • Hero Member
  • *****
  • Posts: 6594
Re: Display base64 image in DBImage
« Reply #7 on: October 18, 2024, 09:23:15 am »
But I can't view the image. The image is empty but 96Kb.
Why are you encoding the image to base64 while blob can handle byte data?

TDBImage can't handle base64 but only raw data.

Second, even if you have the raw data to the blob, you might need to implement the onimageread to say it's a tiff.
https://lazarus-ccr.sourceforge.io/docs/lcl/dbctrls/tdbimage.ondbimageread.html

And if you keep wanting to use base64 in the blob, you shouldn't use TDBImage but read the blob and put it in a TImage.

(TDBImage would also write raw data back to the database including a header. The header can be disabled but the raw data not.)

nikel

  • Full Member
  • ***
  • Posts: 218
Re: Display base64 image in DBImage
« Reply #8 on: October 18, 2024, 10:00:33 am »
I didn't know those. Thanks a lot.

 

TinyPortal © 2005-2018