Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading Picture into image Delphi

Tags:

image

delphi

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 :)

like image 409
Noobdelphi Avatar asked Oct 05 '10 15:10

Noobdelphi


2 Answers

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;
like image 72
Andreas Rejbrand Avatar answered Sep 28 '22 00:09

Andreas Rejbrand


First place a Timage and an OpenPictureDialog on your form and then on your uses clause add jpeg. Then on click event of btnLoadPic put the code as

procedure TForm1.btnLoadPicClick(Sender: TObject);

Begin

    If not OpenPictureDialog1.Execute Then
       Exit;
    Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
    //If not (Image1.Picture.Graphic is TJPEGImage) Then

    //raise Exception.Create('File not JPEG image');

End;

If you want only the JPEG image then uncomment the commented lines. In the object inspector you can set the Timage property Stretch to True.

like image 20
Sushil Avatar answered Sep 28 '22 00:09

Sushil