Using PictureBox In Windows Forms

INTRODUCTION

In this article, I am going to explain how to use a PictureBox on a Windows.Forms app using Visual Studio 2017.

STEP 1 - Start the Project

Let's create a new project using Visual Studio 2017.

Select New Project--->Visual C#-->Windows Forms App (.NET Framework), give your project a name and click OK.

Using PictureBox In Windows Forms
 
This action creates a WinForms project with a default form and you should see the Windows Designer.

STEP 2 - Drag and Drop Control

Let's add a PictureBox control to the form by dragging it from Toolbox and dropping it to the form. You will see that a PictureBox 1 is added to the form. This control is now available to you in the code behind. 
 
Using PictureBox In Windows Forms 
 
Additional control 
 
Now, let's add another control to the form by dragging the other control from Toolbox to the form. Drag and drop openFileDialog, saveFileDialog and button control from Toolbox. You can also change the properties of the other controls.
 
PictureBox
 
The PictureBox control is used for displaying images on the form. The Image property of the control allows you to set an image both at design time or at a runtime. 
 
Properties of PictureBox 
  • AllowDrop Property
    Specifies whether the picture box accepts data that a user drags on it.

  • ImageLocationProperty
    Gets or sets the path or the URL for the image displayed in the control.

  • InitialImage Property
    Gets or sets the image displayed in the control when the main image is loaded.

  • WaitOnLoad Property
    Specifies whether or not an image is loaded synchronously. 
STEP 3 - Coding for Button Click Event

You can add a button-click event handler by simply double-clicking on the button control event handler. We can open and save the picture using saveFileDialog and openFileDialog.
  1. public partial class Form1: Form  
  2.     {  
  3.         Image file;  
  4.   
  5.   
  6.         public Form1()  
  7.         {  
  8.             InitializeComponent();  
  9.         }  
  10.   
  11.         private void button1_Click(object sender, EventArgs e)  
  12.         {  
  13.             OpenFileDialog f = new OpenFileDialog();  
  14.             f.Filter = "JPG (*.JPG)|*.jpg";  
  15.             if(f.ShowDialog() == DialogResult.OK)  
  16.             {  
  17.                 file = Image.FromFile(f.FileName);  
  18.                 pictureBox1.Image = file;  
  19.             }  
  20.         }  
  21.   
  22.         private void button2_Click(object sender, EventArgs e)  
  23.         {  
  24.   
  25.             SaveFileDialog f = new SaveFileDialog();  
  26.             f.Filter = "JPG (*.JPG)|*.jpg";  
  27.             if (f.ShowDialog() == DialogResult.OK)  
  28.             {  
  29.                 file.Save(f.FileName);  
  30.                   
  31.             }  
  32.   
  33.         }  
  34.   
  35.     }  
  36. }  
STEP 4 - Compile and Run

Now, simply compile and run the application. Click the open button. Choose the image from the local disk and it will be displayed in the PictureBox.
 
Using PictureBox In Windows Forms
 
Using PictureBox In Windows Forms
 
Now, click and save the button. The picture will be saved to the disk in the local folder.
 
Using PictureBox In Windows Forms
 
Using PictureBox In Windows Forms
 
Summary

In this article, you saw how to use a PictureBox control. Hope you found this article interesting. For any feedback, please post it as a comment at the bottom of this article. Thank you!.