Zebra0.com

csharp pictureviewer

Write the code shown below:

// Programmer: Janet Joy
// PIcture Viewer app: Select a picture file and display it.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace picture_viewer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void mnuOpen_Click(object sender, EventArgs e)
        {
            // Set the filter so that only pictures can be selected.
            openFileDialog1.Filter = "All Pictures|*.bmp;*.gif;*.png;*.jpg|JPG|*.jpg|Bitmaps|*.bmp|GIFS|*.gif|PNG|*.png";
            // Get rid of any previous filename
            openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();
            // If the user clicks CANCEL the filename is blank 
            if (openFileDialog1.FileName != "")
            {
                // Load the selected picture into the picture box
                picShow.Load(openFileDialog1.FileName);
                // Display the name of the file
                Text = openFileDialog1.FileName;
            }
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            // End the program by closing the form.
            this.Close();
        }
    }
}
The filter will cause the open file dialog to show only picture files:

Test your program and try opening several different picture formats.

Save all before moving on to the next step.

End of lesson, Next lesson: The Pizza Project in C#