C# tutorial-C# GUI: image viewer example


Image viewer application example

In this C# GUI example, you will learn to create an image viewer application as shown below:

c# gui image viewer

You will use one command button, one listBox, one pictureBox, and one openFileDialog. The user will click the button to add images to the listBox. The image will be viewed when it is selected from the listBox.

The C# code to add and view images is shown below:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;


namespace CWindowGUI


    public partial class Form2 : Form


        public ImageView()





        private void button1_Click(object sender, EventArgs e)


        DialogResult result;

        //set filter

        openFileDialog1.Filter = "JPEG Files (*.jpg)|*.jpg|Bitmap Files(*.bmp)|*.bmp| Gif files(*.gif)|*.gif";

        //Enable multi-file names selection

        openFileDialog1.Multiselect = true;

        //Open file dialog

        result = openFileDialog1.ShowDialog();

        if (result == DialogResult.OK)

            foreach(string filename in openFileDialog1.FileNames){

                //add selected file names to listbox






        private void ImageView_Load(object sender, EventArgs e)





        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)


          //view selected image 

         pictureBox1.Image = Image.FromFile(listBox1.SelectedItem.ToString());





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.