Link to home
Start Free TrialLog in
Avatar of Rudy_Rai
Rudy_Rai

asked on

i want ask about database in delphi 5.0

hi...

i want make database about field (blob image), i want save my picture(*.jpg) in my field (BLOB Image)...
and update my picture in this field...


please help me...
Avatar of kretzschmar
kretzschmar
Flag of Germany image

for what database?

(i'm sure epsylon will give you a working sample,
he has delphi on hand, which i don't have yet)

meikl ;-)
ASKER CERTIFIED SOLUTION
Avatar of Mohammed Nasman
Mohammed Nasman
Flag of Palestine, State of 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
if you want, send me email at nasman@mogaza.org to send u the project if u like
well, mnasman did it
Avatar of Epsylon
Epsylon

My standard answer  :o)

You can store for example jpeg images in the database by using a blob field (Ole Object field in Access):


var fs: TFileStream;
bs: TBlobStream;


Writing to database:

with Table1 do
begin
Append;
fs := TFileStream.Create('image1.jpg', fmOpenRead);
bs := TBlobStream(CreateBlobStream(FieldByName('picture'), bmWrite));
bs.CopyFrom(fs, 0);
bs.Free;
fs.Free;
Post;
end;


Reading from database:

with Table1 do
begin
fs := TFileStream.Create('image1.jpg', fmOpenWrite or fmCreate);
bs := TBlobStream(CreateBlobStream(FieldByName('picture'), bmRead));
fs.CopyFrom(bs, 0);
bs.Free;
fs.Free;
end;


If you want to display JPEG images you can use a TJPEGImage and then assign it to a TImage:


var j: TJPEGImage;

with Table1 do
begin
bs := TBlobStream(CreateBlobStream(FieldByName('picture'), bmRead));
j := TJPEGImage.Create;
j.LoadFromStream(bs);
Image1.Picture.Assign(j);
bs.Free;
j.Free;
end;


Hope this helps...

Epsylon.
well, then i will post also my old version from my paq

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 in [dsEdit,dsInsert] then    // Do only if in edit or insert 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 ;-)))

You must Load your image from disk to Table using this istruction:

Table1.Edit;
Table1Field.LoadFromFile('c:\mydir\myImage.jpg');
Table1.Post;

for use this istruction you must insert your table on your form and Add your graphic Fields (double click into Table1 component into your form + left click on the new window + Add Fields).

Table1Field is the name assigned at your graphic field, the first part is your Table name, the second part is your Fields name (you can see it on object into your unit program, the type is TGraphicField). The string "myImage.jpg" is the name of your image (you can insert the directory)!

If you want update this image on your table you must reload the image (using the same istruction).

If you desire save the image on your table into file disk, write this:

Table1Field.SaveToFile('c:\mydir\NewImage.jpg');


For load one file selected by user, can you write this code:

If Open.Execute then
begin
  Table1.Edit;
  Table1Field.LoadFromFile(Open.FileName);
  Table1.Post;
end;

Open is one TOpenDialog component!!!
Modify the Open.Filter property to list only *.jpg file (Open.Filter := 'JPEG Format |*.jpg').

!!! remember: insert the jpeg unit on your program (uses jpeg) !!!!!

bye bye

bye bye


>>>sigh<<<
Epsylon,  :-/
ondertol,
thats not the right way, even as i guess that would not work correctly
and also if it works it needs persistent fields

all comments above your answer, which are containing code are verified working samples,

so please be so kind, and withdraw your answer, and let the questioner decide,
(keep this also in mind for the future, because it was not the first  time you did this!)

if not,
then

Rudy_Rai, please reject ondertol's answer and grade mnasman,
which gives you first a correct answer

meikl ;-)
Avatar of Rudy_Rai

ASKER

sorry ondertol, because i will try first their program ok...
i want in database paradox (*.db)....
and  halo mnasman what is table1pic and table1id ??
it's two fields in the table
i made talbe with two fields to test
one is id and the other is pic
id : string
pic : blob
the example i gave u it work fine with paradox table
i test it with oracle, access and paradox, and it work fine with them without any change in code
just change the table name
if you need a copy of this project, give me ur email

Mohammed
my email is: rudy_r@mailcity.com...

can u give me your source code...