Upload/Display Image In Picture Box Using C#

Introduction

Browsing and displaying an image in picture box tool using  Windows Forms application is a very simple task. In this application, we use the following classes/controls to do the b.

  • OpenFileDialog 
  • PictureBox
  • TextBox
  • Button 
Main Functions

This functions simply perform the following steps,
  1. Open a file dialog box so that a user can select an image from his/her machine
  2. Browse the image 
  3. Display selected image in a picture box on a Form 
  4. Display image file path in text box 
Here is the code,
  1. // open file dialog   
  2. OpenFileDialog open = new OpenFileDialog();  
  3. // image filters  
  4. open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";  
  5. if (open.ShowDialog() == DialogResult.OK) {  
  6.     // display image in picture box  
  7.     pictureBox1.Image = new Bitmap(open.FileName);  
  8.     // image file path  
  9.     textBox1.Text = open.FileName;  
  10. }  
What is an image filter?

Image filter is basically a filter that restrict user to select only valid image file. You can remove image filter. 

Up Next
    Ebook Download
    View all
    Learn
    View all