Link to home
Start Free TrialLog in
Avatar of ryan_sabarre
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.
Avatar of ryan_sabarre
ryan_sabarre

ASKER

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
Avatar of atul_parmar
Yeah then....

TO SAVE
var
  ms : TMemoryStream;
begin
  ms := TMemoryStream.Create;
  Image1.SaveToStream(ms);
  ms.Position := 0;
  Table1.Append;
  TBlobField(Table1.FieldByName(Image')).LoadFromStream(ms);
  Table1.Post;
end;
and ...

TO LOAD
var
  ms : TStream;
begin
  ms := Table1.CreateBlobStream(Table1.FieldByName('Image'), bmRead);
  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.
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.FieldByName(Image')).LoadFromStream(ms);
  Table1.Post;
end;
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?
Hi,

It should be
  Image1.Picture.Graphic.SaveToStream(ms); and
  Image1.Picture.Graphic.LoadFromStream(ms);
[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?
It does not work. No graphics display

 Image1.Picture.Graphic.SaveToStream(ms); and
  Image1.Picture.Graphic.LoadFromStream(ms);

here's my code

procedure TfPatientRecordEntry.SpeedButton2Click(Sender: TObject);
var
  ms : TMemoryStream;
begin
 if OpenPictureDialog1.Execute then
 Begin
  ms := TMemoryStream.Create;
  //Image1.Picture.LoadFromFile(OpenPictureDialog1.filename);
  Image1.Picture.Bitmap.SaveToStream(ms);
  ms.Position := 0;
  TBlobField(oTable.FieldByName('PPICTURE')).LoadFromStream(ms);
 End;
end;

procedure TfPatientRecordEntry.Button1Click(Sender: TObject);
var
  ms : TStream;
begin
  ms := oTable.CreateBlobStream(oTable.FieldByName('PPICTURE'), bmRead);
  Image1.Picture.Bitmap.LoadFromStream(ms);
  ms.Free;
end;
I got a fatal error when i change to Image1.picture.graphics.savetostream

procedure TfPatientRecordEntry.SpeedButton2Click(Sender: TObject);
var
  ms : TMemoryStream;
begin
 if OpenPictureDialog1.Execute then
 Begin
  ms := TMemoryStream.Create;
  //Image1.Picture.LoadFromFile(OpenPictureDialog1.filename);
  Image1.Picture.Graphic.SaveToStream(ms);
  ms.Position := 0;
  TBlobField(oTable.FieldByName('PPICTURE')).LoadFromStream(ms);
 End;
end;

procedure TfPatientRecordEntry.Button1Click(Sender: TObject);
var
  ms : TStream;
begin
  ms := oTable.CreateBlobStream(oTable.FieldByName('PPICTURE'), bmRead);
  Image1.Picture.Graphic.LoadFromStream(ms);
  ms.Free;
end;
you need to ms.seek(0,sofromorigin); after creating the stream just to be on the safe side
The error now is in here

procedure TfPatientRecordEntry.Button1Click(Sender: TObject);
var
  ms : TStream;
begin
  ms := oTable.CreateBlobStream(oTable.FieldByName('PPICTURE'), bmRead);
  Image1.Picture.Graphic.LoadFromStream(ms); <------- error
  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(oTable.FieldByName('PPICTURE'), bmRead);
 ms.seek(0,sofromorigin); OR ms.position:+0; as you did on the other place
procedure TfPatientRecordEntry.Button1Click(Sender: TObject);
var
  ms : TStream;
  gr : TGraphic;
begin
  ms := oTable.CreateBlobStream(oTable.FieldByName('PPICTURE'), bmRead);
  gr := TGraphic.Create;
  gr.LoadFromStream(ms);
  Image1.Picture.Graphic.Assign(gr);
  gr.Free;
  ms.Free;
end;
By the way, i used BLOB as my data type
Still create an error

procedure TfPatientRecordEntry.Button1Click(Sender: TObject);
var
  ms : TStream;
begin
 ms := oTable.CreateBlobStream(oTable.FieldByName('PPICTURE'), bmRead);
 ms.position:=0;
 Image1.Picture.Graphic.LoadFromStream(ms); <----- ERRRO
 ms.Free;
end;
Yeah it would give an error

your Button1Click should look like the following
procedure TfPatientRecordEntry.Button1Click(Sender: TObject);
var
  ms : TStream;
  gr : TGraphic;
begin
  ms := oTable.CreateBlobStream(oTable.FieldByName('PPICTURE'), bmRead);
  gr := TGraphic.Create;
  gr.LoadFromStream(ms);
  Image1.Picture.Graphic.Assign(gr);
  gr.Free;
  ms.Free;
end;
Hi atul_parmar, thanks for the code but it returned a "ABSTRACT ERROR"

procedure TfPatientRecordEntry.Button1Click(Sender: TObject);
var
  ms : TStream;
  gr : TGraphic;
begin
  ms := oTable.CreateBlobStream(oTable.FieldByName('PPICTURE'), bmRead);
  gr := TGraphic.Create;
  gr.LoadFromStream(ms);
  Image1.Picture.Graphic.Assign(gr);
  gr.Free;
  ms.Free;
end;
ASKER CERTIFIED SOLUTION
Avatar of atul_parmar
atul_parmar
Flag of India 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
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...
Thanks again.
Great.

Infect, I should do it at first place. :)