PictureBox Control in C#

In this article, we will learn how to use picture box in windows form in C#.

Let's create windows form.

In Visual Studio select "File" = > "New" = > "Project..." then select C# Windows Forms Application then click Ok.

create windows form

First of all, we'll know about the "picture box".

PictureBox helps display images. We'll discuss properties and functions and events of picture box write down.

create picture box

Adding image on picture box

Following these steps :

  1. At first, you click the "arrow icon" that are present on picture top-right corner. This button open a dialog box.When the dialog box open then you click the "Choose image..." button. This button open a dialog box.

    arrow dialog box
  2. When the dialog box open then you click the "Local Resource:" radio button and click the "Import..." button. This button open a open file dialog.

    arrow dialog box2
  3. Then choose image which is want to use your project and click "Open".

    arrow dialog box3
  4. When you see that image which are choosing you. then click "OK".

    arrow dialog box4
  5. You see the image on your picture box.

    arrow dialog box5
  6. In runtime you adding image on picture box :

    // in Bitmap constructor you enter image file location.
    pictureBox1.Image = new Bitmap(@"C:\logo.png");
    

Picturebox with SizeMode

5 types SizeMode are available on visual studio 2010.

  1. AutoSize - The picture box size equal to the size of the image that it contains.

    pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
    
    Autosize of image
  2. CenterImage - The image is displayed in the center if the picture box is larger than the image. If the image larger than the picture box, the picture is placed in the center of the picture box and the outside edge are clipped.

    pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
    
    centerimage of image
  3. Normal - The image is placed in the upper-left corner of the picture box. The image is clipped if it is larger than the picture box it is contained in.

    pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
    
    normal of image
  4. StretchImage - The image within the picturebox is stretched or shrunk to fit the size of picturebox.

    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
    
    stretchimage of image
  5. Zoom - The size of the image increased or decreased maintaining the size ratio.

    pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
    
    zoom of image

BackColor - Changes the background color of picture box.

pictureBox1.BackColor = Color.White;
backcolor of picture box

BackgroundImage - gets or sets the background image on picture box.

pictureBox1.BackgroundImage = new Bitmap(@"C:\logo.png");
background Image of picture box

BackgroundImageLayout - gets or sets the background image layout on picture box.

  1. Center - The image is centered with in the picture box.

    pictureBox1.BackgroundImage = new Bitmap(@"C:\logo.png");
    pictureBox1.BackgroundImageLayout = ImageLayout.Center;
    
    background Image center of picture box
  2. None - The image is left-aligned at top across the picture box.

    pictureBox1.BackgroundImage = new Bitmap(@"C:\logo.png");
    pictureBox1.BackgroundImageLayout = ImageLayout.None;
    
    background Image none of picture box
  3. Stretch - The image is stretch across the picture box.

    pictureBox1.BackgroundImage = new Bitmap(@"C:\logo.png");
    pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
    
    background Image stretch of picture box
  4. Tile - The image is tiled across the picture box.

    pictureBox1.BackgroundImage = new Bitmap(@"C:\logo.png");
    pictureBox1.BackgroundImageLayout = ImageLayout.Tile;
    
    background Image tile of picture box
  5. Zoom - The image is enlarged with in the picture box.

    pictureBox1.BackgroundImage = new Bitmap(@"C:\logo.png");
    pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
    
    background Image zoom of picture box

Dock - Defines which borders of the control are bound to the container.

  1. Bottom - The picture box is docked in bottom of the form control.

    pictureBox1.Dock = DockStyle.Bottom;
    
    dock bottom of picture box

    Fill - The picture box is docked in whole form.

    pictureBox1.Dock = DockStyle.Fill;
    
    dock fill of picture box

    Left - The picture box is docked left side of form.

    pictureBox1.Dock = DockStyle.Left;
    
    dock left of picture box

    None - The picture box is no docked.

    pictureBox1.Dock = DockStyle.None;
    
    dock none of picture box

    Right - The picture box is docked right side of form.

    pictureBox1.Dock = DockStyle.Right;
    
    dock right of picture box

    Top - The picture box is docked top side of form control.

    pictureBox1.Dock = DockStyle.Top;
    
    dock top of picture box


Write a comment


Name Email Message