PictureBox.Image 屬性

定義

取得或設定 PictureBox 顯示的影像。Gets or sets the image that is displayed by PictureBox.

public:
 property System::Drawing::Image ^ Image { System::Drawing::Image ^ get(); void set(System::Drawing::Image ^ value); };
[System.ComponentModel.Bindable(true)]
public System.Drawing.Image Image { get; set; }
member this.Image : System.Drawing.Image with get, set
Public Property Image As Image

屬性值

要顯示的 ImageThe Image to display.

屬性

範例

下列PictureBox程式碼範例示範如何在執行時間建立點陣圖, 並藉由Image設定屬性將它顯示在中。The following code example demonstrates how to create a bitmap at runtime and display it in a PictureBox by setting the Image property. 若要執行此範例, 請將它貼入 Windows form, CreateBitmapAtRuntime並從表單的「函式」呼叫。To run this example, paste it into a Windows Form and call CreateBitmapAtRuntime from the form's constructor.

PictureBox pictureBox1 = new PictureBox();
public void CreateBitmapAtRuntime()
{
    pictureBox1.Size = new Size(210, 110);
    this.Controls.Add(pictureBox1);

    Bitmap flag = new Bitmap(200, 100);
    Graphics flagGraphics = Graphics.FromImage(flag);
    int red = 0;
    int white = 11;
    while (white <= 100) {
        flagGraphics.FillRectangle(Brushes.Red, 0, red, 200,10);
        flagGraphics.FillRectangle(Brushes.White, 0, white, 200, 10);
        red += 20;
        white += 20;
    }
    pictureBox1.Image = flag;
 
}
Private pictureBox1 As New PictureBox()

Public Sub CreateBitmapAtRuntime() 
    pictureBox1.Size = New Size(210, 110)
    Me.Controls.Add(pictureBox1)
    
    
    Dim flag As New Bitmap(200, 100)
    Dim flagGraphics As Graphics = Graphics.FromImage(flag)
    Dim red As Integer = 0
    Dim white As Integer = 11
    While white <= 100
        flagGraphics.FillRectangle(Brushes.Red, 0, red, 200, 10)
        flagGraphics.FillRectangle(Brushes.White, 0, white, 200, 10)
        red += 20
        white += 20
    End While
    pictureBox1.Image = flag

End Sub 

備註

Image 屬性設定為要顯示的 ImageThe Image property is set to the Image to display. 您可以在設計階段或執行時間執行此動作。You can do this either at design time or at run time.

注意

如果您想要在多個PictureBox控制項中使用相同的影像, 請為每個PictureBox控制項建立映射的複本。If you want to use the same image in multiple PictureBox controls, create a clone of the image for each PictureBox. 從多個控制項存取相同的影像會導致發生例外狀況。Accessing the same image from multiple controls causes an exception to occur.

適用於

另請參閱