C# Windows Forms PictureBox Examples

Let us look at a simple C# Windows Forms PictureBox example.

Example 1: PictureBox

This example will comprise the following files:

  • PictureBox.cs
  • Resources.Designer.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). PictureBox.cs

Create a file named PictureBox.cs

Here is the full code

using System.IO;
using System.Windows.Forms;

namespace Examples {
  class MainForm : Form {
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new MainForm());
    }

    public MainForm() {
      Text = "PictureBox example";
      ClientSize = new System.Drawing.Size(300, 300);

      pictureBox1.Parent = this;
      pictureBox1.BorderStyle = BorderStyle.FixedSingle;
      pictureBox1.Location = new System.Drawing.Point(20, 20);
      pictureBox1.Size = new System.Drawing.Size(260, 260);
      pictureBox1.ImageLocation = "c:\logo.bmp";
      pictureBox1.Image = Examples.Properties.Resources.Logo;
      pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
      pictureBox1.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
    }

    private PictureBox pictureBox1 = new PictureBox();
  }
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Example 1: PictureBoxWithoutResource

This example will comprise the following files:

  • PictureBoxWithoutResource.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). PictureBoxWithoutResource.cs

Create a file named PictureBoxWithoutResource.cs

Here is the full code

using System.IO;
using System.Windows.Forms;

namespace Examples {
  class MainForm : Form {
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new MainForm());
    }

    public MainForm() {
      this.pictureBox1.Parent = this;
      this.pictureBox1.Size = new System.Drawing.Size(280, 280);
      this.pictureBox1.Location = new System.Drawing.Point(10, 10);
      this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
      this.pictureBox1.Load(Path.Combine("Resources", "Logo.png"));
      this.pictureBox1.BorderStyle = BorderStyle.FixedSingle;

      this.Text = "PictureBox example";
      this.ClientSize = new System.Drawing.Size(300, 300);
    }

    private PictureBox pictureBox1 = new PictureBox();
  }
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Related Posts