tags:

views:

121

answers:

2

Hello I am currently working on a program and I would like to add a button that would allow the user to Load a picture from his computer into the Image

procedure TForm1.btnLoadPicClick(Sender: TObject);
 begin
 img1.Picture.LoadFromFile( 'test.1');
 img1.Stretch := True ;

I was using this code but it limits the person to only being able to use that specific picture and I would like him to select one from his Computer thanks :)

+5  A: 

You need to display an open dialog:

procedure TForm1.Button1Click(Sender: TObject);
begin
  with TOpenDialog.Create(self) do
    try
      Caption := 'Open Image';
      Options := [ofPathMustExist, ofFileMustExist];
      if Execute then
        Image1.Picture.LoadFromFile(FileName);
    finally
      Free;
    end;
end;
Andreas Rejbrand
Thanks alot :) worked nicely
Noobdelphi
+1 - despite the `with`.
Ulrich Gerhardt
For nitpicking, in a construct like `Create/try/.../finally/Free/end`, it's better to omit the owner (Self) and use `Create(nil)`. It would avoid sending a flurry of unnecessary notification messages throughout the application.
François
You could even use a `TOpenPictureDialog` so users can see a preview while choosing a file. That component still exists nowadays, doesn't it?
Rob Kennedy
@François: Sometimes a component needs an owner for other reasons than being destroyed, e.g. poOwnerFormCenter.
Ulrich Gerhardt
@Ulrich Gerhardt: Right, but I would say extremely rarely, and not even necessary with `poOwnerFormCenter` when the owner is the MainForm. And not at all in this case.
François
@Rob Kennedy: `TOpenPictureDialog` still there in D2010 at least...
François
A: 

hello its me again i have implemented that into my program and i would like to know how i can save it into a database using SQL i have made an access database and table i would like to be able to save it into a table and load it out thanks

Delphinoob
Welcome to StackOverflow. To have a chance to get an answer to *that* question, it's best to post it as **separate question**. And certainly not as an answer.
François
It's also best if you **use the same account**. Then you don't have to say it's "me again"; we'll just know because we can see your question history.
Rob Kennedy