ryan_sabarre
asked on
How can i save/retrieve images to/from mySQL?
How can i save/retrieve images to/from mySQL?
Thanks in advance.
Thanks in advance.
you will need a blob type field (http://forums.mysql.com/read.php?49,79243,84633#msg-84633)
or, you can use a varchar field or any other text like field, bas64encode the image and save it like that.
The above solutions should work for any database server that knows blob (data) fields
or, you can use a varchar field or any other text like field, bas64encode the image and save it like that.
The above solutions should work for any database server that knows blob (data) fields
Yeah then....
TO SAVE
var
ms : TMemoryStream;
begin
ms := TMemoryStream.Create;
Image1.SaveToStream(ms);
ms.Position := 0;
Table1.Append;
TBlobField(Table1.FieldByN ame(Image' )).LoadFro mStream(ms );
Table1.Post;
end;
TO SAVE
var
ms : TMemoryStream;
begin
ms := TMemoryStream.Create;
Image1.SaveToStream(ms);
ms.Position := 0;
Table1.Append;
TBlobField(Table1.FieldByN
Table1.Post;
end;
and ...
TO LOAD
var
ms : TStream;
begin
ms := Table1.CreateBlobStream(Ta ble1.Field ByName('Im age'), bmRead);
Image1.LoadFromStream(ms);
ms.Free;
end;
TO LOAD
var
ms : TStream;
begin
ms := Table1.CreateBlobStream(Ta
Image1.LoadFromStream(ms);
ms.Free;
end;
To be perfectly honest with you, it is very inefficient to store image data in a mySQL table.
It is better to store the PATH to the image as a string in the table and then store the image
in a directory structure someplace else.
I also like ciuly's suggestion of saving a base64 encoded image but that means you also have
to decode it on-the-fly.
It is better to store the PATH to the image as a string in the table and then store the image
in a directory structure someplace else.
I also like ciuly's suggestion of saving a base64 encoded image but that means you also have
to decode it on-the-fly.
ASKER
HI i got an error and it say's "unknown identifyier"
TO SAVE
var
ms : TMemoryStream;
begin
ms := TMemoryStream.Create;
Image1.SaveToStream(ms); <------------------ UNKNOW IDENTIFIER
ms.Position := 0;
Table1.Append;
TBlobField(Table1.FieldByN ame(Image' )).LoadFro mStream(ms );
Table1.Post;
end;
TO SAVE
var
ms : TMemoryStream;
begin
ms := TMemoryStream.Create;
Image1.SaveToStream(ms); <------------------ UNKNOW IDENTIFIER
ms.Position := 0;
Table1.Append;
TBlobField(Table1.FieldByN
Table1.Post;
end;
ASKER
I'm using delphi 7.
EddieShipman,
what if the pict is save in other network PC, how can i access it considering his PC has a security? how about if the folder is not shared to everybody?
what makes an image not good to be stored in database?
EddieShipman,
what if the pict is save in other network PC, how can i access it considering his PC has a security? how about if the folder is not shared to everybody?
what makes an image not good to be stored in database?
Hi,
It should be
Image1.Picture.Graphic.Sav eToStream( ms); and
Image1.Picture.Graphic.Loa dFromStrea m(ms);
It should be
Image1.Picture.Graphic.Sav
Image1.Picture.Graphic.Loa
[quote]what makes an image not good to be stored in database?[/quote]
Because it is inherently less efficient to access them.
I was just giving my opinion, if you don't want to take it, that's ok. Now ciuly's suggestion
to base64 encode adds another step to the process but it is much more efficient than storing
the image itself because string data takes up less space in a DB than binary data.
What mySQL access controls are you using?
Because it is inherently less efficient to access them.
I was just giving my opinion, if you don't want to take it, that's ok. Now ciuly's suggestion
to base64 encode adds another step to the process but it is much more efficient than storing
the image itself because string data takes up less space in a DB than binary data.
What mySQL access controls are you using?
ASKER
It does not work. No graphics display
Image1.Picture.Graphic.Sav eToStream( ms); and
Image1.Picture.Graphic.Loa dFromStrea m(ms);
here's my code
procedure TfPatientRecordEntry.Speed Button2Cli ck(Sender: TObject);
var
ms : TMemoryStream;
begin
if OpenPictureDialog1.Execute then
Begin
ms := TMemoryStream.Create;
//Image1.Picture.LoadFromF ile(OpenPi ctureDialo g1.filenam e);
Image1.Picture.Bitmap.Save ToStream(m s);
ms.Position := 0;
TBlobField(oTable.FieldByN ame('PPICT URE')).Loa dFromStrea m(ms);
End;
end;
procedure TfPatientRecordEntry.Butto n1Click(Se nder: TObject);
var
ms : TStream;
begin
ms := oTable.CreateBlobStream(oT able.Field ByName('PP ICTURE'), bmRead);
Image1.Picture.Bitmap.Load FromStream (ms);
ms.Free;
end;
Image1.Picture.Graphic.Sav
Image1.Picture.Graphic.Loa
here's my code
procedure TfPatientRecordEntry.Speed
var
ms : TMemoryStream;
begin
if OpenPictureDialog1.Execute
Begin
ms := TMemoryStream.Create;
//Image1.Picture.LoadFromF
Image1.Picture.Bitmap.Save
ms.Position := 0;
TBlobField(oTable.FieldByN
End;
end;
procedure TfPatientRecordEntry.Butto
var
ms : TStream;
begin
ms := oTable.CreateBlobStream(oT
Image1.Picture.Bitmap.Load
ms.Free;
end;
ASKER
I got a fatal error when i change to Image1.picture.graphics.sa vetostream
procedure TfPatientRecordEntry.Speed Button2Cli ck(Sender: TObject);
var
ms : TMemoryStream;
begin
if OpenPictureDialog1.Execute then
Begin
ms := TMemoryStream.Create;
//Image1.Picture.LoadFromF ile(OpenPi ctureDialo g1.filenam e);
Image1.Picture.Graphic.Sav eToStream( ms);
ms.Position := 0;
TBlobField(oTable.FieldByN ame('PPICT URE')).Loa dFromStrea m(ms);
End;
end;
procedure TfPatientRecordEntry.Butto n1Click(Se nder: TObject);
var
ms : TStream;
begin
ms := oTable.CreateBlobStream(oT able.Field ByName('PP ICTURE'), bmRead);
Image1.Picture.Graphic.Loa dFromStrea m(ms);
ms.Free;
end;
procedure TfPatientRecordEntry.Speed
var
ms : TMemoryStream;
begin
if OpenPictureDialog1.Execute
Begin
ms := TMemoryStream.Create;
//Image1.Picture.LoadFromF
Image1.Picture.Graphic.Sav
ms.Position := 0;
TBlobField(oTable.FieldByN
End;
end;
procedure TfPatientRecordEntry.Butto
var
ms : TStream;
begin
ms := oTable.CreateBlobStream(oT
Image1.Picture.Graphic.Loa
ms.Free;
end;
you need to ms.seek(0,sofromorigin); after creating the stream just to be on the safe side
ASKER
The error now is in here
procedure TfPatientRecordEntry.Butto n1Click(Se nder: TObject);
var
ms : TStream;
begin
ms := oTable.CreateBlobStream(oT able.Field ByName('PP ICTURE'), bmRead);
Image1.Picture.Graphic.Loa dFromStrea m(ms); <------- error
ms.Free;
end;
procedure TfPatientRecordEntry.Butto
var
ms : TStream;
begin
ms := oTable.CreateBlobStream(oT
Image1.Picture.Graphic.Loa
ms.Free;
end;
sorry for being ambigous, but that was the only place where the stream was created and not positioned :) so
ms := oTable.CreateBlobStream(oT able.Field ByName('PP ICTURE'), bmRead);
ms.seek(0,sofromorigin); OR ms.position:+0; as you did on the other place
ms := oTable.CreateBlobStream(oT
ms.seek(0,sofromorigin); OR ms.position:+0; as you did on the other place
procedure TfPatientRecordEntry.Butto n1Click(Se nder: TObject);
var
ms : TStream;
gr : TGraphic;
begin
ms := oTable.CreateBlobStream(oT able.Field ByName('PP ICTURE'), bmRead);
gr := TGraphic.Create;
gr.LoadFromStream(ms);
Image1.Picture.Graphic.Ass ign(gr);
gr.Free;
ms.Free;
end;
var
ms : TStream;
gr : TGraphic;
begin
ms := oTable.CreateBlobStream(oT
gr := TGraphic.Create;
gr.LoadFromStream(ms);
Image1.Picture.Graphic.Ass
gr.Free;
ms.Free;
end;
ASKER
By the way, i used BLOB as my data type
ASKER
Still create an error
procedure TfPatientRecordEntry.Butto n1Click(Se nder: TObject);
var
ms : TStream;
begin
ms := oTable.CreateBlobStream(oT able.Field ByName('PP ICTURE'), bmRead);
ms.position:=0;
Image1.Picture.Graphic.Loa dFromStrea m(ms); <----- ERRRO
ms.Free;
end;
procedure TfPatientRecordEntry.Butto
var
ms : TStream;
begin
ms := oTable.CreateBlobStream(oT
ms.position:=0;
Image1.Picture.Graphic.Loa
ms.Free;
end;
Yeah it would give an error
your Button1Click should look like the following
procedure TfPatientRecordEntry.Butto n1Click(Se nder: TObject);
var
ms : TStream;
gr : TGraphic;
begin
ms := oTable.CreateBlobStream(oT able.Field ByName('PP ICTURE'), bmRead);
gr := TGraphic.Create;
gr.LoadFromStream(ms);
Image1.Picture.Graphic.Ass ign(gr);
gr.Free;
ms.Free;
end;
your Button1Click should look like the following
procedure TfPatientRecordEntry.Butto
var
ms : TStream;
gr : TGraphic;
begin
ms := oTable.CreateBlobStream(oT
gr := TGraphic.Create;
gr.LoadFromStream(ms);
Image1.Picture.Graphic.Ass
gr.Free;
ms.Free;
end;
ASKER
Hi atul_parmar, thanks for the code but it returned a "ABSTRACT ERROR"
procedure TfPatientRecordEntry.Butto n1Click(Se nder: TObject);
var
ms : TStream;
gr : TGraphic;
begin
ms := oTable.CreateBlobStream(oT able.Field ByName('PP ICTURE'), bmRead);
gr := TGraphic.Create;
gr.LoadFromStream(ms);
Image1.Picture.Graphic.Ass ign(gr);
gr.Free;
ms.Free;
end;
procedure TfPatientRecordEntry.Butto
var
ms : TStream;
gr : TGraphic;
begin
ms := oTable.CreateBlobStream(oT
gr := TGraphic.Create;
gr.LoadFromStream(ms);
Image1.Picture.Graphic.Ass
gr.Free;
ms.Free;
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
atul_parmar,
Your Oops! really helped now! :-) Thank you VERY MUCH! I'm very much satisfied with your answer.
THANKS FOR THE HELP TOO. I'll increase the points...
Your Oops! really helped now! :-) Thank you VERY MUCH! I'm very much satisfied with your answer.
THANKS FOR THE HELP TOO. I'll increase the points...
ASKER
Thanks again.
Great.
Infect, I should do it at first place. :)
Infect, I should do it at first place. :)
ASKER