API Quickstarts - Silverlight
The fastest way to use the API from Silverlight is to grab our Silverlight helper library. This library abstracts away the logic of making HTTP requests and parsing JSON responses.
To use the library, just add a reference to the ZoomitWebApi.dll assembly from within your Silverlight project, then include its namespace at the top of your code-behind file:
using Microsoft.Zoomit;
Now you have access to the Zoomit class,
which provides easy methods for asynchronously fetching content info:
public static class Zoomit
{
public static IAsyncResult BeginGetContentInfo(
string id, AsyncCallback callback, object state);
public static IAsyncResult BeginGetContentInfo(
Uri uri, AsyncCallback callback, object state);
public static ContentInfo EndGetContentInfo(
IAsyncResult asyncResult);
public static bool TryEndGetContentInfo(
IAsyncResult asyncResult, out ContentInfo content);
}
To convert your content (image, webpage, PDF, SVG, ...) to a Deep Zoom
Image, call the asynchronous
BeginGetContentInfo(Uri, AsyncCallback, object)
method with your content’s URL, along with a callback function to be
called when the response is returned and optionally an object to track
state asynchronously (we recommend simply passing the URL again, but
you can also simply pass null).
Note that the URL you send must be an absolute URL to content on the web. This means you can't send a URL to content embedded as a resource within your Silverlight app.
When your callback is called, call the
EndGetContentInfo(IAsyncResult) method
with the result that you’re given. If this call throws an exception,
your URL was malformed or the service is down. Otherwise, this call will
return a ContentInfo object for your content.
public class ContentInfo
{
public string Id { get; }
public Uri Uri { get; }
public bool Ready { get; }
public bool Failed { get; }
public double Progress { get; }
public Uri ViewUri { get; }
public string EmbedHtml { get; }
public string Title { get; }
public string AttributionText { get; }
public Uri AttributionUri { get; }
public DziInfo Dzi { get; }
}
You can check the boolean Ready property on the
ContentInfo to see if your DZI is ready. If it is, the
Dzi property returns a DziInfo object with
the DZI's info, including its URL as the Uri property.
public class DziInfo
{
public Uri Uri { get; }
public int Width { get; }
public int Height { get; }
public int TileSize { get; }
public int TileOverlap { get; }
public string TileFormat { get; }
}
If the DZI isn't ready, check the boolean Failed property
to see if we had a problem. Otherwise, the Progress
property returns a double between 0 and 1 that tells you how far along
we are. You may choose to keep polling in this case.
Once you have the URL to a DZI, you can plug it into a
<MultiScaleImage>
element to display the image. You do this by settings its
Source property to a
DeepZoomImageTileSource instance
constructed from the DZI's URL.
One important subtlety to be aware of is that the callback function is
raised on a separate thread. If you want to modify the UI during this
callback — for example, to set the Source on a
MultiScaleImage — you'll need to queue your code to
run on the UI thread. You can do this via the
BeginInvoke() method of the
Dispatcher class.
The sample UserControl code below puts it all together!
protected MultiScaleImage msi;
public MainPage()
{
InitializeComponent();
Uri uri = new Uri("http://example.com/");
Zoomit.BeginGetContentInfo(uri, ZoomitCallback, uri);
}
private void ZoomitCallback(IAsyncResult asyncResult)
{
Uri uri = asyncResult.AsyncState as Uri;
base.Dispatcher.BeginInvoke(() =>
{
try
{
ContentInfo content =
Zoomit.EndGetContentInfo(asyncResult);
DziInfo dzi = content.Dzi;
if (content.Ready && dzi != null)
msi.Source = new DeepZoomImageTileSource(dzi.Uri);
else if (content.Failed)
ShowMessage("{0} failed to convert.", uri);
else
ShowMessage("{0} is at {1}% progress.",
uri, Math.Round(100 * content.Progress));
}
catch (Exception e)
{
ShowMessage(
"{0} was rejected! Error message:\n{1}",
uri, e.Message);
}
});
}
private void ShowMessage(string message, params object[] args)
{
MessageBox.Show(string.Format(message, args));
}
