DBImage (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example requires a TDBImage, a TClientDataSet and a TDataSource already on the form. A custom data set is created and a record is added to it, containing a sample image. The TDBImage control then displays the 'Image' field in the data source. The Proportional flag of the TDBImage control is set to True in order to make the sample image fit the bounds of the TDBImage.

Code

__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  ClientDataSet1->Active = false;
  TFieldDef *fieldDef= ClientDataSet1->FieldDefs->AddFieldDef();
  fieldDef->DataType = ftString;
  fieldDef->Size = 20;
  fieldDef->Name = "Name";

  fieldDef = ClientDataSet1->FieldDefs->AddFieldDef();
  fieldDef->DataType = ftGraphic;
  fieldDef->Name = "Image";

  DataSource1 = new TDataSource(Form1);
  DataSource1->DataSet = ClientDataSet1;

  ClientDataSet1->CreateDataSet();

  ClientDataSet1->Insert();
  ClientDataSet1->FieldByName("Name")->Value = "test";
  dynamic_cast<TGraphicField*>(ClientDataSet1->FieldByName("Image"))->LoadFromFile("..\\flower.bmp");
  ClientDataSet1->Post();

  DBImage1->DataSource = DataSource1;
  DBImage1->Proportional = True;
  DBImage1->DataField = "Image";
}

Uses