test

progging - To wander about and beg; to seek food or other supplies by low arts; to seek for advantage by mean shift or tricks.
progging - Programmer slang for writing computer code.

fredag 5. august 2011

Getting an Image into QuickReport


I had a big problem getting a .bmp file to show up in a QReport. Everything seemed straight forward. I had to do this dynamically during run-time, so I would read in the image to a paradox database and then have the QReport point to the correct database field.

The problem was that when I first configured the QReport, the database field was MEMO and not GRAPHIC. This information is stored in the .QR2 report file. And even though I corrected this field in the database using Database Manager, it would not be updated in the QuickReport.

The solution was to remove the dataset and add it again. This time the field correctly showed up as GRAPHIC, and the image showed up beautifully :-)

One possible solution could be to only remove the field in the dataset and then add it again.

The initial code for getting in image into the DB was:


(FieldByName('Icon') as TGraphicField).LoadFromFile('\path\to\image.bmp');

Although this works fine, the image file has to be available on the disk when the program runs. A better solution is to get the images included in the .exe as an embedded resource. The simplest way to achieve this was to include a TImageList on a form, and populate this with the images I needed. Then use the following code to put them in the database:


procedure InsertImageToDB(Field: TField; ImageIndex: Integer);
var
  stream: TStream;
  bmp: TBitmap;
begin
  bmp := TBitmap.Create();
  stream := Field.DataSet.CreateBlobStream(Field, bmWrite);
  try
    Frm.ImageList.GetBitmap(ImageIndex, bmp);
    bmp.SaveToStream(stream);
  finally
    stream.Free;
    bmp.Free;
  end;
end;
This has to be included in a Edit()...Post() block to get it posted to the DB.

Ingen kommentarer:

Legg inn en kommentar