SIGN UP MEMBER LOGIN:    
ARTICLE

Upload/Display Image in Picture Box Using C#

Posted by Muhammad Irfan Articles | Windows Forms C# June 17, 2010
In this article, we learn how to browse, view and display an image path in a picture box tool using C# Windows Forms application.
Reader Level:
Download Files:
 


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:
// open file dialog
 OpenFileDialog open = new OpenFileDialog();
 // image filters
 open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
 if (open.ShowDialog() == DialogResult.OK)
{
   // display image in picture box
   pictureBox1.Image = new Bitmap(open.FileName);
    // image file path
    textBox1.Text = open.FileName;
  }

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. 

Login to add your contents and source code to this article
share this article :
post comment
 

Hi Irfan, I need to Delete/Replece the original image file that currently displayed in picture box using C# code. while I'm trying to delete the image file.I caught the below error, <!> Resource Already in use... Thanks in Advance..! regards, -Sakthi Manoj R

Posted by sakthi manoj Apr 16, 2012

VEry nicely done, thanks

Posted by eyesark izzo Jan 05, 2012

it help me a lot.

Posted by sisay demelash Dec 23, 2010

Thanks.. now I want save this image as datatype = binary in database sql2005/08. can u please give me solution for that.

Posted by Devang RC Dec 09, 2010

This is your Visual Basic code:

' open file dialog 

Dim open As New OpenFileDialog()

' image filters

open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp"

If open.ShowDialog() = DialogResult.OK Then

	' display image in picture box

	pictureBox1.Image = New Bitmap(open.FileName)

	' image file path

	textBox1.Text = open.FileName

End If


Posted by Muhammad Irfan Oct 27, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor