Click here to Skip to main content
15,895,142 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 123.4K   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.
<%@ WebHandler Language="C#" Class="TileHandler" %>
// 
// 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.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web;
using DbDzComposer;

public class TileHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {

        if (context.Request.QueryString.HasKeys())
        {
            // Being lazy...
            int imageId = int.Parse(context.Request.QueryString.Get("imageId"));
            int tileLevel = int.Parse(context.Request.QueryString.Get("tileLevel"));
            int tilePosX = int.Parse(context.Request.QueryString.Get("tilePositionX"));
            int tilePosY = int.Parse(context.Request.QueryString.Get("tilePositionY"));
            string contentType = context.Request.QueryString.Get("contentType");

            context.Response.ContentType = contentType;
            System.Drawing.Bitmap bitmap = GetTile(imageId, tileLevel, tilePosX, tilePosY);


            if (bitmap != null)
            {
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                if (contentType.Contains("png"))
                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                else
                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                ms.WriteTo(context.Response.OutputStream);
            }
        }
    }

    /// <summary>
    /// Returns a tile from the database
    /// </summary>
    /// <param name="imageId">The id of the image</param>
    /// <param name="level">The requested level</param>
    /// <param name="x">The x coordinate</param>
    /// <param name="y">The y coordinate</param>
    /// <returns></returns>
    private Bitmap GetTile(int imageId, int level, int x, int y)
    {
        try
        {
            DzDbAccess accessDb = DeepZoomSilverlightWeb.Helper.GetAccessDb();
            Bitmap bmp = accessDb.GetImageTile(imageId, level, x, y);
            accessDb.DbConnection.Close();
            return bmp;
        }
        catch (Exception)
        {
            return null;
        }

    }

    /// <summary>
    /// Return true if the handler should stay in memory.
    /// </summary>
    public bool IsReusable
    {
        get
        {
            // To enable pooling, return true here.
            // This keeps the handler in memory.
            return true;
        }
    }



}

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