PictureBox 类

表示用于显示图像的 Windows 图片框控件。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class PictureBox
    Inherits Control
    Implements ISupportInitialize
用法
Dim instance As PictureBox
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
public class PictureBox : Control, ISupportInitialize
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] 
public ref class PictureBox : public Control, ISupportInitialize
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
public class PictureBox extends Control implements ISupportInitialize
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
public class PictureBox extends Control implements ISupportInitialize

备注

通常使用 PictureBox 来显示位图、元文件、图标、JPEG、GIF 或 PNG 文件中的图形。

在设计时或运行时将 Image 属性设置为要显示的 Image。也可以通过设置 ImageLocation 属性指定图像,然后使用 Load 方法同步加载图像或使用 LoadAsync 方法异步加载图像。SizeMode 属性(设置为 PictureBoxSizeMode 枚举中的值)控制图像在显示区域中的剪裁和定位。可以在运行时使用 ClientSize 属性来更改显示区域的大小。

默认情况下,PictureBox 控件在显示时没有任何边框。即使图片框不包含任何图像,仍可以使用 BorderStyle 属性提供一个标准或三维的边框,以便使图片框与窗体的其余部分区分。PictureBox 不是可选择的控件,这意味着该控件不能接收输入焦点。

示例

下面的代码示例演示如何设置图像以及调整图片框的显示区域的大小。此示例假定从现有窗体中调用了 ShowMyImage,并且假定已将 System.Drawing 命名空间添加到窗体的源代码中。

Private MyImage As Bitmap
   
Public Sub ShowMyImage(fileToDisplay As String, xSize As Integer, _
                       ySize As Integer)
    ' Sets up an image object to be displayed.
    If (MyImage IsNot Nothing) Then
        MyImage.Dispose()
    End If
       
    ' Stretches the image to fit the pictureBox. 
    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    MyImage = New Bitmap(fileToDisplay)
    pictureBox1.ClientSize = New Size(xSize, ySize)
    pictureBox1.Image = CType(MyImage, Image)
End Sub
private Bitmap MyImage ;
public void ShowMyImage(String fileToDisplay, int xSize, int ySize)
{
   // Sets up an image object to be displayed.
   if (MyImage != null)
   {
      MyImage.Dispose();
   }

   // Stretches the image to fit the pictureBox.
   pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage ;
   MyImage = new Bitmap(fileToDisplay);
   pictureBox1.ClientSize = new Size(xSize, ySize);
   pictureBox1.Image = (Image) MyImage ;
}
   Bitmap^ MyImage;

public:
   void ShowMyImage( String^ fileToDisplay, int xSize, int ySize )
   {
      
      // Sets up an image object to be displayed.
      if ( MyImage != nullptr )
      {
         delete MyImage;
      }

      
      // Stretches the image to fit the pictureBox.
      pictureBox1->SizeMode = PictureBoxSizeMode::StretchImage;
      MyImage = gcnew Bitmap( fileToDisplay );
      pictureBox1->ClientSize = System::Drawing::Size( xSize, ySize );
      pictureBox1->Image = dynamic_cast<Image^>(MyImage);
   }
private Bitmap myImage;
public void ShowMyImage(String fileToDisplay, int xSize, int ySize)
{
    // Sets up an image object to be displayed.
    if (myImage != null) {
        myImage.Dispose();
    }

    // Stretches the image to fit the pictureBox.
    pictureBox1.set_SizeMode(PictureBoxSizeMode.StretchImage);
    myImage = new Bitmap(fileToDisplay);
    pictureBox1.set_ClientSize(new Size(xSize, ySize));
    pictureBox1.set_Image(myImage);
} //ShowMyImage

继承层次结构

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.Control
        System.Windows.Forms.PictureBox

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

PictureBox 成员
System.Windows.Forms 命名空间