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 :)
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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With