Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
PictureBox Class

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
PictureBox Class

Represents a Windows picture box control for displaying an image.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic (Declaration)
<DockingAttribute(DockingBehavior.Ask)> _
<ComVisibleAttribute(True)> _
<DefaultBindingPropertyAttribute("Image")> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class PictureBox _
    Inherits Control _
    Implements ISupportInitialize
Visual Basic (Usage)
Dim instance As PictureBox
C#
[DockingAttribute(DockingBehavior.Ask)]
[ComVisibleAttribute(true)]
[DefaultBindingPropertyAttribute("Image")]
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
public class PictureBox : Control, ISupportInitialize
Visual C++
[DockingAttribute(DockingBehavior::Ask)]
[ComVisibleAttribute(true)]
[DefaultBindingPropertyAttribute(L"Image")]
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)]
public ref class PictureBox : public Control, 
    ISupportInitialize
JScript
public class PictureBox extends Control implements ISupportInitialize

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. You can alternatively specify the image by setting the ImageLocation property and load the image synchronously using the Load method or asynchronously using the LoadAsync method.

NoteNote:

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.

The SizeMode property, which is set to values in the PictureBoxSizeMode enumeration, controls the clipping and positioning of the image in the display area. You can change the size of the display area at run time with the ClientSize property.

By default, the PictureBox control is displayed by 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. The PictureBox is not a selectable control, which means that it cannot receive input focus.

The following code example illustrates how you can set an image and resize the display area of the picture box. This example requires that ShowMyImage is called in 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 (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


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 ;
}


Visual C++
   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);
   }


System..::.Object
  System..::.MarshalByRefObject
    System.ComponentModel..::.Component
      System.Windows.Forms..::.Control
        System.Windows.Forms..::.PictureBox
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker