[英]Loading Picture into image Delphi
您好我目前正在開發一個程序,我想添加一個按鈕,允許用戶將圖片從他的計算機加載到圖像中
procedure TForm1.btnLoadPicClick(Sender: TObject); begin img1.Picture.LoadFromFile( 'test.1'); img1.Stretch:= True;
我正在使用此代碼,但它限制了該人只能使用該特定圖片,我希望他從他的計算機中獲得 select,謝謝:)
您需要顯示一個打開的對話框:
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。 然后在 btnLoadPic 的點擊事件上將代碼作為
過程 TForm1.btnLoadPicClick(Sender: TObject);
開始
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');
結尾;
如果您只想要 JPEG 圖像,請取消注釋注釋行。 在 object 檢查器中,您可以將 Timage 屬性 Stretch 設置為 True。
要打開圖形文件以便用戶可以自己打開文件,必須將TImage
、 TOpenPictureDialog
和TButton
組件放置在窗體上。
將以下代碼放在按鈕的單擊處理程序中:
If OpenPictureDialog1.Execute then Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
要打開代碼頂部的 jpeg 和 png 文件,在 uses 行中我們需要添加兩個庫的名稱JPEG
、 PNGImage
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.