訓練
如何:於執行階段修改圖片的大小或位置 (Windows Form)
如果您在表單上使用 Windows Forms PictureBox 控制項,則可以將表單上的 SizeMode 屬性設為:
將圖片的左上角與控制項左上角對齊
將圖片置於控制項中心
調整控制項的大小,以符合其所顯示的圖片
延展其顯示的任何圖片,以配合控制項
延展圖片 (尤其是點陣圖格式的圖片) 可能會導致影像品質下降。 中繼檔,這是在執行階段繪製影像的圖形指示清單,比點陣圖更適合延展。
將 SizeMode 設為 Normal (預設值)、AutoSize、CenterImage 或 StretchImage。 Normal 表示將影像放在控制項的左上角;如果影像大於控制項,則會裁剪其下邊緣和右邊緣。 CenterImage 表示將影像放置於控制項中心;如果影像大於控制項,則會裁剪圖片的外緣。 AutoSize 表示會將控制項的大小調整為影像的大小。 StretchImage 是反向的,表示會將影像的大小調整為控制項的大小。
在下列範例中,針對影像位置設定的路徑是 [我的文件] 資料夾。 這是因為您可以假設大部分執行 Windows 作業系統的電腦都會包含這個目錄。 也可讓具備最小系統存取層級的使用者安全地執行應用程式。 下列範例會假設表單中已新增 PictureBox 控制項。
Private Sub StretchPic() ' Stretch the picture to fit the control. PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage ' Load the picture into the control. ' You should replace the bold image ' in the sample below with an icon of your own choosing. PictureBox1.Image = Image.FromFile _ (System.Environment.GetFolderPath _ (System.Environment.SpecialFolder.Personal) _ & "\Image.gif") End Sub
private void StretchPic(){ // Stretch the picture to fit the control. PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; // Load the picture into the control. // You should replace the bold image // in the sample below with an icon of your own choosing. // Note the escape character used (@) when specifying the path. PictureBox1.Image = Image.FromFile _ (System.Environment.GetFolderPath _ (System.Environment.SpecialFolder.Personal) _ + @"\Image.gif") }
private: void StretchPic() { // Stretch the picture to fit the control. pictureBox1->SizeMode = PictureBoxSizeMode::StretchImage; // Load the picture into the control. // You should replace the bold image // in the sample below with an icon of your own choosing. pictureBox1->Image = Image::FromFile(String::Concat( System::Environment::GetFolderPath( System::Environment::SpecialFolder::Personal), "\\Image.gif")); }