Export (0) Print
Expand All
4 out of 12 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)

[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
[ComVisibleAttribute(true)] 
public class PictureBox : Control, ISupportInitialize
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
/** @attribute ComVisibleAttribute(true) */ 
public class PictureBox extends Control implements ISupportInitialize
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
ComVisibleAttribute(true) 
public class PictureBox extends Control implements ISupportInitialize
Not applicable.

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.

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
           Microsoft.Ink.InkPicture
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 Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 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.