How To…Use the OnGetPictureEvent

Question

“How can I use the DBImage.OnGetPicture event to conditionally display a default image? For example, some records in my dataset have any empty image field.”

Solution

The following examples uses the DBImage.OnGetPicture event to conditionally display a default image.

Download: UseDBImageOnGetPictureEvent.zip

Delphi code sample:

procedure TForm1.FormCreate(Sender: TObject);
begin

  FDefaultPicture := TPicture.Create;

  // load the default image
//  FDefaultPicture.Bitmap.LoadFromFile('c:\myDefaultImage.bmp');

  // for this example, we will create a simple bitmap
  FDefaultPicture.Bitmap.Width := ppDBImage1.spWidth;
  FDefaultPicture.Bitmap.Height := ppDBImage1.spHeight;
  FDefaultPicture.Bitmap.Canvas.Font.Size := 14;
  FDefaultPicture.Bitmap.Canvas.TextOut(10, 10, '(No Image Available)');

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FDefaultPicture.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ppReport1.Print;
end;

procedure TForm1.ppDBImage1GetPicture(Sender: TObject; aPicture: TPicture);
var
  lPicture: TPicture;
begin

  // call DataPipeline.GetFieldAsPicture method
  lPicture := plBioLife.GetFieldAsPicture('Graphic');

  if lPicture <> nil then
    aPicture.Assign(lPicture)
  else
    aPicture.Assign(FDefaultPicture);

  // for this example, replace the third image on each page
  if ppReport1.DetailBand.Count = 3 then
    aPicture.Assign(FDefaultPicture);

end;