View Image Files

Lardmeister 0 Tallied Votes 173 Views Share

A simple picture viewer written in C# that allows you to view jpg, bmp, gif and animated gif image files. Uses a file dialog to load files, and keeps track of pictures already viewed with a list box.

// view jpeg, bitmap and gif (also animated) images
// this is a Windows GUI program with Buttons, ListBox, PictureBox,
// and an OpenFileDialog
// compiled with C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe

using System;
using System.Drawing;        // image
//using System.Collections;
using System.Windows.Forms;  // GUI stuff
using System.IO;             // Path.GetFileName()

namespace PicView1
{
    // summary description for Form1.
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.ListBox listBox1;
        private System.Windows.Forms.Button OpenFileButton;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Button QuitButton;
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            InitializeComponent();
        }
        
        // a must, clean up any resources being used
        protected override void Dispose( bool disposing )
        {
            if (disposing)
            {
                if (components != null) 
                {
                     components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        // these are the GUI components used
        private void InitializeComponent()
        {
            this.QuitButton = new System.Windows.Forms.Button();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.OpenFileButton = new System.Windows.Forms.Button();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            // 
            // QuitButton
            // 
            this.QuitButton.Location = new System.Drawing.Point(440, 344);
            this.QuitButton.Name = "QuitButton";
            this.QuitButton.Size = new System.Drawing.Size(144, 23);
            this.QuitButton.TabIndex = 1;
            this.QuitButton.Text = "Quit";
            this.QuitButton.Click += new System.EventHandler(this.QuitButton_Click);
            // 
            // pictureBox1
            // 
            this.pictureBox1.Location = new System.Drawing.Point(8, 8);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(424, 360);
            this.pictureBox1.TabIndex = 2;
            this.pictureBox1.TabStop = false;
            // 
            // openFileDialog1
            // 
            this.openFileDialog1.Filter = "jpeg|*.jpg|Bitmap|*.bmp|gif|*.gif";
            this.openFileDialog1.Title = "Open Image File";
            // 
            // OpenFileButton
            // 
            this.OpenFileButton.Location = new System.Drawing.Point(440, 8);
            this.OpenFileButton.Name = "OpenFileButton";
            this.OpenFileButton.Size = new System.Drawing.Size(144, 23);
            this.OpenFileButton.TabIndex = 0;
            this.OpenFileButton.Text = "Open File";
            this.OpenFileButton.Click += new System.EventHandler(this.OpenFileButton_Click);
            // 
            // listBox1
            // 
            this.listBox1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(224)), ((System.Byte)(192)));
            this.listBox1.ItemHeight = 16;
            this.listBox1.Location = new System.Drawing.Point(440, 48);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(144, 276);
            this.listBox1.TabIndex = 3;
            this.listBox1.SelectedIndexChanged += new System.EventHandler(this.ListBox1SelectedIndexChanged);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
            this.ClientSize = new System.Drawing.Size(592, 376);
            this.Controls.Add(this.listBox1);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.QuitButton);
            this.Controls.Add(this.OpenFileButton);
            this.Name = "Form1";
            this.Text = "Picture Viewer";
            this.ResumeLayout(false);
        }

        static void Main() 
        {
            Application.Run(new Form1());
        }

        private void OpenFileButton_Click(object sender, System.EventArgs e)
        {
            string fname;
          
            // show the openFile dialog box
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fname = openFileDialog1.FileName;
                pictureBox1.Image = Image.FromFile(fname);
                this.Text = String.Concat("Viewer (" + fname + ")");
                listBox1.Items.Add(Path.GetFileName(fname));
            }
        }

        private void QuitButton_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
        
        // select past filename from the ListBox items
        void ListBox1SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string fname = listBox1.SelectedItems[0].ToString();
            this.Text = fname;
            pictureBox1.Image = Image.FromFile(fname);
        }
    }
}
Lardmeister 461 Posting Virtuoso

To compile the program, copy and paste the code into an editor, and save it for instance as "viewer.cs". Then find the C# compiler "csc.exe" in the "C:\Windows\Microsoft.NET\Framework\ ..." folder and run the command line (you might have to specify the folders too):
csc.exe viewer.cs
That should produce a file "viewer.exe". The nice thing about this approach is that you have one clean code file and one clean small executable file. The MSV studio program on the other hand produces a confusing number of files, splitting the code.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

A real nice example of a C# GUI program. Thanks for the comment on running it simply from the c# compiler csc.exe that seems to come with most Windows installations.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.