次の方法で共有


方法: 実行時に画像のサイズまたは配置を変更する (Windows フォーム)

フォームで Windows フォーム PictureBox コントロールを使用する場合は、そのフォームの SizeMode プロパティを次の値に設定できます。

  • 図の左上隅をコントロールの左上隅に合わせる

  • コントロール内の画像を中央揃えする

  • 表示する画像に合わせてコントロールのサイズを調整する

  • 表示される画像をコントロールに合わせて引き伸ばす

画像 (特にビットマップ形式の画像) を拡大すると、画質が低下する可能性があります。 メタファイルは、実行時に画像を描画するためのグラフィックス命令のリストであり、ビットマップよりもストレッチに適しています。

実行時に SizeMode プロパティを設定するには

  1. SizeModeNormal (既定値)、AutoSizeCenterImage、または 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"));  
       }  
    

関連項目