SIGN UP MEMBER LOGIN:    
ARTICLE

PictureBox in C#

Posted by Mahesh Chand Articles | Windows Controls C# August 24, 2010
PictureBox control is used to display images in Windows Forms. In this article, I will discuss how to use a PictureBox control to display images in Windows Forms applications.
Reader Level:
Download Files:
 


PictureBox control is used to display images in Windows Forms. In this article, I will discuss how to use a PictureBox control to display images in Windows Forms applications.


Creating a PictureBox

PictureBox class represents a PictureBox control. The following code snippet creates a PictureBox, sets its width and height and adds control to the Form by calling Controls.Add() method.

C# Code:

PictureBox imageControl = new PictureBox();

imageControl.Width = 400;

imageControl.Height = 400;

 

Controls.Add(imageControl);

 

VB.NET Code:

 

Dim imageControl As New PictureBox()

imageControl.Width = 400

imageControl.Height = 400

Display an Image

Image property is used to set an image to be displayed in a PictureBox control. The following code snippet creates a Bitmap from an image and sets the Image property of PictureBox control. Code also sets the Dock property of PictureBox.

C# Code:                                                                                                                                  

private void DisplayImage()

{

    PictureBox imageControl = new PictureBox();

    imageControl.Width = 400;

    imageControl.Height = 400;

 

    Bitmap image = new Bitmap("C:\\Images\\Creek.jpg");

    imageControl.Dock = DockStyle.Fill;

    imageControl.Image = (Image)image;

 

    Controls.Add(imageControl);

}

 

VB.NET Code:

 

Private Sub DisplayImage()

        Dim imageControl As New PictureBox()

        imageControl.Width = 400

        imageControl.Height = 400

        Dim Image As New Bitmap("C:\\Images\\Creek.jpg")

        imageControl.Dock = DockStyle.Fill

        imageControl.Image = Image

        Controls.Add(imageControl)

    End Sub

 

The output looks like Figure 1 where an image is displayed.

PictureBoxImg1.jpg

SizeMode

SizeMode property is used to position an image within a PictureBox. It can be Normal, StretchImage, AutoSize, CenterImage, and Zoom. The following code snippet sets SizeMode property of a PictureBox control.

C# Code:

imageControl.SizeMode = PictureBoxSizeMode.CenterImage;

 

VB.NET Code:

 

imageControl.SizeMode = PictureBoxSizeMode.CenterImage

 

Summary
In this article, we discussed discuss how to use a PictureBox control to display images in Windows Forms applications.
Further Readings

share this article :
post comment
 

Post comment here

Posted by Mahesh Chand Aug 24, 2010
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor