Images & Pictures in Delphi (TImage & TDBImage)

In Delphi loading, saving and deleting images in application is very easy. Delphi provides lots of classes and members to manage images in application. We can use useful component like TImage and TDBImage to load and save an Images. TImage is used to save or load an Image to or from a file. And TDBImage is used to save or load an Image to or from a database. Lets check in detail.

Timage

TImage control to display an image from files that ends in extensions including ICO, BMP, WMF, WMF, GIF and JPG. The Picture property specifies the image that appears in the TImage control. Delphi supports several different methods for assigning an image for the TImage component: a TPicture's method LoadFromFile reads graphics from disk or the Assign method obtains the image from Clipboard.


Properties
Center
Sets image in center
Stretch
Shows image in stretch mode
Transparet
Sets Transparent background
Proportional
Indicates weather the image should be changed, without distortion.
Picture
To load, save picture to Timage control

Events
onClick
When user clicks on image
onProgress
When image is about to process or clear. This can be used to show percentages in case of slow process.
                         
Methods
Picture.LoadFromFile
Loads an image from file
Picture.SaveToFile
Saves an image to a file



Lets check with an example

First I put a Timage control on form to load and save images. Here I have also used OpenPicturedialog and SavePicturedialog component to show open and save dialog to select files.

Load image from file to Timage

procedure TForm1.btnLoadImgClick(Sender: TObject);
begin
  if OpenPictureDialog1.Execute then
  begin
    if OpenPictureDialog1.FileName<>'' then
    begin
      Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
    end;
  end;
end;

Clear image from Timage

procedure TForm1.btnDelImgClick(Sender: TObject);
begin
  Image1.Picture := Nil;
  //Or//
  Image1.Picture.Assign(Nil);
end;

Save image from Timage to a file

procedure TForm1.btnSaveImgClick(Sender: TObject);
begin
  if SavePictureDialog1.Execute then
  begin
    if SavePictureDialog1.FileName<>'' then
    begin
      Image1.Picture.SaveToFile(SavePictureDialog1.FileName);
    end;
  end;
end;


TDBimage
TDBImage control to display an image that stored in a database. By using TDBImage we can save and retrieve an Image to and from database. The Datasource and DataField property specifies the database field that stores the image and which will display in TDBImage control.


Properties
Center
Sets image in center
Stretch
Shows image in stretch mode
Transparent
Sets Transparent background
Proportional
Indicates weather the image should be changed, without distortion.
DataSource
Sets datasource link
DataField
Actual database field that stores image data

Events
onClick
When user clicks on image
onDblClick
When user double clicks

Methods
Picture.LoadFromFile
Loads an image from file
Picture.SaveToFile
Saves an image to a file




Lets check with an example

First I put a TDBImage control on form and set datasource, datafield properties. Then I used OpenPicturedialog and SavePicturedialog component to show open and save dialog to select files.

Load image from file to AdoTable image field and then store in DB.

procedure TForm1.btnLoadImgClick(Sender: TObject);
begin
  if OpenPictureDialog1.Execute then
  begin
    if OpenPictureDialog1.FileName<>'' then
    begin
         ADOTable1.Edit;         
         TBlobField(ADOTable1.FieldByName('IMAGE1')).
                                     LoadFromFile(OpenPictureDialog1.FileName);
         ADOTable1.Post;  //will store the image in DB field//
    end;
  end;
end;

Clear image from  AdoTable image field.

procedure TForm1.btnDelImgClick(Sender: TObject);
begin
    ADOTable1.Edit;
    TBlobField(ADOTable1.FieldByName('IMAGE1')).Clear;
//or//
    TBlobField(ADOTable1.FieldByName('IMAGE1')).Assign(Nil);
    ADOTable1.Post;  //will clear the image from DB field//
end;

Save image from AdoTable image field to a file

procedure TForm1.btnSaveImgClick(Sender: TObject);
begin
  if SavePictureDialog1.Execute then
  begin
    if SavePictureDialog1.FileName<>'' then
    begin
       TBlobField(ADOTable1.FieldByName('IMAGE1')).
                                      SaveToFile(SavePictureDialog1.FileName);
    end;
  end;
end;





















Comments

  1. Nice to see people helping.
    However, for a novic programmer such as myself none of this helps until I see a working compileable Project on my computer.

    Do you Have something for D-11 I could try ?

    ReplyDelete

Post a Comment

Popular posts from this blog

ShellExecute in Delphi

MS Excel Automation in Delphi

Drawing Shapes in Delphi