Click here to Skip to main content
15,886,513 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.6K   8.3K   95  
Thumbnail and image viewing controls for Windows Forms, using C#.
#region gnu_license
/*
    Crystal Controls - C# control library containing the following tools:
        CrystalControl - base class
        CrystalGradientControl - a control that can either have a gradient background or be totally transparent.
        CrystalLabel - a homegrown label that can have a gradient or transparent background.
        CrystalPanel - a panel that can have a gradient or transparent background.
        CrystalTrackBar - a homegrown trackbar that can have a gradient or transparent background.
        CrystalToolStripTrackBar - a host for CrystalTrackBar that allows it to work in a ToolStrip.
        
        CrystalImageGridView - a control that hosts thumbnail images in a virtual grid.
        CrystalImageGridModel - a data model that holds a collection of CrystalImageItems
                                to feed to CrystalImageGridView.
        CrystalImageItem - a class that describes an Image file.
        CrystalThumbnailer - provides thumbnailing methods for images.

        CrystalCollector - a base class for a controller that links 
                            CrystalImageGridView to the CrystalImageGridModel.
        CrystalFileCollector - a controller that works on disk-based Image files.
        CrystalDesignCollector - a controller that works in Visual Studio toolbox designer.
        CrystalMemoryCollector - a controller that can be used to add images from memory.
        CrystalMemoryZipCollector - a controller that accesses images in zip files by streaming them into memory.
        CrystalZipCollector - a controller that accesses images in zip files by unpacking them.
        CrystalRarCollector - a controller that accesses images in rar files by unpacking them.

        CrystalPictureBox - a picture box control, derived from CrystalGradientControl.
        CrystalPictureShow - a control for viewing images and processing slideshows.
        CrystalComicShow - a control for viewing comic-book images in the CDisplay format.
 
    Copyright (C) 2006, 2008 Richard Guion
    Attilan Software Factory: http://www.attilan.com
    Contact: richard@attilan.com

   Version 1.0.0
        This is a work in progress: USE AT YOUR OWN RISK!  Interfaces/Methods may change!
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
#endregion

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Attilan.Crystal.Tools;
using Attilan.Tools;

namespace Attilan.Crystal.Controls
{
    /// <summary>
    /// A Controller class that helps you tie the CrystalImageGridView, CrystalComicShow,
    /// and other misc controls to create a Comic Book Viewer application quickly.
    /// </summary>
    public class CrystalComicShowController : CrystalPictureShowController
    {
        /// <summary>
        /// When true indicates that the Comic Show Controller is in comic book display mode.
        /// </summary>
        bool _comicBookMode = false;

        /// <summary>
        /// Number of images unpacked from the compressed collector.
        /// </summary>
        int _unpackTotal = 0;

        /// <summary>
        /// Form that will be displayed while waiting for the collector to be unpacked.
        /// This Form should implement ICrystalWaitUnpack.
        /// </summary>
        private Form _waitUnpackForm = null;

        #region Events

        /// <summary>
        /// EventHandler that will be called when the time is right to go into full page mode.
        /// </summary>
        public event EventHandler CDisplayModeReady;

        #endregion

        /// <summary>
        /// Form that will be displayed while waiting for the collector to be unpacked.
        /// This Form should implement ICrystalWaitUnpack.
        /// </summary>
        public Form WaitUnpackForm
        {
            get { return _waitUnpackForm; }
            set { _waitUnpackForm = value; }
        }

        /// <summary>
        /// Returns the CrystalComicShow control used to display images.
        /// </summary>
        public CrystalComicShow ComicShow
        {
            get { return (CrystalComicShow)PictureShow; }
        }

        /// <summary>
        /// Creates the appropriate collector based on the source location.
        /// If the source location is a folder, CrystalFileCollector will be returned.
        /// If the source location is a .zip file, CrystalZipCollector will be returned.
        /// If the source location is a .rar file, CrystalRarCollector will be returned.
        /// </summary>
        /// <param name="sourcePath">Full path to the source, either a folder, zip file, or rar file.</param>
        /// <returns>CrystalCollector-based object.</returns>
        public override CrystalCollector CreateCollector(string sourcePath)
        {
            if (Collector != null)
            {
                Collector.ImageUpdated -= ImageUnpacked;
            }

            CrystalCollector returnCollector = CrystalCollectorFactory.DefaultFactory.CreateCollector(sourcePath);

            if (returnCollector is ICrystalCompressedCollector)
            {
                _unpackTotal = 0;
                returnCollector.ImageUpdated += ImageUnpacked;

                _comicBookMode = true;
            }
            else
            {
                _comicBookMode = false;
            }

            return returnCollector;
        }

        /// <summary>
        /// Event handler that responds to an image getting unpacked from the compressed archive.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">Event arguments.</param>
        void ImageUnpacked(object sender, EventArgs e)
        {
            if ((CDisplayModeReady != null) && (++_unpackTotal > 3))
            {
                InitInitialImage(0, true);
                EventNotifier.UpdateSubscribers(CDisplayModeReady, e, sender);
                /*
                if (CDisplayModeReady.Target is Control)
                {
                    ((Control)(CDisplayModeReady.Target)).Invoke(CDisplayModeReady, new object[] { this, e });
                }
                else
                {
                    CDisplayModeReady(this, e);
                }
                 */
            }
        }

        /// <summary>
        /// Initialize the Crystal collector object.
        /// </summary>
        /// <param name="sourcePath">Source folder path where images files are located.</param>
        /// <returns>True if collect images was started properly, false otherwise.</returns>
        public override bool InitCollector(string sourcePath)
        {
            if (base.InitCollector(sourcePath))
            {
                if (Collector is ICrystalCompressedCollector)
                {
                    ShowUnpackForm(sourcePath);
                }

                return true;
            }

            return false;
        }

        /// <summary>
        /// Sets the ComicShow Controller in CDisplay compatible mode.
        /// </summary>
        private void SetCDisplayImage()
        {
            PictureShow.ImageSizeMode = SizeMode.CDisplay;
            //SetZoomComboText(ZoomFactors[ComicIndex]);
        }

        /// <summary>
        /// Sets the ComicShow Controller in Full Screen Mode.
        /// </summary>
        public override void FullScreenMode()
        {
            CurrentViewMode = CrystalViewMode.FullScreenView;

            if (_comicBookMode)
            {
                SetCDisplayImage();
            }
            else
            {
                SetFitImage();
            }

            ComicShow.SetScrollPos(new Point(0, 0));
        }

        /// <summary>
        /// Displays the image contained in the CrystalImageItem object.
        /// </summary>
        /// <param name="imageItem">CrystalImageItem object containing information an image to display.</param>
        /// <returns>True if the controller could display the object contained in the CrystalImageItem object.</returns>
        public override bool DisplayImage(CrystalImageItem imageItem)
        {
            if ((imageItem == null) || (!imageItem.Selected))
            {
                SetNullImage();
                UpdateDisplaySubscribers();
                return false;
            }

            if (imageItem != CurrentImage)
            {
                if ((ComicShow.IsCDisplay()) && (CurrentViewMode == CrystalViewMode.FullScreenView))
                {
                    ComicShow.PageNumberCaption =
                        CrystalTools.GetPageString(GetSelectedItemIndex(), Collector.GridModel.Count);
                }

                return base.DisplayImage(imageItem);
            }

            return false;
        }

        /// <summary>
        /// Returns the UnpackForm that is properly initialized with the image name and event handlers set up.
        /// </summary>
        /// <param name="fileName">Name of the compressed file that will be unpacked.</param>
        /// <returns>The Form object if properly initialized, false otherwise.</returns>
        private Form GetUnpackForm(string fileName)
        {
            if (_waitUnpackForm != null)
            {
                if (_waitUnpackForm is ICrystalWaitUnpack)
                {
                    ICrystalWaitUnpack unpackForm = (ICrystalWaitUnpack)_waitUnpackForm;
                    unpackForm.UnpackTerminated += _waitUnpackForm_UnpackTerminated;
                    unpackForm.ImageName = Path.GetFileName(fileName);
                    unpackForm.StartShow();
                }
            }

            return _waitUnpackForm;
        }

        /// <summary>
        /// Handles the unpack terminated event of the unpack form.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">Event arguments.</param>
        void _waitUnpackForm_UnpackTerminated(object sender, EventArgs e)
        {
            HideUnpackForm();
            StopThumbnailer();
        }

        /// <summary>
        /// Displays the unpack form with the given filename.
        /// </summary>
        /// <param name="fileName">Name of the file getting unpacked.</param>
        private void ShowUnpackForm(string fileName)
        {
            Form unpackForm = GetUnpackForm(fileName);
            if (unpackForm != null)
            {
                unpackForm.StartPosition = FormStartPosition.Manual;
                Point newLoc = ImageForm.Location;
                newLoc.X += 30;
                newLoc.Y += 30;
                unpackForm.Location = newLoc;
                unpackForm.Show(ImageForm);
            }
        }

        /// <summary>
        /// Delegate that is is to call HideUnpackForm when InvokeRequire is true.
        /// </summary>
        delegate void HideUnpackFormDelegate();

        /// <summary>
        /// Hides the unpack form.
        /// </summary>
        private void HideUnpackForm()
        {
            if (_waitUnpackForm != null)
            {
                if (!_waitUnpackForm.InvokeRequired)
                {
                    if (_waitUnpackForm is ICrystalWaitUnpack)
                    {
                        ICrystalWaitUnpack unpackForm = (ICrystalWaitUnpack)_waitUnpackForm;
                        unpackForm.EndShow();
                    }

                    _waitUnpackForm.Hide();
                }
                else
                    _waitUnpackForm.Invoke(new HideUnpackFormDelegate(HideUnpackForm));
            }
        }

        /// <summary>
        /// Deals with the ThumbnailComplete event from the CrystalCollector.
        /// </summary>
        protected override void ThumbnailingComplete()
        {
            base.ThumbnailingComplete();
            HideUnpackForm();
        }
    }
}

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