Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将图片加载到图像Delphi中_Delphi_Image - Fatal编程技术网

将图片加载到图像Delphi中

将图片加载到图像Delphi中,delphi,image,Delphi,Image,您好,我目前正在开发一个程序,我想添加一个按钮,允许用户将图片从计算机加载到图像中 procedure TForm1.btnLoadPicClick(Sender: TObject); begin img1.Picture.LoadFromFile( 'test.1'); img1.Stretch := True ; 我正在使用此代码,但它限制了用户只能使用该特定图片,我希望他从计算机中选择一张图片,谢谢:)您需要显示一个打开的对话框: procedure TForm1.Button1C

您好,我目前正在开发一个程序,我想添加一个按钮,允许用户将图片从计算机加载到图像中

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

我正在使用此代码,但它限制了用户只能使用该特定图片,我希望他从计算机中选择一张图片,谢谢:)

您需要显示一个打开的对话框:

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;

首先在表单上放置Timage和OpenPictureDialog,然后在uses子句上添加jpeg。然后单击BTNLOADIC的事件,将代码设置为

程序TForm1.BTNLOADICCLICK(发送方:TObject)

开始

结束


如果只需要JPEG图像,请取消注释注释注释行。在对象检查器中,您可以将Timage属性Stretch设置为True。

对于挑剔,在类似
Create/try/../finally/Free/end
的构造中,最好省略所有者(Self)并使用
Create(nil)
。它可以避免在整个应用程序中发送大量不必要的通知消息。您甚至可以使用
TOpenPictureDialog
,以便用户在选择文件时可以看到预览。该组件现在仍然存在,不是吗?@François:有时组件需要所有者,而不是被销毁,例如poOwnerFormCenter。@Ulrich Gerhardt:是的,但我会说非常罕见,当所有者是主表单时,甚至没有必要使用
poOwnerFormCenter
。在这种情况下一点也不。@LionsFan:我刚刚在表单上删除了一个
TImage
控件。
    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');