Solved

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

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

Thanks in advance.
0
Comment
Question by:ryan_sabarre
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 7
  • 3
  • +1
23 Comments
 

Author Comment

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

Expert Comment

by:2266180
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 28

Expert Comment

by:2266180
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:2266180
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

691 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