Recent

Author Topic: Load a jpg photo into DBImage on a form ...  (Read 818 times)

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 278
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Load a jpg photo into DBImage on a form ...
« on: July 19, 2024, 11:03:02 pm »
Does anyone know how to load a small photo.jpg into a DBImage component on a form. Easy enough to put the component on a form, define a PopUpMenu.  Maybe the issue is how to setup the PopUpMenu to use the windows File Explorer.

Not sure how to approach this, any suggestions would be great ...

JanRoza

  • Hero Member
  • *****
  • Posts: 700
    • http://www.silentwings.nl
Re: Load a jpg photo into DBImage on a form ...
« Reply #1 on: July 20, 2024, 01:23:38 am »
Something like this?

Code: Pascal  [Select][+][-]
  1. procedure TfrmData.btnPictureClick(Sender: TObject);
  2. var
  3.   BlobField: TBlobField;
  4.   Tmp_img: TImage;
  5.   fStream: Tstream;
  6.   jpg_img : TJpegImage;
  7. begin
  8.   if (SelectPicture.Execute)
  9.   then begin
  10.        Tmp_img := TImage.Create(Self);
  11.        BlobField := (DataModule1.qryxxx.fieldbyname('fieldname') as TBlobField);
  12.        fstream := TMemoryStream.Create;
  13.  
  14.        try
  15.          jpg_img := TJpegImage.Create;
  16.          Tmp_img.Picture.LoadFromFile(SelectPicture.FileName);
  17.          jpg_img.Assign(Tmp_img.Picture.Bitmap);
  18.          jpg_img.SaveToStream(fstream);
  19.  
  20.          DataModule1.qryxxx.Edit;
  21.          BlobField.LoadFromStream(fstream);
  22.          DataModule1.qryxxx.Post;
  23.          DataModule1.qryxxx.ApplyUpdates;
  24.          DataModule1.DbTransaction.CommitRetaining;
  25.        finally
  26.          Tmp_img.Free;
  27.          jpg_img.Free;
  28.        end;
  29.   end;
  30.  
  31. end;
  32.  
OS: Windows 11 / Linux Mint 22
       Lazarus 4.0 RC FPC 3.2.2
       CodeTyphon 8.50 FPC 3.3.1

cdbc

  • Hero Member
  • *****
  • Posts: 1678
    • http://www.cdbc.dk
Re: Load a jpg photo into DBImage on a form ...
« Reply #2 on: July 20, 2024, 08:23:29 am »
Hi
A small note of warning:
IIRC you're using TDbf ~ 'dbase' as you database backend?
If memory serves, back in the day, I and several others, have had our share of problems, with putting images in dbf-tables and getting that to play nice with the image-components in the lcl, db-aware or not.
I seem to remember, the solution on my part, were to put the images in a directory on disk and then only storing the filenames in db-tables...  %)
There should be a lot of scribble on the interweb about such 'Shenanigans'...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

wp

  • Hero Member
  • *****
  • Posts: 12476
Re: Load a jpg photo into DBImage on a form ...
« Reply #3 on: July 20, 2024, 05:29:22 pm »
I think you are using TDbf. I checked again, both fpc and svn versions: none of them supports the ftGraphic FieldType which is needed for using TDBImage (take a look at the methods TDbfFieldDef.NativeToVCL and TDbfFieldDef.VCLToNative in unit dbf_fields, they do not mention "ftGraphic").

What you can do is to declare the graphic field as FieldType ftBlob and use a standard TImage. Some event handlers are needed to keep the TImage in sync with the dbf. See details in https://forum.lazarus.freepascal.org/index.php/topic,65525.msg499345.html#msg499345

 

TinyPortal © 2005-2018