Loading image into Delphi image

Hello, I am currently working on a program, and I would like to add a button that will allow the user to download the image from his computer to the image

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

I used this code, but it limits the person only to the ability to use this particular picture, and I would like him to choose one of his computers :)

+4
source share
2 answers

You need to display an open dialog box:

 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; 
+12
source

First place Timage and OpenPictureDialog in your form, and then in the uses add jpeg section. Then in the btnLoadPic click event, enter 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 a JPEG image, then uncomment the commented lines. In the object inspector, you can set the Timage Stretch property to True.

+1
source

All Articles