窗体设计如图:
image button OpenPictureDialog 各一个
代码:
1 unit Unit1; 2 3 interface 4 5 uses 6 Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, 7 Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtDlgs, Vcl.StdCtrls, Vcl.ExtCtrls; 8 9 type10 TForm1 = class(TForm)11 Image1: TImage;12 Button1: TButton;13 OpenPictureDialog1: TOpenPictureDialog;14 procedure Button1Click(Sender: TObject);15 private16 { Private declarations }17 public18 { Public declarations }19 end;20 21 var22 Form1: TForm1;23 24 implementation25 26 { $R *.dfm}27 28 procedure TForm1.Button1Click(Sender: TObject);29 var30 openfilepath:string;31 begin32 if OpenPictureDialog1.Execute then33 begin34 openfilepath := OpenPictureDialog1.FileName;//把打开文件名字赋值给openfilepath35 Image1.Picture.LoadFromFile(openfilepath); //在image1打开图片36 end;37 38 end;39 40 end.