Forum > Databases

Load MySQL Blob into TImage

(1/2) > >>

HopefulGuy:
I have a question. I'm working on a program that reads information from MariaDB (mysql clone) tables. Some of these tables contain BLOB fields that are actual jpeg files. So far, the only way I have found to actually load this into a TImage is to write the contents of the blob field to a file, then use the TImage.LoadFromFile routine. Isn't there a better way? Some way I can just take the TImage data and dump it straight into the database, and then later read the field straight back into the TImage?

Thanks!

lucamar:
If for some reason you don't want to use TDBImage and have to do it "by hand", you can always save the data to a (memory) stream rather than a file and load the image from there with TImage.LoadFromStream(), though note that the advisability of doing it like that depends on the size of the BLOB and the available memory.

HopefulGuy:
I didn't see TDBImage, thanks for pointing that out. I'll have to experiment with that. Hopefully that will work.

HopefulGuy:

--- Quote from: lucamar on May 08, 2021, 11:53:30 am ---If for some reason you don't want to use TDBImage and have to do it "by hand", you can always save the data to a (memory) stream rather than a file and load the image from there with TImage.LoadFromStream(), though note that the advisability of doing it like that depends on the size of the BLOB and the available memory.

--- End quote ---

So, I looked at a TDBImage, and it looks like it MAY work, But I'm not 100% sure. One thing I'm confused about is how I would translate an image loaded from a local file into a blob on the database? If it's just replacing, that's one thing. But if I need to add a new image to the system, then that constitutes a new record in the image table. So how do I tell Lazarus that what I want it to do is to insert a new record into the sub-table when I add a new image? I can see the TDBImage.Picture.LoadFromFile() routine, and that's great for the picture from a file. But once that happens, a DB Aware component is going to try to update an existing record, or, if I'm in insert mode, insert a new record. But the record can not be inserted without extra data that is not present in the TDBImage (namely a record number for the person in the main table who owns that image, and the sequenc number of that picture).

I'm sure that some of this has to do with the SQL that would be reading the image from the table in the first place, but I'm still lost! Any help is appreciated.

Jurassic Pork:
hello,
you can have a look to my database example (in Lazarus\examples\Database\Image_Mushrooms ) :
it is for sqlite but using sqldb components.

--- Quote ---- 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.
--- End quote ---

Friendly, J.P

Navigation

[0] Message Index

[#] Next page

Go to full version