Export (0) Print
Expand All
15 out of 20 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)
[DockingAttribute(DockingBehavior.Ask)]
[ComVisibleAttribute(true)]
[DefaultBindingPropertyAttribute("Image")]
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
public class PictureBox : Control, 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.

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 ;
}
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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.