Delphi Pages Forums

Delphi Pages Forums (http://www.delphipages.com/forum/index.php)
-   General (http://www.delphipages.com/forum/forumdisplay.php?f=2)
-   -   insert a image into a microsoft access database (http://www.delphipages.com/forum/showthread.php?t=208111)

Josh 02-23-2010 05:09 PM

insert a image into a microsoft access database
 
hi,

i am currently using delphi 2007, i am wanting to be able to add a image(prefrebly jpeg) into a access database. i have tried doing this but i cant as it does not insert correctly, has anyone made any code or anything similar to this to help me out?

this is the code i am currently using to add it to the database

begin
opendialog1.Execute;
with adotable1 do
begin
Edit;
(fieldbyName('Picture') as Tblobfield).loadfromFile(opendialog1.filename);
Post;
end;

this is the code i am using where i am wanting to search my files for an image and then i want it to be inserted into the database, however i get the error message saying my bitmap is not valid....

what do i do to solve this issue?

thanks in advance.

MrBaseball34 02-24-2010 01:21 PM

What is the Access FieldType for the Picture field?

Josh 02-24-2010 08:06 PM

i set it as an OLE image.

MrBaseball34 02-24-2010 09:16 PM

Have you seen this article?
http://delphi.about.com/od/database/l/aa030601a.htm

Josh 02-24-2010 09:47 PM

yes, ive just tried using that, but i could not get it to work as i dont have the rx library.
anyway i figured out how to add an image to a database, however it is saved as long binary data, how do i load this in delphi? i have no idea how to make this work.

MrBaseball34 02-25-2010 01:36 AM

Let me check out some other things.

delphimpd 02-25-2010 05:58 AM

Hi and hope it helps

I use this in MSSQL but it should also work in ACCESS

But when u copy and paste, Always remember to read and study the code and try to understand what it does, so u can lear.

Step One

2 Buttons (Open) (Save)
1 Open Dialog
1 Save Dialog
1 Image Holder


In the Uses Clause include "Jpeg"

Create this Procedure

Code:

Procedure SaveImagen(wimg:TImage;wfield:string);
var
 BlobStream: TStream;
 buf: tmemorystream;
begin
 try
  if wimg.Picture.Graphic <> nil then
  begin
      buf := tmemorystream.create();
    wimg.picture.graphic.savetostream(buf);
    buf.seek(0, soFromBeginning);
//      buf.SaveToFile('U:\x.jpg');
    dataM.QryPersonnel.edit; //Your Data Set this is Mine
    TblobField( dataM.QryPersonnel.Fieldbyname('Picture')).LoadFromStream(buf);
    dataM.QryPersonnel.post;
    buf.free;
  end;
 except
 end;
end;


Then on the Button Open

Code:

Var
FileName: TFileName;
  begin
    if OpenD.Execute then
    Begin
      FileName:=opend.filename;
      ProfilePicture.Picture.LoadFromFile(FileName);
    RestrictedImage.Picture.LoadFromFile(FileName);
end;


On the Save button

Code:

SaveImagen(ProfilePicture,'Picture');
  SaveImagen(RestrictedImage,'Picture');


Good Luck

Josh 02-25-2010 01:38 PM

thanks this code works for me :)
i just have one more question, once i have saved a jpeg to my database it saves it as long binary data, i want to me able to display this in a new form -

once a button is clicked, a new form will appear showing the image that is stored in that record. how do i display this long binary data?

delphimpd 02-25-2010 06:06 PM

The same Code, but pointing to the new form and to the image Image.

Just put the new form in the uses clause, you can dynamically create the form or have it created automatically for you. But remember make sure if you dynamically create it, you must free it when you close it, if not memory will build up.

Josh 02-25-2010 07:40 PM

ive tried this, but all i am getting is a jpeg #42 error.... what do i do to solve this?


All times are GMT. The time now is 01:43 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.