Solved

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

Posted on 2006-06-14
23
408 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
loading dll from external path 1 210
add combobox item based on numbers 9 122
Intraweb download file link ? 1 108
TServerSocket - file via sendbuf or Text via sendtext ? 1 42
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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

912 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

24 Experts available now in Live!

Get 1:1 Help Now