Recent

Author Topic: Lazreport + Image + Blog Field  (Read 17158 times)

cpalx

  • Hero Member
  • *****
  • Posts: 753
Lazreport + Image + Blog Field
« on: December 03, 2012, 03:17:23 am »
i searched a lot how to make report (Lazreport) to display images from a database (Bytea field in Postgres) and the only thing is to have the files in the hard disk. Any other idea?


« Last Edit: December 03, 2012, 04:05:25 am by cpalx »

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Lazreport + Image + Blog Field
« Reply #1 on: December 03, 2012, 08:17:14 am »
Yes, store them in a BLOB in the database.

Tony Whyman's remarks here
http://bugs.freepascal.org/view.php?id=22416
indicate there's currently support for BLOB images in Lazreport.

Don't use Lazreport myself, you might want to have a look at the wiki documentation and/or Lazreport source code to see how you should do this.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

denver

  • Jr. Member
  • **
  • Posts: 67
Re: Lazreport + Image + Blog Field
« Reply #2 on: December 03, 2012, 03:00:16 pm »
Here is the example that I handle image Database( MySQL ) BLOB and Lazreport :

Table structure : create table imagelist (  id int AUTO_INCREMENT,  cimage blob, primary key ( id ) ) ;

I am using Zeos Lib as example ( but it also work for SQLdb ).

1. Load image from file to database :
Code: [Select]
procedure TForm1.dbsaveimage(mfilename, mfldname: String);
var
  ms : TMemoryStream ;
begin
  ms := TMemoryStream.Create ;
  ms.LoadFromFile( mfilename ) ;
  ms.Position := 0 ;
  ZQuery1.SQL.Clear ;
  ZQuery1.SQL.Add( 'insert into imagelist ( cimage ) values ( :cimage )' ) ;
  ZQuery1.Params.Items[ 0 ].LoadFromStream( ms, ftBlob )  ;
  ZQuery1.ExecSQL ;
  ms.Free

2. Load image from database and display on TImage
Code: [Select]
var
  ms : TMemoryStream ;
begin
  ZQuery1.SQL.Clear ;
  ZQuery1.SQL.Add( 'select * from imagelist where id = ' + IntToStr( id ) ) ;
  ZQuery1.Active := True ;
  ms := TMemoryStream.Create ;
  TBlobField( ZQuery1.FieldByName( 'cimage' ) ).SaveToStream( ms ) ;
  ms.Position := 0 ;
  Image1.Picture.LoadFromStream( ms ) ;
  ms.Free ; 

The above method work for jpg, bmp, png .... any type of graphic that TImage supported !

3. Load Image from database and display on Lazreport

Code: [Select]
procedure TForm1.dbimageinlazreport;
var
  ms : TMemoryStream ;
  mPict : TfrPictureView ;
  mImage : TImage ;
  mTop,mLeft,mWidth,mHeight : Integer ;
begin
  ms := TMemoryStream.Create ;
  mImage := TImage.Create( Self );
  ZQuery1.SQL.Clear ;
  ZQuery1.SQL.Add( 'select * from imagelist where id = 1' ) ;
  ZQuery1.Active := True ;
  TBlobField( ZQuery1.FieldByName( 'cimage' ) ).SaveToStream( ms ) ;
  ms.Position := 0 ;
  mImage.Picture.LoadFromStream( ms ) ;
  mTop := 10 ;
  mLeft := 10 ;
  mWidth := 50 ;
  mHeight := 50 ;
  mPict := TfrPictureView.Create ;
  mPict.SetBounds( mTop,mLeft,mWidth,mHeight ) ;
  mPict.Picture.Assign( mImage.Picture ) ;
  frReport1.Pages[ 0 ].Objects.Add( mPict ) ;
  mImage.Free ;
  ms.Free ;
  // no need to free mPict, frReport1 will free it automatically
end;   

paweld

  • Hero Member
  • *****
  • Posts: 1003
Re: Lazreport + Image + Blog Field
« Reply #3 on: December 03, 2012, 03:09:02 pm »
put picture object on report (master data band) and add event OnEnterRect for frReport:
Code: [Select]
procedure TForm1.raportEnterRect(Memo: TStringList; View: TfrView);
var
  pic: TStream;
begin
  if (View.Name='Picture1') then //'Picture1' - name of PictureObject in report
  begin
    TFrPictureView(View).Picture.Clear;
    pic:=photods.CreateBlobStream(photods.FieldByName('photo_blob'), bmRead); //photods - dataset with blob field (photo_blob)
    TFrPictureView(View).Picture.LoadFromStream(pic);
    pic.Free;
  end;
end;
Best regards / Pozdrawiam
paweld

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Lazreport + Image + Blog Field
« Reply #4 on: December 03, 2012, 03:26:38 pm »
Anybody interested in adding those examples to the LazReport wiki page ;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

 

TinyPortal © 2005-2018