Solved

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

Posted on 2006-06-14
23
411 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: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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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