How to: Use Deep Zoom in Silverlight

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Deep Zoom enables you to zoom and pan high-resolution images rapidly. Deep Zoom achieves this by using multi-resolution images. This topic shows you how to create a very simple Silverlight application that uses Deep Zoom.

To view a running sample of this application, click the following link.

Run this sample

Creating a Deep Zoom Image

A Deep Zoom image is composed of tiles of JPEG or PNG images at different resolutions that make up an image pyramid. The best way to create Deep Zoom images is to use a tool like Deep Zoom Composer.

To create a Deep Zoom image

  1. Download and install Deep Zoom Composer.

  2. Start Deep Zoom Composer and create a new project.

  3. In the Import workspace, click Add image and add a high-resolution image to the project.

  4. Click the Compose workspace.

  5. On the Images tab, drag the image onto the artboard.

  6. Size the image as needed.

  7. Click the Export workspace.

  8. Click the Custom tab.

  9. In the Output type section, select Silverlight Deep Zoom.

  10. In the Name box, type dzc_output.

  11. In the Location box, specify a location.

  12. In the Image settings section, select Export as a composition (single image).

  13. Leave the other settings as their defaults.

  14. Click the Export button to export the Deep Zoom image files.

Loading a Deep Zoom Image

Once you have a Deep Zoom image, you can use the MultiScaleImage control to load it.

To load a Deep Zoom image

  1. In Visual Studio, create a new Silverlight Application project in Visual Basic or C#.

  2. If you selected the Host the Silverlight application in a new Web site option, while creating your Silverlight Application, open the <ApplicationName>.Web\ClientBin folder for the Silverlight application in Windows Explorer. If you did not select the Host the Silverlight application in a new Web site option, while creating your Silverlight Application, open the \Bin\Debug folder for the Silverlight application in Windows Explorer.

  3. Create a new folder named Source.

  4. In the Exported Data\dzc_output\GeneratedImages folder created by Deep Zoom Composer, copy the generated dzc_output.xml file and the dzc_output_files folder to the Source folder.

  5. In Visual Studio, open MainPage.xaml.

  6. Within the Grid element, add the following MultiScaleImage element.

    <MultiScaleImage x:Name="deepZoomObject" Source="source/dzc_output.xml" />
    
  7. Build and run the application.

    You should see Deep Zoom image fill the entire browser window.

  8. Refresh the browser.

    Notice that the image is initially blurry and then become sharp.

Adding Interactivity to a Deep Zoom Image

After you load a Deep Zoom image, the user cannot yet interact with the image. To enable interaction, you need to handle MultiScaleImage events and use code to provide the zooming and panning functionality.

To add interactivity to a Deep Zoom image

  1. In Visual Studio, open MainPage.xaml.

  2. Within the Grid element, update the MultiScaleImage element with the MouseEnter and MouseLeave properties.

    <MultiScaleImage x:Name="deepZoomObject" Source="source/dzc_output.xml" 
     MouseEnter="DeepZoomObject_MouseEnter"
     MouseLeave="DeepZoomObject_MouseLeave"/>
    
  3. Open MainPage.xaml.vb or MainPage.xaml.cs.

  4. Add the following code add interactivity to the Deep Zoom image.

    Private Sub DeepZoomObject_MouseEnter(ByVal sender As Object, ByVal e As MouseEventArgs)
        ' The ZoomAboutLogicalPoint method allows you to zoom and pan
        ' in the same step. The first parameter is the zoom (3x) and the
        ' third and fourth parameters are the respective x and y coordinates
        ' of the logical point to zoom around.
        Me.deepZoomObject.ZoomAboutLogicalPoint(3, 0.5, 0.5)
    End Sub
    
    Private Sub DeepZoomObject_MouseLeave(ByVal sender As Object, ByVal e As MouseEventArgs)
        Dim zoom As Double = 1
        zoom = (zoom / 3)
        ' This time the zoom is reversed (1/3) although the pan 
        ' remains the same - zoom back out from the middle.
        Me.deepZoomObject.ZoomAboutLogicalPoint(zoom, 0.5, 0.5)
    End Sub
    
    private void DeepZoomObject_MouseEnter(object sender, MouseEventArgs e)
    {
    
        // The ZoomAboutLogicalPoint method allows you to zoom and pan
        // in the same step. The first parameter is the zoom (3x) and the
        // third and fourth parameters are the respective x and y coordinates
        // of the logical point to zoom around.
        this.deepZoomObject.ZoomAboutLogicalPoint(3, .5, .5);
    }
    
    private void DeepZoomObject_MouseLeave(object sender, MouseEventArgs e)
    {
        double zoom = 1;
        zoom = zoom / 3;
        // This time the zoom is reversed (1/3) although the pan 
        // remains the same - zoom back out from the middle.
        this.deepZoomObject.ZoomAboutLogicalPoint(zoom, .5, .5);
    }
    
  5. Build and run the application.

    You should see Deep Zoom image fill the entire browser window.

  6. Move the mouse pointer over the image to zoom in.

  7. Move the mouse pointer outside the image to zoom out.

See Also

Concepts