Click here to Skip to main content
Click here to Skip to main content

Silverlight Database Deep Zoom

By , 26 Mar 2009
Rate this:
Please Sign up or sign in to vote.
Sample Image


This code has grown out of two other projects/articles: the DecosDeepZoom by Berend Engelbrecht, and an article from Mike Ormond about the MultiTileSource. I used code from Berend Engelbrecht to generate the tiles.


This article shows how to store a Deep Zoom image in a database, and how to retrieve the data for display in a Silverlight app. For simplicity, I use a Microsoft Access database, but the code can easily be adapted for Microsoft SQL Server or Oracle. As simple as this might sound, it takes quite a lot to make it work. The article covers the following ground:

  • How to create a Deep Zoom image from a single (large) image. (Or more exactly, how to create the tiles that make up a Deep Zoom image.)
  • How to store the created tiles in a database.
  • The database model that is required.
  • How to read the tiles from the database and pass it to the MultiScaleImage control.

Along the journey, you will see some other useful stuff, for example, how to store a bitmap in an Access database, or how you can use LINQ to convert a DataSet into a list of objects that can be the data source for a Silverlight control.


I have always been fascinated by how you can zoom in endlessly in Google Earth. When Silverlight came with the Deep Zoom feature, I was quite excited. However, I found it awkward that you would have to use a separate tool (Deep Zoom Composer) to create such an image. I also found the files that it generates on the file system impractical. It certainly works if you have to deploy one or two Deep Zoom images onto a website. But what do you do, if you have to create thousands of them?

I work for a company called Scope Solutions, located in Basle, Switzerland. We do software for "historical" archives, which means that we mainly manage meta data about the archival holding, which includes images, maps, and building plans. Of course, the archives scan their interesting items to make them available to the public. And, if an archivist is going to scan an old map, he will do that in the highest possible quality so he has a "backup" that matches the original as close as possible. Just in case the original is damaged/lost. This can lead to very large files and bitmaps. For example, a bitmap with 13722 x 9737 pixels is not uncommon. Such a bitmap uses, depending on the format, up to 400 MB on the disk. And having said this, it should be no surprise to hear that some archives have map collections with ten thousands of maps or over a quarter million images.

Wouldn't it be cool if hobby archivists from all over the world could view "deep zoomable" images of these historical maps? Using the Deep Zoom Composer and a file based system is surely not the way to go. That is why I investigated and wanted to find out how to store the images in the database.

Using the Code

The demo presented here consists of two applications:

  • A Windows Forms application that generates the tiles for a Deep Zoom image and stores them in a Microsoft Access database.
  • A Silverlight web application that displays the images from the database.

Both applications are contained in the same solution. To compile and run the solutions, you will need VS 2008 SP 1 (required for Silverlight development) with .NET 3.5. (for simplicity, I decided to wrap both applications in one solution). For that reason, you will have to change the startup project, depending on which app you want to run.

Important: Before you can run the solution on your machine, you have to change the connection string in the configuration to match your path location. The connection string for the Windows Forms application can be changed in the settings file. The connection string for the ASP.NET application can be changed in the web.config file. The (empty) sample database is located in the App_Data directory of the ASP.NET application.

The four projects found within the solution are:

  • DatabaseDeepZoom: A simple Windows Forms application that provides the UI for creating the tiles of a Deep Zoom image.
  • DbDzComposer: This library is used by both projects, and does most of the work. It contains a class for generating the tiles, and a class for storing and retrieving the data from the database.
  • DeepZoomSilverlightProject: The Silverlight app that displays the images. This is mainly the default project that you get when you use Deep Zoom Composer. It has been extended by a list for displaying the thumbnails to the right of the Deep Zoom image.
  • DeepZoomSilverlightWeb: The ASP.NET app that hosts the Silverlight app. It is an extended version of the default project that Deep Zoom Composer creates. Two HttpHandlers have been added that return the tiles and the thumbnails that the Silverlight app requires, and a WCF Web Service for returning the data about the images.

In this article, I have omitted most of the code comments. The code you can download is better documented.

DatabaseDeepZoom Project

This simple app generated the tiles for a Deep Zoom image. The user provides a filename and a name for the image. The code calls the function GenerateFromFile from the DeepZoomComposer class that is part of the DbDzComposer library. The DeepZoomGenerator class is instantiated with the default constructor which instructs the class to create 256 x 256 pixel tiles and save them as 24bit color JPEG tiles with a quality of 90. You can change that behavior using the second constructor or by setting properties.

The most important fact is that we also instantiate the database access class. This class inherits the interface from an abstract base class that defines the basic interface. Using that interface, it is easy to write an additional provider that would store the data in an Oracle or SQL Server database instead of the Access class we use here.

public MainForm()

    dz = new DeepZoomGenerator {DatabasePersister = GetAccessDb()};
    dz.CreationProgress += dz_CreationProgress;

private DzDbAccess GetAccessDb()
    string connectString = Settings.Default.ConnectionString;
    var cn = new OleDbConnection(connectString);

    var accessPersistence = new DzDbAccess();
    accessPersistence.DbConnection = cn;
    return accessPersistence;

The rest of the code just deals with opening the image file, and some other UI related stuff like progress bars.

DbDzComposer Project

This library is used by the Windows Forms application and the ASP.NET application. It contains classes for creating the Deep Zoom image as well as a class for storing the generated tiles in a Microsoft Access database. The DzDbAccess class inherits from an abstract interface that defines the methods for storing and retrieving the images from the database. I have chosen to create this interface so I could easily add another database storage provider for a different database system. As you can see from the code, the interface is quite simple. It provides a method to save the information about an image and a method to save the individual tiles. Then, it has methods to return a list with the information about the images stored in the database, and methods to return a thumbnail preview and the individual tiles back to the caller. And, of course, it needs a property with the information about the database connection.

namespace DbDzComposer
    public abstract class IDzDbPersistance
        public abstract int SaveImageInfo(string imageName, int width, int height,
            int tileSize, int overlap, string mimeType, Bitmap thumbnail);
        public abstract List<ImageInfo> GetImageInfo(Uri fromUri);
        public abstract void SaveImageTile(int imageId, int level, int x, int y,
            Bitmap bitmap);
        public abstract Bitmap GetImageTile(int imageId, int level, int x, int y);
        public abstract Bitmap GetThumbnail(int imageId);
        public abstract IDbConnection DbConnection { get; set; }

    public class ImageInfo
        public int ImageId { get; set; }
        public string ImageName { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
        public int TileSize { get; set; }
        public int Overlap { get; set; }
        public string MimeType { get; set; }
        public string ThumbnailUrl { get; set; }

The concrete database persister class is used by the DeepZoomGenerator class for storing the images. This DeepZoomGenerator class is based on the work by Berend Engelbrecht, and is a stripped down version, as my goal was to create tiles for single images only. This class takes a large image, and creates the tiles required for creating the Deep Zoom image. It does, however, not support the more advanced stuff like image collections that the Deep Zoom technology provides and that Berend in his original class provided.

DeepZoomSilverlight Project

This Silverlight project consists of a single page that displays the Deep Zoom image and displays the available images from the database in a listbox. The question is, of course, how does the MultiScaleImage control display the tiles that are stored in the database? Usually, one would assign the Source property of that control the URI to an XML file, e.g., something like msi.Source="../GeneratedImages/dzc_output.xml". But, this property actually is of type MultiScaleTileSource.

So, what we have to do is create a class that inherits from MultiScaleTileSource and implement its interface. The main method of that class is called GetTileLayers, and is called from the MultiScaleImage control for every tile that is needed. Inside that method, you add each tile to a list, but not in the form of a bitmap, but in the form of an URI. The URI itself must then stream back the image, and that is why we use a HttpHandler in the web project.

/// <summary>
/// Gets a collection of the tiles that comprise the Deep Zoom image.
/// </summary>
/// <param name="tileLevel">Level of the tile.</param>
/// <param name="tilePositionX">X-coordinate position of the tile.</param>
/// <param name="tilePositionY">Y-coordinate position of the tile.</param>
/// <param name="tileImageLayerSources">Source of the tile image layer.</param>
protected override void GetTileLayers(int tileLevel, int tilePositionX,
                                      int tilePositionY,
                                      IList<object> tileImageLayerSources)
    string source =
                      "TileHandler.ashx?tileLevel={0}&tilePositionX={1}&" +
                      tileLevel, tilePositionX, tilePositionY, imageId, mimeType);

    var uri = new Uri(HtmlPage.Document.DocumentUri, source);


The HttpHandler is not part of the Silverlight project, but of the ASP.NET application that hosts the Silverlight app. This kind of architecture is required because Silverlight itself is not capable of accessing the database as it runs in a secure sandbox environment.

DeepZoomSilverlight Web Project

This is the ASP.NET application that hosts the Silverlight control. This application also provides the Silverlight control with the data. There are two HttpHandlers in this application, one that returns the bitmap tiles, and one that returns a thumbnail for a specific image. Besides that, there is a WCF service defined that the Silverlight app uses to retrieve the information about the images.

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,

        if (bitmap != null)
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            if (contentType.Contains("png"))
                bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

private Bitmap GetTile(int imageId, int level, int x, int y)
        DzDbAccess accessDb = DeepZoomSilverlightWeb.Helper.GetAccessDb();
        Bitmap bmp = accessDb.GetImageTile(imageId, level, x, y);
        return bmp;
    catch (Exception)
        return null;

Points of Interest

After having given an overview about the different projects involved in this solution and cleared the most important points, I would like to go deeper and give you additional insight on the code.

Deep Zoom File Format

Of course, this article would not be complete without some words about the Deep Zoom file format. But, I am not going to repeat what Microsoft has described in detail in MSDN.

Database Design

One of the questions I had to answer was the database structure that is needed to store the image tiles. It turned out that this is easier than I first thought after looking at all the files the Deep Zoom Composer generates. The database design consists of two tables. One table is needed to store information about the image, most importantly about the width and height, the tile size, and the overlap, as this information is required to correctly initialize the MultiScaleTileSource. The other table stores the generated tiles, and on which level and on which coordinates they are used.

Storing the Bitmaps in the Microsoft Access Database

Even though I do quite a lot of database programming, I'm not storing bitmaps in a BLOB field every day. To achieve the goal, you have to use a parameterized command object. The parameter for the bitmap must be of type OleDbType.LongVarBinary, and you pass this parameter a byte array. To convert the bitmap into a byte array, you will serialize the bitmap into a memory stream using the BinaryFormatter.

public override void SaveImageTile(int imageId, int level, 
                     int x, int y, Bitmap bitmap)
    // Create the SQL
    string sql = "INSERT INTO DeepZoomTile(imageId, [level], x, y,
        bitmap) values (?, ?, ?, ?, ?)";
    var cmd = new OleDbCommand(sql, dbConnection);

    //serialise the image
    byte[] bytes = BitmapToByteArray(bitmap);

    // Add the parameters
    cmd.Parameters.Add("@imageid", OleDbType.Integer).Value = imageId;
    cmd.Parameters.Add("@level", OleDbType.Integer).Value = level;
    cmd.Parameters.Add("@x", OleDbType.Integer).Value = x;
    cmd.Parameters.Add("@y", OleDbType.Integer).Value = y;
    cmd.Parameters.Add("@bitmap", OleDbType.LongVarBinary,
        bytes.Length).Value = bytes;

    // Execute it

/// <summary>
/// Serializes a bitmap to an byte array
/// </summary>
/// <param name="bitmap">The bitmap.</param>
/// <returns></returns>
private static byte[] BitmapToByteArray(Bitmap bitmap)
    var memStream = new MemoryStream();
    var formatter = new BinaryFormatter();
    formatter.Serialize(memStream, bitmap);

    byte[] bytes = memStream.GetBuffer();
    return bytes;

Returning the Information About Images from the Database to the Silverlight Listbox

The web front-end shows a listbox to the right of the Deep Zoom image. This listbox displays data from the database. As Silverlight cannot deal with DataTables or DataSets, we cannot pass one of those objects as we would normally do. Therefore, the database access class returns a list of ImageInfo objects. Here, LINQ does a great job in simplifying the developer's work, once you have understood the syntax that it requires. You can use this example every time you have to return a list of objects from a DataSet. One thing that is special about this method is the passed parameter fromUri that is required to return the ThumbnailUrl. As the Silverlight app can be deployed anywhere on the net, and thus the path to the HttpHandler that returns the thumbnail image changes, I pass in the URI from the calling page. Like that, I am sure that the address for the ThumbnailUrl is always correct.

/// <summary>
/// Gets a list with the information about the images.
/// </summary>
/// <param name="fromUri">The Url from which the call is made.</param>
/// <returns></returns>
public override List<ImageInfo> GetImageInfo(Uri fromUri)
    string sql = "Select * from DeepZoomImage order by imageName";
    var dataAdapter = new OleDbDataAdapter(sql, dbConnection);
    var ds = new DataSet();

    dataAdapter.Fill(ds, "ImageInfo");

    var imageInfos = from image in ds.Tables["ImageInfo"].AsEnumerable()
                     select new ImageInfo
                        ImageId = image.Field<int>("ImageId"),
                        ImageName = image.Field<string>("ImageName"),
                        Height = image.Field<int>("Height"),
                        Width = image.Field<int>("Width"),
                        TileSize = image.Field<int>("TileSize"),
                        Overlap = image.Field<int>("Overlap"),
                        MimeType = image.Field<string>("MimeType"),
                        ThumbnailUrl = new Uri(fromUri,
                               "ThumbnailHandler.ashx?ImageId=" +

    return imageInfos.ToList();

The returned list of ImageInfo objects is then passed to the Silverlight application using a WCF Service. Here is how the Service, that is defined in the ASP.NET application, looks like:

namespace DeepZoomSilverlightWeb
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode =
    public class ImageListService
        /// <summary>
        /// Gets a list of images from the database.
        /// </summary>
        /// <returns>A list of ImageInfo objects</returns>
        public List<ImageInfo> GetImageList()
            DbDzComposer.DzDbAccess accessDb = Helper.GetAccessDb();
            return accessDb.GetImageInfo(HttpContext.Current.Request.Url);

In the Silverlight app that consumes the returned list, we call the GetImageList method from within the constructor of the page. As this is an async call, we bind it in the completed event handler of that method.

First, initialize imageListClient...

public Page()

    // Setting the datasource for the list
    var imageListClient = ServiceClientFactory.GetImageListServiceClient();
    imageListClient.GetImageListCompleted +=
      new EventHandler<DeepZoomSilverlightProject.

Bind the result to the imageList and select the first item in the list...

/// <summary>
/// Handles the GetImageListCompleted event of the imageListCliet control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="DeepZoomSilverlight
///                 Project.ImageListClient.GetImageListCompletedEventArgs"/>
/// instance containing the event data.</param>
void imageListCliet_GetImageListCompleted(object sender,
    DeepZoomSilverlightProject.ImageListClient.GetImageListCompletedEventArgs e)
    // Set the datasource for the image list control
    imageList.ItemsSource = e.Result;

    if (imageList.Items.Count > 0)
        imageList.SelectedIndex = 0;

And, in the SelectionChanged event of the listbox, we pass the MultiScaleImage a MultiScaleTileSource object...

private void imageList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    ImageInfo imageInfo = imageList.SelectedItem as ImageInfo;

    if (imageInfo != null)
        // Setting the source of the multi scale image
        msi.Source = new DeepZoomTileSource(imageInfo.ImageId,
            imageInfo.Width, imageInfo.Height, imageInfo.TileSize,
            imageInfo.Overlap, imageInfo.MimeType);

That's all. I hope you find the code useful and easy to follow. If you have any comments or suggestions, just let me know.


  • 04 February 2009 - Version 1.0 created
  • 25 March 2009 - Article updated


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Joerg Lang
Switzerland Switzerland
I have my own software company called Evelix ( 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 (, 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

QuestionCan't success in Silverlight 4 PinmembereHuaTian24-Sep-12 4:06 
AnswerRe: Can't success in Silverlight 4 PinmemberJoerg Lang24-Sep-12 20:38 
QuestionUsing Ms Access as a Pivot Viewer Collection Pinmembernirajzambad3-Aug-12 22:50 
Question Pinmemberiimagegrapher23-Nov-11 19:07 
QuestionLoad Multi image in msi from database Pinmembervahidbakhtiary2-Aug-11 20:31 
AnswerRe: Load Multi image in msi from database PinmemberJoerg Lang5-Aug-11 3:33 
GeneralRe: Load Multi image in msi from database Pinmembervahidbakhtiary9-Aug-11 0:47 
GeneralRe: Load Multi image in msi from database PinmemberJoerg Lang9-Aug-11 1:10 
GeneralRe: Load Multi image in msi from database Pinmembervahidbakhtiary13-Aug-11 0:34 
GeneralRe: Load Multi image in msi from database PinmemberJoerg Lang18-Aug-11 20:32 
QuestionHow can i use DbDzComposer in silverlight application Pinmemberpouya_devil9-Jul-11 4:39 
AnswerRe: How can i use DbDzComposer in silverlight application PinmemberJoerg Lang9-Jul-11 12:51 
GeneralRe: How can i use DbDzComposer in silverlight application [modified] Pinmemberpouya_devil9-Jul-11 22:20 
GeneralRe: How can i use DbDzComposer in silverlight application Pinmemberpouya_devil10-Jul-11 1:10 
GeneralRe: How can i use DbDzComposer in silverlight application PinmemberJoerg Lang10-Jul-11 11:34 
You should be able to use the EntityFramework without any problems. The database access code is all contained in the class DzDbAccess that inherits from the abstract class IDzDbPersistance.
So what you would have to do is to create a new class (for example DzDbSqlServer) that inherits the same abstract class. You then simply have to implement all the required members, but instead of using OleDb and OleDBCommands you can use the Entity Framework and Linq to store and read the data.
Hope that helps
GeneralRe: How can i use DbDzComposer in silverlight application Pinmemberpouya_devil2-Aug-11 20:25 
GeneralBlack Tile issue PinmemberChris Ivey14-Jul-10 12:07 
QuestionHow to show many Images, not one? Pinmemberbirkmx14-Jun-10 23:31 
GeneralSystem.Web.Silverlight Pinmemberdanix136-Jan-10 9:46 
GeneralQuality is Inferior to Images Generated via DeepZoomTools.dll PinmemberCDCii7-Dec-09 13:16 
QuestionDeepzoom dimensions bug? PinmemberFred van der Meulen29-Oct-09 5:21 
AnswerRe: Deepzoom dimensions bug? PinmemberJoerg Lang1-Nov-09 4:01 
GeneralRe: Deepzoom dimensions bug? Pinmembershaghrack2-Nov-09 3:45 
GeneralRe: Deepzoom dimensions bug? PinmemberFred van der Meulen6-Nov-09 2:26 
GeneralHi Joerg Pinmembermrprata8-Jul-09 17:53 
AnswerRe: Hi Joerg PinmemberJoerg Lang15-Jul-09 10:04 
Generalhave problem with the database connection Pinmembermrprata31-May-09 20:37 
GeneralRe: have problem with the database connection PinmemberJoerg Lang1-Jun-09 20:45 
GeneralRe: have problem with the database connection Pinmembermrprata3-Jun-09 16:32 
GeneralRe: have problem with the database connection PinmemberJoerg Lang8-Jun-09 0:00 
GeneralYou are the Best ! Pinmemberenricoe20-Mar-09 6:43 
GeneralThanks for a great sample Pinmemberanderswq14-Mar-09 12:29 
GeneralUnreachable code Pinmemberanderswq11-Mar-09 7:13 
GeneralRe: Unreachable code PinmemberJoerg Lang12-Mar-09 21:46 
GeneralInteresting.. PinmemberAnil Kr. Sharma13-Feb-09 0:44 
GeneralGreate stuff Pinmemberandrewward11-Feb-09 3:16 
GeneralPretty Good! PinmemberAndries Olivier10-Feb-09 0:15 
GeneralLike the idea PinmemberLigblou9-Feb-09 19:54 
GeneralGreat job PinmemberDaniel Vaughan9-Feb-09 10:25 
GeneralGreat job PinmvpSacha Barber6-Feb-09 0:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140421.2 | Last Updated 26 Mar 2009
Article Copyright 2009 by Joerg Lang
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid