Link to home
Start Free TrialLog in
Avatar of Goodangel Matope
Goodangel MatopeFlag for Zambia

asked on

Images in a database

I would like to a database to have a person's details and their photo. How do I link each photo to a field in the database. Someone talked of BLOB fileds and I have no idea how it is done. Please help!
Avatar of kretzschmar
kretzschmar
Flag of Germany image

what database?
Avatar of Goodangel Matope

ASKER

Paradox (I presume that comes default with Delphi 4)

To load a new Picture just use :

DbImage1.Picture.LoadFromFile ('C:\xxx.bmp');

Best Regards

Cesario
which image-formats u will use?
I would like to use jpegs, and tiffs if possible (tiff for image of identification card.)
well, from my paq

first the bad news,
you cannot use the graphic-fieldtyp for jpeg-images, also you can't use the
tdbimage-component for visualzing the image.

the soloution:

a sample

first in the paradox structure the BlobField is type Binary
and now follows a sample-unit,
which stores and get the JPeg

unit db_pict_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, DBTables, ExtCtrls, DBCtrls, StdCtrls, Mask, Menus, jpeg; // JPeg unit is used

type
  TForm1 = class(TForm)
    DBEdit1: TDBEdit;   // A other DBField
    DBNavigator1: TDBNavigator;
    Table1: TTable;
    DataSource1: TDataSource;
    PopupMenu1: TPopupMenu;     // a Popup linked to Image1
    LoadPicture1: TMenuItem;    // the MenuItem of the Popup
    OpenDialog1: TOpenDialog;   // OpenDialog
    Image1: TImage;             // Visual Output for the JPeg
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure LoadJPEG1Click(Sender: TObject);      // File Load and Store in DB
    procedure Table1AfterScroll(DataSet: TDataSet); // Get JPeg and Display in Image1
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  JPegImage : TJpegImage;  //A temporary JPeg-Object

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  JpegImage := TJPegImage.Create; // Create JPeg-Object
  Table1.Open;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Table1.Close;
  JPegImage.Free;  // Free JPeg-Object
end;

procedure TForm1.LoadJPEG1Click(Sender: TObject);
Var
  MyBlobStream : TBlobStream;      // Streams
  MyJPegStream : TMemoryStream;
begin
  if (Table1.State = dsEdit) or
     (Table1.State = dsInsert) then    // Do only if in edit mode
  begin
    If opendialog1.Execute then    // If file selected
    begin
      if Table1.FieldByName('Bild2').IsBlob then   // this line can deleted
      begin
        MyJPegStream := TMemoryStream.Create;      // Handle JPegFile
        JPegImage.LoadFromFile(opendialog1.filename);
        JPegImage.SaveToStream(MyJPegStream);
        // Prepare Blob
        MyBlobStream :=
TBlobStream.Create(TBlobField(Table1.FieldByName('Bild2')),bmReadWrite);
        MyBlobStream.CopyFrom(MyJPegStream,0);  // Copy JPeg into BlobField
        Image1.Picture.Assign(JPegImage);  // Display
        MyJPegStream.Free;  //Free all
        MyBlobStream.Free;
      end;
    end;
  end else ShowMessage('Table is not in Edit Mode!');
end;

procedure TForm1.Table1AfterScroll(DataSet: TDataSet);
Var
  MyBlobStream : TBlobStream;
begin
  // Do Only if a JPeg available
  if (Table1.FieldByName('Bild2').IsBlob) and (not(Table1.FieldByName('Bild2').IsNull)) then
  begin
    // Prepare Streams
    MyBlobStream :=
TBlobStream.Create(TBlobField(Table1.FieldByName('Bild2')),bmRead);
    JPegImage.LoadFromStream(MyBlobStream);
    Image1.Picture.Assign(JPegImage);  //Display JPeg
    MyBlobStream.Free;  // Free Stream
  end
  else image1.Picture := Nil;  // No JPeg saved disable display
end;

end.

meikl
just to remark,

its just a sample,
therefore i've no try..except, try..finally
blocks inserted, which should be done for daus (users, which are not carefull)

meikl
Does that mean I cannot save to the BLOB field? How will I access the same image again in future?
?,

you can access and replace/save the jpeg-image in the blob-field as described above, you can also get it back to a file (this method is not included in the sample above, but it isn't difficult)

meikl
Hi,

An Artikel:Using Jpegs with Paradox

http://www.delphi3000.com/article.asp?ID=1315

Best Regards Cesario
well cesario,
but this article is just not complete,
because if you use table.fieldname as the author mentioned
you will have a tfield, and then you must do an additional conversion to tblobfield to get access to the streaming capabilities.

but at least its just the same as my sample

meikl
sorry cesario,

didn't read the beginning of the article exactly,
where the author explains to create persistant fields.

well with persistant fields the code in the article will work also

meikl ;-)
any problems, goodangel?
goodangel, are you alive?
Hie Kretzschmar. I am alive and I am wondering if I can also use tiffs. I tried the stuff above and I have not managed to get it to work. I will accept your answer though. Will tiffs work in a paradox database?

hi goodangel,

glad to hear, that you're alive :-)

Tiff will work
in the main manner,
but you will need a tiff-object like the jpeg-object for the visualizing from a third partydevelopment, because tiff is not included with delphi.

take a look to
http://www.torry.ru/vcl/graphics/packs/tiffpack.zip

i've a sample-app for jpg, which i can send you, if you leave your email address here.

and points only, if you get it work

meikl ;-)
>in the main manner,
should be read:
in the same manner,

meikl :-)
Meikl

My email is goodangelma@ncr.co.zw

Please email the sample app. I will be most obliged.
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial