如何:設定 Windows Forms 控制項所顯示的影像

數個 Windows Forms 控制項可以顯示影像。 這些影像可以是厘清控制項用途的圖示,例如表示儲存命令之按鈕上的磁片圖示。 或者,圖示可以是背景影像,以提供控制項您想要的外觀和行為。

程式設計

將 控制項的 ImageBackgroundImage 屬性設定為 類型的 Image 物件。 一般而言,您將使用 FromFile 方法從檔案載入映射。

在下列程式碼範例中,為影像位置設定的路徑是 [ 我的圖片] 資料夾。 執行 Windows 作業系統的大部分電腦都包含此目錄。 這也可讓使用者以最少的系統存取層級安全地執行應用程式。 下列程式碼範例會要求您已經有已新增控制項的 PictureBox 表單。

' Replace the image named below with your own icon.
PictureBox1.Image = Image.FromFile _
   (System.Environment.GetFolderPath _
   (System.Environment.SpecialFolder.MyPictures) _
   & "\Image.gif")
// Replace the image named below with your own icon.
// Note the escape character used (@) when specifying the path.
pictureBox1.Image = Image.FromFile
   (System.Environment.GetFolderPath
   (System.Environment.SpecialFolder.MyPictures)
   + @"\Image.gif");
// Replace the image named below with your own icon.
pictureBox1->Image = Image::FromFile(String::Concat
   (System::Environment::GetFolderPath
   (System::Environment::SpecialFolder::MyPictures),
   "\\Image.gif"));

設計師

  1. 在 Visual Studio 的 [ 屬性 ] 視窗中,選取 控制項的 Image BackgroundImage 屬性,然後選取省略號 ( Ellipsis button in Visual Studio ) 以顯示 [ 選取資源 ] 對話方塊。

  2. 選取您想要顯示的影像。

另請參閱