Click here to Skip to main content
15,886,810 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#.
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Attilan.Crystal.Controls;

namespace CrystalTrackBarViewerDemo
{
    public partial class Form1 : Form
    {
        private float[] zoomFactor = { .25f, .33f, .50f, .66f, .80f, 1f, 1.25f, 1.5f, 2.0f, 2.5f, 3.0f };
        private string[] ZoomFactors = { "25%", "33%", "50%", "66%", "80%", "100%", "125%", "150%", "200%", "250%", "300%", "Fit" };
        private string _currentFile;

        public Form1()
        {
            InitializeComponent();
        }

        protected void InitTrackBar()
        {
            SetTrackBarValue(5);
            zoomToolStripTrackBar.CrystalTrackBar.KeyboardControl = true;
        }

        private void InitScaleCombo()
        {
            foreach (string scaleItem in ZoomFactors)
            {
                zoomComboBox.Items.Add(scaleItem);
            }

            SetZoomComboText(ZoomFactors[ZoomFactors.Length - 1]);
        }

        private void SetZoomComboText(string value)
        {
            this.zoomComboBox.SelectedIndexChanged -= new EventHandler(this.zoomComboBox_SelectedIndexChanged);

            if (IsFitImage())
            {
                zoomComboBox.Text = ZoomFactors[ZoomFactors.Length - 1];
            }
            else
            {
                zoomComboBox.Text = value;
            }

            this.zoomComboBox.SelectedIndexChanged += new EventHandler(this.zoomComboBox_SelectedIndexChanged);
        }

        private void SetZoomImage(float newZoom)
        {
            viewerMain.ImageSizeMode = SizeMode.Scrollable;
            viewerMain.ZoomFactor = newZoom;
        }

        private void SetFitImage()
        {
            viewerMain.ImageSizeMode = SizeMode.RatioStretch;
            viewerMain.ZoomFactor = 1;
        }

        private bool IsFitImage()
        {
            return viewerMain.ImageSizeMode == SizeMode.RatioStretch;
        }

        private void SetTrackBarValue(int value)
        {
            zoomToolStripTrackBar.ValueChanged -= new EventHandler(this.zoomToolStripTrackBar_ValueChanged);
            zoomToolStripTrackBar.Value = value;
            zoomToolStripTrackBar.ValueChanged += new EventHandler(this.zoomToolStripTrackBar_ValueChanged);
        }

        private void SetScale()
        {
            double scale = viewerMain.ZoomFactor;

            int index = 0;
            foreach (double dValue in zoomFactor)
            {
                index++;
                if (scale <= dValue)
                {
                    scale = dValue;
                    break;
                }
            }

            if (index < zoomToolStripTrackBar.CrystalTrackBar.Minimum)
                SetTrackBarValue(zoomToolStripTrackBar.CrystalTrackBar.Minimum);
            else
                if (index > zoomToolStripTrackBar.CrystalTrackBar.Maximum)
                    SetTrackBarValue(zoomToolStripTrackBar.CrystalTrackBar.Maximum);
                else
                    SetTrackBarValue(index);

            if (!IsFitImage())
            {
                int wholeScale = Convert.ToInt32(scale * 100);
                string comboScale = wholeScale.ToString();
                comboScale += "%";
                SetZoomComboText(comboScale);
            }
        }

        private void DisplayImage(string imageName)
        {
            Image imageItem = Image.FromFile(imageName);

            if (imageItem != null)
            {
                viewerMain.Image = imageItem;
                SetScale();

                this.Text = "Image Viewer - ";
                this.Text += Path.GetFileName(imageName);

                imageLabel.Text = imageName;
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void openImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Images (*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG";
            openFileDialog1.FileName = _currentFile;
            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                _currentFile = openFileDialog1.FileName;
                DisplayImage(_currentFile);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SetFitImage();

            InitScaleCombo();
            InitTrackBar();
        }

        private void zoomComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string zoomValue = zoomComboBox.SelectedItem.ToString();

            if (zoomValue.Length < 1)
                return;

            if (zoomValue == ZoomFactors[ZoomFactors.Length - 1])
            {
                SetFitImage();
                SetScale();
            }
            else
            {
                float newZoom = zoomFactor[zoomComboBox.SelectedIndex];
                SetZoomImage(newZoom);
                SetTrackBarValue(zoomComboBox.SelectedIndex);
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (zoomToolStripTrackBar.CrystalTrackBar.ProcessKeystroke(keyData))
                return true;

            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void zoomToolStripTrackBar_ValueChanged(object sender, EventArgs e)
        {
            // The scrollZoom changed so reset the zoom factor
            // based on the scrollZoom TrackBar position.
            float newZoom = zoomFactor[zoomToolStripTrackBar.Value];
            SetZoomComboText(ZoomFactors[zoomToolStripTrackBar.Value]);

            SetZoomImage(newZoom);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            SetScale();
        }

        private void toolStrip1_SizeChanged(object sender, EventArgs e)
        {

        }
    }
}

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