Export (0) Print
Expand All

PictureBox Class

Represents a Windows picture box control for displaying an image.

For a list of all members of this type, see PictureBox Members.

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

[Visual Basic]
Public Class PictureBox
   Inherits Control
[C#]
public class PictureBox : Control
[C++]
public __gc class PictureBox : public Control
[JScript]
public class PictureBox extends Control

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

Typically the PictureBox is used to display graphics from a bitmap, metafile, icon, JPEG, GIF or PNG file.

Set the Image property to the Image you want to display, either at design time or at run time. Clipping and positioning of the image in the display area is controlled by the SizeMode property which is set to values in the PictureBoxSizeMode enumeration. You can change the size of the display area at run time with the ClientSize property.

The PictureBox control is displayed by default without any borders. You can provide a standard or three-dimensional border using the BorderStyle property to distinguish the picture box from the rest of the form, even if it contains no image.

Example

[Visual Basic, C#, C++] The following example illustrates how you can set an image and resize the display area of the picture box. This example assumes that ShowMyImage is called from within an existing form, and that the System.Drawing namespace has been added to the source code for your form.

[Visual Basic] 
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 Not (MyImage Is 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


[C#] 
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 ;
}


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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Windows.Forms (in System.Windows.Forms.dll)

See Also

PictureBox Members | System.Windows.Forms Namespace

Show:
© 2014 Microsoft