WinForms: Get information about thumbnail in the thumbnail viewer.

Code samples for VintaSoft Imaging .NET SDK. Here you can request a code sample.

Moderator: Alex

Post Reply
Alex
Site Admin
Posts: 2300
Joined: Thu Jul 10, 2008 2:21 pm

WinForms: Get information about thumbnail in the thumbnail viewer.

Post by Alex »

This topic contains C# code snippet that shows how to get information about thumbnail image (filename and page index) in the thumbnail viewer.

Code: Select all

using System;
using System.Text;
using System.Windows.Forms;

using Vintasoft.Imaging;
using Vintasoft.Imaging.UI;


namespace WindowsFormsApplication1
{
    /// <summary>
    /// Main form.
    /// </summary>
    public partial class MainForm : Form
    {

        /// <summary>
        /// Initializes a new instance of the <see cref="MainForm"/> class.
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            thumbnailViewer1.FocusedIndexChanged +=
                new EventHandler<FocusedIndexChangedEventArgs>(thumbnailViewer1_FocusedIndexChanged);
        }
        

       
        /// <summary>
        /// Clears image collection of thumbnail viewer and
        /// adds image(s) to an image collection of thumbnail viewer.
        /// </summary>
        private void openButton_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                thumbnailViewer1.Images.ClearAndDisposeItems();

                foreach (string fileName in openFileDialog1.FileNames)
                {
                    try
                    {
                        thumbnailViewer1.Images.Add(fileName);
                    }
                    catch (Exception exc)
                    {
                        MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        /// <summary>
        /// Focused image is changed.
        /// </summary>
        private void thumbnailViewer1_FocusedIndexChanged(object sender, FocusedIndexChangedEventArgs e)
        {
            ThumbnailViewer viewer = (ThumbnailViewer)sender;
            VintasoftImage image = null;

            if (e.FocusedIndex != -1)
                image = viewer.Images[e.FocusedIndex];

            ShowImageInfo(image);
        }

        /// <summary>
        /// Shows the image information.
        /// </summary>
        /// <param name="image">The image.</param>
        private void ShowImageInfo(VintasoftImage image)
        {
            if (image == null)
                imageInfoTextBox.Text = string.Empty;
            else
            {
                ImageSourceInfo sourceInfo = image.SourceInfo;

                StringBuilder builder = new StringBuilder();

                builder.AppendFormat("File: {0}", sourceInfo.Filename);
                builder.AppendLine();

                if (sourceInfo.PageCount > 1)
                {
                    builder.AppendFormat("Page: {0}/{1}", sourceInfo.PageIndex + 1, sourceInfo.PageCount);
                    builder.AppendLine();
                }

                imageInfoTextBox.Text = builder.ToString();
            }
        }

        /// <summary>
        /// Raises the Closed event.
        /// </summary>
        protected override void OnClosed(EventArgs e)
        {
            thumbnailViewer1.Images.ClearAndDisposeItems();

            base.OnClosed(e);
        }

    }
}

Source codes of WinForms application for VintaSoft Imaging .NET SDK 12 can be downloaded here.
Post Reply