Click here to Skip to main content
15,892,059 members
Articles / Desktop Programming / Windows Forms

Crystal Image Toolkit: thumbnail image control and picture viewing.

Rate me:
Please Sign up or sign in to vote.
4.68/5 (21 votes)
11 May 2011LGPL37 min read 109.8K   8.3K   95  
Thumbnail and image viewing controls for Windows Forms, using C#.
using System;
using System.Windows.Forms;
using Attilan.Crystal.Controls;

namespace SimpleImageGridDemo
{
    /// <summary>
    /// Demonstrates the minimal steps required to get CrystalImageGridView running
    /// properly in your Forms applications.
    /// </summary>
    public partial class Form1 : Form
    {
        /// <summary>
        /// CrystalFileCollector object, assists in retrieving image files in a specified folder.
        /// </summary>
        private CrystalCollector _theCollector = null;

        /// <summary>
        /// Internal flag indicating whether the CrystalThumbnail thread is running in the background.
        /// </summary>
        private bool _thumbnailComplete = true;

        public Form1()
        {
            InitializeComponent();
        }


        /// <summary>
        /// Initialize the Crystal collector object.
        /// </summary>
        /// <param name="sourcePath">Source folder path where images files are located.</param>
        private void InitCollector(string sourcePath)
        {
            if (_theCollector != null)
            {
                // Stop the thumbnailer if it is currently running in the background.
                _theCollector.ThumbnailComplete -= new EventHandler(_collector_ThumbnailComplete);
                StopThumbnailer();
                _theCollector.PurgeImages();
            }
            else
                _theCollector =
                    CrystalCollectorFactory.DefaultFactory.CreateCollector(CrystalCollectorType.CrystalFileCollector);

            // If the source path is empty, and the CrystalFileCollector has
            // just been initialized, the default ImageLocation is set to MyPictures.
            if (sourcePath != string.Empty)
                _theCollector.ImageLocation = sourcePath;

            // Add the CrystalImageGridView object to the collector.
            // The collector will work with the view to draw the images.
            _theCollector.SetupView(crystalImageGridView1);
            _thumbnailComplete = false;

            // Let's subscribe to the thumbnail complete event,
            // so we will know when then background thumbnail task is done.
            _theCollector.ThumbnailComplete += new EventHandler(_collector_ThumbnailComplete);

            // Tell CrystalFileCollector to collect the images in the ImageLocation folder.
            _theCollector.CollectImages();
        }

        /// <summary>
        /// Stop the background thumbnail thread.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">Event arguments.</param>
        void _collector_ThumbnailComplete(object sender, EventArgs e)
        {
            _thumbnailComplete = true;
        }

        /// <summary>
        /// Open a new folder using the folder browse dialog.
        /// </summary>
        /// <param name="sender">Object sender.</param>
        /// <param name="e">Event arguments.</param>
        private void openFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Browse for a new image folder and set this into the CrystalFileCollector.
            folderBrowserDialog1.SelectedPath = _theCollector.ImageLocation;
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                InitCollector(folderBrowserDialog1.SelectedPath);
            }
        }

        /// <summary>
        /// Exit the application.
        /// </summary>
        /// <param name="sender">Object sender.</param>
        /// <param name="e">Event arguments.</param>
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// Stop the thumbnailer by killing the collection thread.
        /// </summary>
        protected void StopThumbnailer()
        {
            if ((_theCollector != null) && (!_thumbnailComplete))
            {
                _theCollector.StopCollection();
                _thumbnailComplete = true;
            }
        }

        /// <summary>
        /// Form close event handler.
        /// </summary>
        /// <param name="e">Event arguments.</param>
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            // Kill the thumbnailer before closing.
            StopThumbnailer();
            
            base.OnFormClosing(e);
        }

        /// <summary>
        /// Form load, initialize the collector.
        /// </summary>
        /// <param name="sender">object sender.</param>
        /// <param name="e">Event arguments.</param>
        private void Form1_Load(object sender, EventArgs e)
        {
            // Initialize the collector with no specified folder.
            // This will make the CrystalFileCollector use the MyPictures
            // folder by default.
            InitCollector(string.Empty);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior)
United States United States
Richard has been working with Windows software since 1991. He has worked for Borland, Microsoft, Oracle, and various startup companies such as Livescribe. Currently he is developing projects in C#, Windows Forms, and Net Framework. Visit his blog Attilan (www.attilan.com) to learn more about his tools, projects and discoveries.

Comments and Discussions