您最好将 TImage 组件放在辅助表单中并传递文件名,例如,为您的表单创建一个新的构造函数,如下所示:
type
TForm2 = class(TForm)
Image1: TImage;
private
public
constructor CreateWithImage(AOwner: TComponent; AImgPath: string);
end;
...
implementation
...
constructor TForm2.CreateWithImage(AOwner: TComponent; AImgPath: string);
begin
Create(AOwner);
Image1.Picture.LoadFromFile(AImgPath);
end;
然后,您可以像这样创建并显示您的表单:
procedure TForm1.Button1Click(Sender: TObject);
var
Form2: TForm2;
begin
if OpenPictureDialog1.Execute then
begin
Form2 := TForm2.CreateWithImage(Self, OpenPictureDialog1.FileName);
Form2.Show;
end;
end;
编辑
如果你想在运行时创建图像,你可以这样做:
type
TForm2 = class(TForm)
private
FImage: TImage; //this is now in the private section,
//and not the default (published)
public
constructor CreateWithImage(AOwner: TComponent; AImgPath: string);
end;
...
implementation
...
constructor TForm2.CreateWithImage(AOwner: TComponent; AImgPath: string);
begin
Create(AOwner);
FImage := TImage.Create(Self);
FImage.Parent := Self;
FImage.Align := alClient;
FImage.Picture.LoadFromFile(AImgPath);
end;