Click here to Skip to main content
15,881,872 members
Articles / Programming Languages / C#

Silverlight Database Deep Zoom

Rate me:
Please Sign up or sign in to vote.
4.96/5 (40 votes)
26 Mar 2009CPOL11 min read 122.3K   1.7K   84  
The article describes how to create a Deep Zoom image and store the tiles in a database, and how to read the image from the database and display it in the browser.
//
// This source code is licensed for commercial and non-commercial use under the 
// Code Project Open License (CPOL) 1.02  http://www.codeproject.com/info/cpol10.aspx
// Developer: Joerg Lang (lang.joerg@gmail.com)
//
using System;
using System.Data.OleDb;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using DatabaseDeepZoom.Properties;
using DbDzComposer;

namespace DatabaseDeepZoom
{
    public partial class MainForm : Form
    {
        private readonly DeepZoomGenerator dz;

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

            // Create the DeepZoom generator object and pass it the Access persister object
            dz = new DeepZoomGenerator {DatabasePersister = GetAccessDb()};
            // Attach the event handler, so we can watch the creation progress
            dz.CreationProgress += dz_CreationProgress;
        }


        /// <summary>
        /// Handles the CreationProgress event of the DeepZoomCreator object.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="DbDzComposer.DeepZoomCreationProgressEventArgs"/> instance containing the event data.</param>
        private void dz_CreationProgress(object sender, DeepZoomCreationProgressEventArgs e)
        {
            progressBar.Value = e.CreationProgress;
            Application.DoEvents();
        }

        /// <summary>
        /// Handles the Click event of the cmdCreate control.
        /// Creates the tiles for the selected image
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cmdCreate_Click(object sender, EventArgs e)
        {
            // Check if input exists
            if (txtName.Text.Length == 0 || txtFile.Text.Length == 0)
                return;

            // If the file exists start processing
            if (File.Exists(txtFile.Text))
            {
                // Init the progress bar
                this.Cursor = Cursors.WaitCursor;
                progressBar.Value = 0;
                progressBar.Visible = true;

                // Generate the tile
                dz.GenerateFromFile(txtFile.Text, txtName.Text, true, true);

                // Reset the progress bar
                progressBar.Visible = false;
                this.Cursor = Cursors.Default;
            }
        }

        /// <summary>
        /// Handles the Click event of the cmdFileOpen control.
        /// Opens the file open dialog and lets the user choose a file
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cmdFileOpen_Click(object sender, EventArgs e)
        {
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                txtFile.Text = ofd.FileName;
                txtName.Text = new FileInfo(ofd.FileName).Name;

                // Assign the image to the image preview 
                previewImage.Image = new Bitmap(ofd.FileName);
                previewImage.SizeMode = PictureBoxSizeMode.Zoom;
            }
        }


        /// <summary>
        /// Returns DeepZoom Database data access object for the Access database.
        /// </summary>
        /// <returns></returns>
        private DzDbAccess GetAccessDb()
        {
            // Read connection string from the property settings and open the database
            string connectString = Settings.Default.ConnectionString;
            var cn = new OleDbConnection(connectString);
            cn.Open();

            // Create the MS Access database object and pass it the connection object.
            var accessPersistence = new DzDbAccess();
            accessPersistence.DbConnection = cn;
            return accessPersistence;
        }
    }
}

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 Code Project Open License (CPOL)


Written By
CEO
Switzerland Switzerland
I have my own software company called Evelix (www.evelix.ch). The company is located in Liestal, Switzerland. I develop software for the web and the desktop. Every now and then I give computer classes in a learning institution.

I was born in 1966, am married and have one kid. Hobbies are Fasnacht (www.bmg.bs), skiing and of course computers.

Actually I studied mechanical engineering and have a bachelors degree in it, but computers interested me since I had a Commodore C128. In the meantime my mobile has a thousand times more memory than my computers back then. First I started programming in Basic. After that I did use Pascal for a while, but the real (commercial) programming started with VB3. Now I do programming in C# and sometimes still in VB6 when I have to support an older application.

Currently I'm working towards my Microsoft Certified Trainer status.

Comments and Discussions