Click here to Skip to main content
15,886,963 members
Articles / Programming Languages / C#

C# Image Selection Form

Rate me:
Please Sign up or sign in to vote.
2.68/5 (9 votes)
17 Nov 2006CPOL1 min read 57K   1.1K   10   2
A simple form that displays a lot of images to allow the user to select one from the list.

Sample Image - C__Image_Selection_form.png

Introduction

While working on the development of applications, some times we can save a lot of time if we can find reusable code on the internet. I was searching for a tool to display images in a form to allow a user to select one of them. After searching for a while, I decided to develop this and to post it here on CodeProject. It's a simple user form and is easy to understand. You can include it in your application by adding the three source files into your project.

Displaying the Form

There are two ways to load images. One is by filling the String array named imgList that contains the list of files to be displayed:

C#
FormSelectImage form = new FormSelectImage();
form.imgSize = new Size( 200, 200 );
form.imgList = myListOfFiles;

if( form.ShowDialog() == DialogResult.OK )
{
    String selectedImage = form.selectedImg;
}

The other method is by calling the function scanDirectory. This function scans the directory with the given search pattern and stores the found files in imgList.

C#
FormSelectImage form = new FormSelectImage(); 
form.imgSize = new Size( 200, 200 );
form.scanDirectory( "C:\\myFiles", "*.jpg", System.IO.SearchOption.AllDirectories );
if( form.ShowDialog() == DialogResult.OK ) {
    String selectedImage = form.selectedImg;
}

How it Works

The form contains an empty scrollable panel that will be filled when onLoad is called. It generates a PictureBox for each image, then adds the image into the panel.

C#
foreach( String img in imgList )
{
    PictureBox pb = new PictureBox();
    pb.Size = imgSize;
    pb.SizeMode = PictureBoxSizeMode.Zoom;
    pb.Image = Image.FromFile( img );
    pb.BackColor = Color.White;
    pb.Click += new EventHandler( pb_Click );
    pb.Tag = img;
    panel1.Controls.Add( pb );
}

Final Words

I like to develop, and have found a lot of help here on CodeProject; I hope that this little article will help someone else.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow do I pass an image from nodes in the xml to a component on a form Pin
Try748-Jan-07 4:47
Try748-Jan-07 4:47 
AnswerRe: how do I pass an image from nodes in the xml to a component on a form Pin
Daniel Junges8-Jan-07 5:32
Daniel Junges8-Jan-07 5:32 
Hi,
try to apply the follow example, its tries to load you xml into a DataSet. If any error present in you xml file then an specific error message would appear to indicate whats wrong.

try{
// Get a DataSet object
DataSet myDs = new DataSet();

// Get a FileStream object.
FileStream myFs = new FileStream
("myXmlData.xml",FileMode.Open,FileAccess.Read);

// Apply the ReadXml(fileSteeam method) to read the file
myDs.ReadXml(myFs);

// Get a DataTable object from the first table of the DataSet
DataTable myDt = myDs.Tables[0];

// Getting the values for each row
foreach (DataRow row in myDt.Rows)
int imageID = (int)row["imageid"];
String imageID = (String)row["image"];
}
myFs.Close();
}catch(Exception ex){
MessageBox.Show("ERROR OCCURED!\n\n" + ex.ToString() );
}

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.