Reputation: 71
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 :)
Upvotes: 6
Views: 54922
Reputation: 643
To open a graphical file so that the user can select the file himself, the TImage
, TOpenPictureDialog
, and TButton
components must be placed on the form.
Place the following code in the button's click handler:
If OpenPictureDialog1.Execute then
Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
To open the jpeg and png files at the top of the code, in the line uses we need to add the name of the two libraries, JPEG
, PNGImage
.
Upvotes: -1
Reputation: 196
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.
Upvotes: 1
Reputation: 108929
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;
Upvotes: 15