简体   繁体   English

将图片加载到图像 Delphi

[英]Loading Picture into image Delphi

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:)我正在使用此代码,但它限制了该人只能使用该特定图片,我希望他从他的计算机中获得 select,谢谢:)

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.首先在您的表单上放置一个 Timage 和一个 OpenPictureDialog,然后在您的 uses 子句上添加 jpeg。 Then on click event of btnLoadPic put the code as然后在 btnLoadPic 的点击事件上将代码作为

procedure TForm1.btnLoadPicClick(Sender: TObject);过程 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.如果您只想要 JPEG 图像,请取消注释注释行。 In the object inspector you can set the Timage property Stretch to True.在 object 检查器中,您可以将 Timage 属性 Stretch 设置为 True。

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.要打开图形文件以便用户可以自己打开文件,必须将TImageTOpenPictureDialogTButton组件放置在窗体上。

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 .要打开代码顶部的 jpeg 和 png 文件,在 uses 行中我们需要添加两个库的名称JPEGPNGImage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM