Recent

Author Topic: How do I load an image from a TImageList to a Graphics field?  (Read 4876 times)

RedOctober

  • Sr. Member
  • ****
  • Posts: 452
How do I load an image from a TImageList to a Graphics field?
« on: January 29, 2018, 12:43:18 am »
Platform: Windows Server 2016, Laz 1.8.1, FPC 3.0.5
I have created a calculated TGraphicField in a TSQLQuery calle "IsActv".
I have a TImageList with two .png s in it... a green check and a red dot.
I have a string field that will contain either of the values 'GREEN_CHECK' or 'RED_DOT'.
How do I assign the green dot graphic in the TImageList to the calculated graphics field?
Code: Pascal  [Select][+][-]
  1. procedure TfrmDBTrigs.qryLstCalcFields(DataSet: TDataSet);
  2. begin
  3.   if qryLstSHOW_IMAGE.AsString = 'GREEN_DOT' then
  4.     qryLstIsActv.  ???  // <-- ImgLst.  ?? items[0] ??
  5.   else
  6.     qryLstIsActv.  ???  // <-- ImgLst.  ?? items[1] ??
  7. end;
  8.  

What is the syntax that should replace the ???  characters?

Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: How do I load an image from a TImageList to a Graphics field?
« Reply #1 on: January 29, 2018, 01:45:10 am »
Hi

Done a quick demo of assigning an image.bitmap from an imagelist index. Hopefully it may help.


Using your example something along the lines of

var gr_idx:integer=0;
....
begin
....
  gr_idx:=0;  // default to green
  if qryLstSHOW_IMAGE.AsString = 'RED_DOT' then gr_idx:=1;
  imagelist1.GetBitmap(gr_idx,qryLstIsActv.Picture.Bitmap); // assuming qryLstIsActv is a timage.
....
end; 
« Last Edit: January 29, 2018, 01:51:54 am by josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

RedOctober

  • Sr. Member
  • ****
  • Posts: 452
Re: How do I load an image from a TImageList to a Graphics field?
« Reply #2 on: January 29, 2018, 02:16:05 am »
Hi Josh,

qryLstIsActv is a TGraphicField.  There is no .Picture property and no Picture.Bitmap property.

Hence my original question.

balazsszekely

  • Guest
Re: How do I load an image from a TImageList to a Graphics field?
« Reply #3 on: January 29, 2018, 09:23:17 am »
@RedOctober

Change TGraphicField to TLargeintField then:
Code: Pascal  [Select][+][-]
  1. procedure TfMain.DBGridDrawColumnCell(Sender: TObject; const Rect: TRect;
  2.   DataCol: Integer; Column: TColumn; State: TGridDrawState);
  3. var
  4.   Bmp: TBitmap;
  5. begin
  6.   if DataCol = 0 then
  7.   begin
  8.     Bmp := TBitMap.Create;
  9.     try
  10.       ImageList1.GetBitmap(TDBGrid(Sender).DataSource.DataSet.FieldByName('IsActv').AsInteger, Bmp);
  11.       Bmp.Canvas.Draw(0, 0, Bmp);
  12.       TDBGrid(Sender).Canvas.Draw(Rect.Left + 2, Rect.Top + 2, Bmp);
  13.     finally
  14.       Bmp.Free;
  15.     end;
  16.   end;
  17. end;
  18.  
  19. procedure TfMain.qryLstIsActvCalcFields(DataSet: TDataSet);
  20. begin
  21.   if not Dataset.Active then
  22.     Exit;
  23.  
  24.   if qryLstSHOW_IMAGE.AsString = 'GREEN_DOT' then
  25.      DataSet.FieldByName('IsActv').AsInteger := 0
  26.    else
  27.      DataSet.FieldByName('IsActv').AsInteger := 1
  28. end;
  29.  
  30. procedure TfMain.qryLstIsActvIsactvGetText(Sender: TField; var aText: string;
  31.   DisplayText: Boolean);
  32. begin
  33.   aText := '';
  34. end;          
  35.  

PS: You can generate the last event "qryLstIsActvIsactvGetText" by  doubleclicking the query-->select calc field isact-->OI events.

 

TinyPortal © 2005-2018