Using LEADTOOLS with Data Controls (Delphi)

You can associate the LEAD control with a data control so that the LEAD control's bitmap is bound to a specified field in the data control's data set.

Take the following steps to start a project and to add some code that associates the LEAD control with a data control:

1.

Start Delphi.

2.

On the Project menu, select Remove From Project and remove Unit1 and Form1.

3.

On the Database menu, choose Form Wizard. In the first wizard dialog, select the following options and click Next:

 

 

Create a simple form

 

Create a form using TTable Object

 

4.

In the second wizard dialog, select the file IMAGES.DB shipped with LEADTOOLS and click Next.

5.

In the following dialog, click the ">>" button to select both fields, then click Next.

6.

In the following dialog, choose Vertically, then click Next.

7.

In the following dialog, choose Top, then click Next.

8.

In the last dialog, make sure that the options are as follows, then click Finish.

 

 

image\sqrblit.gif Generate a main form

 

image\sqrblit.gif Form Only

 

9.

Click on the ImageBMP control and delete it. In its place, insert a LEAD Main VCL Control (LEADImage) from the LEADTOOLS tab and position it as you would like it to appear in your application.

10.

Change the following properties of the LEAD Main Control as follows:

 

Property

Value

DataSource

DataSource1

DataField

BMP

 

 

You must assign the DataSource before the DataField.

 

11.

From the LEADTOOLS tab, add a LEADDlgFile control and a LEADDlgService control to the form.

12.

Add a Button control to the form and change its name to btnFlip.

13.

Add the following code to the FormCreate procedure. Do not delete code that already exists in the procedure.

   LEADImage1.BorderStyle := bsSingle;
   LEADImage1.BackColor := RGB(255, 255, 125);
   LEADImage1.AutoRepaint := True;
   LEADImage1.AutoSize := False;
   LEADImage1.AutoSetRects := True;
   LEADImage1.PaintSizeMode := smFit;
   LEADImage1.DataLoadBits := 0;
   LEADDlgService1.InitDialogs(0);

14.

Handle the DBNavigator OnClick event, and code the DBNavigatorClick procedure as follows:

procedure TForm1.DBNavigatorClick(Sender: TObject; Button: TNavigateBtn);
begin
   if (Button <> nbInsert) then { do nothing }
      Exit;
   LEADDlgFile1.DlgFlags := 0;
   LEADDlgFile1.DialogTitle := 'Add an image';
   LEADDlgFile1.PreviewEnabled := True;
   LEADDlgFile1.Filter := 'All |*.*|CMP|*.cmp|JPEG|*.jpg';
   LEADDlgFile1.FilterIndex := 2; {look for *.cmp first}
   LEADDlgFile1.InitialDir := 'C:\Program Files\LEAD Technologies, Inc\LEADTOOLS14\Images\';
   LEADDlgFile1.LoadPasses := 0;
   LEADDlgFile1.LoadRotated := True;
   LEADDlgFile1.LoadCompressed := False;
   LEADDlgFile1.DlgFlags :=  
      DLG_OPEN_SHOW_PROGRESSIVE   +   
      DLG_OPEN_SHOW_MULTIPAGE   +   
      DLG_OPEN_SHOW_LOADROTATED   +   
      DLG_OPEN_SHOW_LOADCOMPRESSED   +   
      DLG_OPEN_SHOW_FILEINFO   +   
      DLG_OPEN_SHOW_PREVIEW   +   
      DLG_OPEN_SHOW_DELPAGE   +   
      DLG_OPEN_VIEWTOTALPAGES   +   
      DLG_OPEN_LOADBITMAP;
   LEADDlgFile1.LEADImage := LEADImage1; {assign the main control for processing}
   LEADDlgFile1.EnableMethodErrors := False;
   { when the user does not load and image, abort the process }
   If (LEADDlgFile1.ShowOpenDlg(Self) <> SUCCESS_DLG_OK) then
   begin
      Table1.Cancel();
      Exit;
   end;
   { Enable image processing before posting to the database }
   btnFlip.Enabled := True;
   { Specify the appropriate properties for saving the image in the database }
   case (LEADImage1.BitmapBits) of
     1: 
         begin
           LEADImage1.DataSaveBits := 1;
           LEADImage1.DataSaveFormat := FILE_LEAD1BIT;
        end;
     4: 
         begin
           LEADImage1.DataSaveBits := 4;
           LEADImage1.DataSaveFormat := FILE_PCX;
        end;
   else 
   begin
           if (LEADImage1.IsGrayScale = GRAY_NO) then { save as color }
              LEADImage1.DataSaveBits := 24
           else {save as grayscale}
              LEADImage1.DataSaveBits := 8;
           LEADImage1.DataSaveFormat := FILE_CMP;
           LEADImage1.DataSaveQuality := QMS;
   end;
   end; {of case statement }
   EditNAME.SetFocus();
end;

15.

Handle the btnFlip OnClick event, and code the btnFlipClick procedure as follows:

procedure TForm1.btnFlipClick(Sender: TObject);
begin
   LEADImage1.Flip();
end;

 

16.

Handle the LEADImage1 OnChange event, and code the LEADImage1Change procedure as follows:

procedure TForm1.LEADImage1Change(Sender: TObject);
begin
   { Avoid processing events that occur before the bitmap is fully loaded }
   if (LEADImage1.Bitmap = 0) then
      Exit;
   { Demonstrate how the DataChanged property works }
   if (LEADImage1.DataChanged And (DataSource1.State <> dsInsert)) then
      ShowMessage('You flipped the image');
end;

17.

Handle the DataSource1 OnStateChange event, and code the DataSource1StateChange procedure as follows:

procedure TForm1.DataSource1StateChange (Sender: TObject);
begin
      btnFlip.Enabled := (DataSource1.State = dsEdit);
end;

18.

Add LEADDef to the uses section of Unit1.

19.

Run your program to test it.