Export (0) Print
Expand All
7 out of 18 rated this helpful - Rate this topic

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)

[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
public class PictureBox : 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

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. 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.

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. The PictureBox is not a selectable control, meaning 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 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.

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


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

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.