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...
Rudy_RaiAsked:
Who is Participating?
 
Mohammed NasmanConnect With a Mentor Software DeveloperCommented:
i'm working with one project, it work with blob filed in access and oracle


unit Unit1;

interface

uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
 DBCtrls, StdCtrls, Mask, Db, DBTables, ExtCtrls, Grids, DBGrids,Jpeg;

type
 TForm1 = class(TForm)
   Button1: TButton;
   Image1: TImage;
   Table1: TTable;
   DataSource1: TDataSource;
   Database1: TDatabase;
   Table1PIC: TBlobField;
   DBNavigator1: TDBNavigator;
   OpenDialog1: TOpenDialog;
   DBGrid1: TDBGrid;
   Table1ID: TStringField;
   Label1: TLabel;
   DBEdit1: TDBEdit;
   procedure Button1Click(Sender: TObject);
   procedure FormDestroy(Sender: TObject);
   procedure FormCreate(Sender: TObject);
   procedure DataSource1DataChange(Sender: TObject; Field: TField);
 private
   { Private declarations }
   FileName : String;
   procedure ReadPicFromDB;
 public
   { Public declarations }

 end;

var
 Form1: TForm1;

implementation

{$R *.DFM}
uses shellapi;
procedure TForm1.ReadPicFromDB;
var
 FileStream : TFileStream;
 BlobStream : TBlobStream;
begin
 Image1.Picture.Assign(nil);
 BlobStream := TBlobStream.Create(Table1Pic, bmRead);
 if BlobStream.Size =0 then
 begin
   BlobStream.Free;
   Exit;
 end;
 if FileName <> '' then
   DeleteFile(FileName);
 FileName := 'Img.jpg';
 FileStream := TFileStream.Create(FileName, fmCreate or fmOpenWrite);
 FileStream.CopyFrom(BlobStream,BlobStream.Size);
 FileStream.Free;
 BlobStream.Free;
 Image1.Picture.LoadFromFile(FileName);
end;// end procedure radpicfromdb

procedure TForm1.Button1Click(Sender: TObject);
var
 FileStream : TFileStream;
 BlobStream : TBlobStream;
begin
 Application.ProcessMessages;
 if OpenDialog1.Execute then
 begin
   Button1.Enabled := false;
   FileStream := TFileStream.Create(OpenDialog1.FileName, fmOpenRead);
   Table1.Edit;
   BlobStream := TBlobStream.Create(Table1Pic,bmReadWrite);
   BlobStream.Seek(0,soFromBeginning);
   BlobStream.Truncate;
   BlobStream.CopyFrom(FileStream,FileStream.Size);
   FileStream.Free;
   BlobStream.Free;
   Table1.Post;
   ReadPicFromDB;
   Button1.Enabled := True;
 end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
 if FileName <> '' then
   DeleteFile(FileName);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 OpenDialog1.Filter := 'Jpeg Files |*.jpg';
 OpenDialog1.Options := [ofHideReadOnly, ofNoChangeDir];
 Image1.Stretch := true;
end;

procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
begin
 if DataSource1.State = dsBrowse then
   ReadPicFromDb;
end;

end.
0
 
kretzschmarCommented:
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 ;-)
0
 
Mohammed NasmanSoftware DeveloperCommented:
if you want, send me email at nasman@mogaza.org to send u the project if u like
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
kretzschmarCommented:
well, mnasman did it
0
 
EpsylonCommented:
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.
0
 
kretzschmarCommented:
:-)))
0
 
kretzschmarCommented:
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 ;-)))

0
 
ondertolCommented:
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


0
 
EpsylonCommented:
>>>sigh<<<
0
 
Mohammed NasmanSoftware DeveloperCommented:
Epsylon,  :-/
0
 
kretzschmarCommented:
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 ;-)
0
 
Rudy_RaiAuthor Commented:
sorry ondertol, because i will try first their program ok...
0
 
Rudy_RaiAuthor Commented:
i want in database paradox (*.db)....
and  halo mnasman what is table1pic and table1id ??
0
 
Mohammed NasmanSoftware DeveloperCommented:
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
0
 
Mohammed NasmanSoftware DeveloperCommented:
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
0
 
Rudy_RaiAuthor Commented:
my email is: rudy_r@mailcity.com...

can u give me your source code...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.