Load and save image from blob field in delphi using firebird

stacker picture stacker · Dec 13, 2012 · Viewed 22.6k times · Source

In my Firebird database I have a Blob field that contain a Bitmap. I'll have to load and display in a TImage located on my Form. Subsequently I'll have to save in the same field the image selected by a OpenDialog.

Answer

bummi picture bummi · Dec 13, 2012
Procedure LoadBitmapFromBlob(Bitmap: TBitmap; Blob: TBlobField);
var
  ms, ms2: TMemoryStream;
begin
  ms := TMemoryStream.Create;
  try
    Blob.SaveToStream(ms);
    ms.Position := 0;
    Bitmap.LoadFromStream(ms);
  finally
    ms.Free;
  end;
end;

example usage

procedure TForm4.Button1Click(Sender: TObject);
var
  bmp: TBitmap;
begin
  bmp := TBitmap.Create;
  try
    LoadBitmapFromBlob(bmp, TBlobField(Dataset.FieldByName('Image')));
    Image1.Picture.Assign(bmp);
    bmp.SaveToFile(OpenDialog.FileName);
  finally
    bmp.Free;
  end;

end;