HOW TO:於執行階段修改圖片的大小或位置 (Windows Form)
如果您在表單上使用 Windows Form PictureBox 控制項,可以將控制項的 SizeMode 屬性設定為:
將圖片的左上角對齊控制項的左上角
置中控制項內的圖片
調整控制項的大小以容納它顯示的圖片
自動縮放所顯示的任何圖片以符合控制項
自動縮放圖片 (特別是點陣圖格式的圖片) 可能會降低影像品質。 中繼檔是圖形指令清單,可在執行階段繪製影像,它與自動縮放的配合度比點陣圖還好。
若要在執行階段設定 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.set_SizeMode(PictureBoxSizeMode.StretchImage); // Load the picture into the control. // You should replace "image.gif" in the sample below // with an icon of your own choosing. pictureBox1.set_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")); }
請參閱
工作
HOW TO:使用設計工具載入圖片 (Windows Form)
HOW TO:在執行階段設定圖片 (Windows Form)
參考
PictureBox 控制項概觀 (Windows Form)