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...
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...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if you want, send me email at nasman@mogaza.org to send u the project if u like
well, mnasman did it
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(CreateBlobStre am(FieldBy Name('pict ure'), 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(CreateBlobStre am(FieldBy Name('pict ure'), 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(CreateBlobStre am(FieldBy Name('pict ure'), bmRead));
j := TJPEGImage.Create;
j.LoadFromStream(bs);
Image1.Picture.Assign(j);
bs.Free;
j.Free;
end;
Hope this helps...
Epsylon.
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
bs := TBlobStream(CreateBlobStre
bs.CopyFrom(fs, 0);
bs.Free;
fs.Free;
Post;
end;
Reading from database:
with Table1 do
begin
fs := TFileStream.Create('image1
bs := TBlobStream(CreateBlobStre
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(CreateBlobStre
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(Send er: 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(ope ndialog1.f ilename);
JPegImage.SaveToStream(MyJ PegStream) ;
// Prepare Blob
MyBlobStream := TBlobStream.Create(TBlobFi eld(Table1 .FieldByNa me('Bild2' )),bmReadW rite);
MyBlobStream.CopyFrom(MyJP egStream,0 ); // Copy JPeg into BlobField
Image1.Picture.Assign(JPeg Image); // Display
MyJPegStream.Free; //Free all
MyBlobStream.Free;
end;
end;
end else ShowMessage('Table is not in Edit Mode!');
end;
procedure TForm1.Table1AfterScroll(D ataSet: TDataSet);
Var
MyBlobStream : TBlobStream;
begin
// Do Only if a JPeg available
if (Table1.FieldByName('Bild2 ').IsBlob) and (not(Table1.FieldByName('B ild2').IsN ull)) then
begin
// Prepare Streams
MyBlobStream := TBlobStream.Create(TBlobFi eld(Table1 .FieldByNa me('Bild2' )),bmRead) ;
JPegImage.LoadFromStream(M yBlobStrea m);
Image1.Picture.Assign(JPeg Image); //Display JPeg
MyBlobStream.Free; // Free Stream
end
else image1.Picture := Nil; // No JPeg saved disable display
end;
end.
meikl ;-)))
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:
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:
begin
Table1.Close;
JPegImage.Free; // Free JPeg-Object
end;
procedure TForm1.LoadJPEG1Click(Send
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'
begin
MyJPegStream := TMemoryStream.Create; // Handle JPegFile
JPegImage.LoadFromFile(ope
JPegImage.SaveToStream(MyJ
// Prepare Blob
MyBlobStream := TBlobStream.Create(TBlobFi
MyBlobStream.CopyFrom(MyJP
Image1.Picture.Assign(JPeg
MyJPegStream.Free; //Free all
MyBlobStream.Free;
end;
end;
end else ShowMessage('Table is not in Edit Mode!');
end;
procedure TForm1.Table1AfterScroll(D
Var
MyBlobStream : TBlobStream;
begin
// Do Only if a JPeg available
if (Table1.FieldByName('Bild2
begin
// Prepare Streams
MyBlobStream := TBlobStream.Create(TBlobFi
JPegImage.LoadFromStream(M
Image1.Picture.Assign(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\m yImage.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\New Image.jpg' );
For load one file selected by user, can you write this code:
If Open.Execute then
begin
Table1.Edit;
Table1Field.LoadFromFile(O pen.FileNa me);
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
Table1.Edit;
Table1Field.LoadFromFile('
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:
For load one file selected by user, can you write this code:
If Open.Execute then
begin
Table1.Edit;
Table1Field.LoadFromFile(O
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 ;-)
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 ;-)
ASKER
sorry ondertol, because i will try first their program ok...
ASKER
i want in database paradox (*.db)....
and halo mnasman what is table1pic and table1id ??
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
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
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
ASKER
my email is: rudy_r@mailcity.com...
can u give me your source code...
can u give me your source code...
(i'm sure epsylon will give you a working sample,
he has delphi on hand, which i don't have yet)
meikl ;-)