C# tutorial-C# GUI: image viewer example |
|||||||||||||||||||||||||||
C# GUIImage viewer application exampleIn this C# GUI example, you will learn to create an image viewer application as shown below:![]() 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() { InitializeComponent(); }
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 listBox1.Items.Add(filename);}
}
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()); } } }
|
|||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||