WinForms: Load a TIFF image into an image viewer and PictureBox.

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

Moderator: Alex

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

WinForms: Load a TIFF image into an image viewer and PictureBox.

Post by Alex » Wed Feb 15, 2017 12:12 pm

This topic contains a code snippet that shows how to load a TIFF image into an image viewer and PictureBox.

Code: Select all

using System;
using System.Drawing;
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();

            thumbnailViewer.FocusedIndexChanged +=
                new EventHandler<FocusedIndexChangedEventArgs>(thumbnailViewer_FocusedIndexChanged);

            pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
        }



        /// <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 (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                thumbnailViewer.Images.ClearAndDisposeItems();

                try
                {
                    thumbnailViewer.Images.Add(openFileDialog.FileName);
                }
                catch (Exception exc)
                {
                    MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        /// <summary>
        /// Image is loaded in the thumbnail viewer.
        /// </summary>
        private void thumbnailViewer_FocusedIndexChanged(
            object sender,
            FocusedIndexChangedEventArgs e)
        {
            ThumbnailViewer viewer = (ThumbnailViewer)sender;
            VintasoftImage image = null;

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

            ShowImageInImageViewer(image);
            ShowImageInPictureBox(image);
        }

        /// <summary>
        /// Shows the image in ImageViewer.
        /// </summary>
        /// <param name="image">The image.</param>
        private void ShowImageInImageViewer(VintasoftImage image)
        {
            imageViewer.Image = image;
        }

        /// <summary>
        /// Shows the image in PictureBox.
        /// </summary>
        /// <param name="image">The image.</param>
        private void ShowImageInPictureBox(VintasoftImage image)
        {
            Image newBitmap = null;
            try
            {
                if (image != null)
                    // create bitmap
                    newBitmap = image.GetAsBitmap();
            }
            catch
            {
            }

            // get the previous bitmap
            Image previousBitmap = pictureBox.Image;

            // load new bitmap in PictureBox
            pictureBox.Image = newBitmap;

            // if previous bitmap exists
            if (previousBitmap != null)
                // dispose the previous bitmap
                previousBitmap.Dispose();
        }

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

            base.OnClosed(e);
        }

    }
}

Source codes of WinForms application can be downloaded here.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest