Solved

How can i save/retrieve images to/from mySQL?

Posted on 2006-06-14
23
402 Views
Last Modified: 2010-04-05
How can i save/retrieve images to/from mySQL?

Thanks in advance.
0
Comment
Question by:ryan_sabarre
  • 11
  • 7
  • 3
  • +1
23 Comments
 

Author Comment

by:ryan_sabarre
ID: 16902105
Thanks in advance.
0
 
LVL 28

Expert Comment

by:ciuly
ID: 16902140
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
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16902274
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;
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16902284
and ...

TO LOAD
var
  ms : TStream;
begin
  ms := Table1.CreateBlobStream(Table1.FieldByName('Image'), bmRead);
  Image1.LoadFromStream(ms);
  ms.Free;
end;
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 16902983
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.
0
 

Author Comment

by:ryan_sabarre
ID: 16903107
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;
0
 

Author Comment

by:ryan_sabarre
ID: 16903138
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?
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16903166
Hi,

It should be
  Image1.Picture.Graphic.SaveToStream(ms); and
  Image1.Picture.Graphic.LoadFromStream(ms);
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 16903214
[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?
0
 

Author Comment

by:ryan_sabarre
ID: 16903265
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;
0
 

Author Comment

by:ryan_sabarre
ID: 16903302
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;
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 28

Expert Comment

by:ciuly
ID: 16903320
you need to ms.seek(0,sofromorigin); after creating the stream just to be on the safe side
0
 

Author Comment

by:ryan_sabarre
ID: 16903398
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;
0
 
LVL 28

Expert Comment

by:ciuly
ID: 16903413
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
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16903446
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;
0
 

Author Comment

by:ryan_sabarre
ID: 16903448
By the way, i used BLOB as my data type
0
 

Author Comment

by:ryan_sabarre
ID: 16903476
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;
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16903501
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;
0
 

Author Comment

by:ryan_sabarre
ID: 16903547
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;
0
 
LVL 10

Accepted Solution

by:
atul_parmar earned 150 total points
ID: 16903673
Oops!

procedure TfPatientRecordEntry.Button1Click(Sender: TObject);
var
  ms : TStream;
  gr : TBitmap;
begin
  ms := oTable.CreateBlobStream(oTable.FieldByName('PPICTURE'), bmRead);
  gr := TBitmap.Create;
  gr.LoadFromStream(ms);
  Image1.Picture.Bitmap.Assign(gr);
  gr.Free;
  ms.Free;
end;
0
 

Author Comment

by:ryan_sabarre
ID: 16903701
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...
0
 

Author Comment

by:ryan_sabarre
ID: 16903711
Thanks again.
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16903727
Great.

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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now