Introduction

Visual Studio 2005 shipped with a very nice feature called debugger visualizers. In accordance with their names, debugger visualizers allow you to visually view useful information about objects during debug. Try placing a breakpoint in an application with a DataSet object and hover your mouse over its variable. You get a tooltip with various information and a little icon with a magnifying glass. Click that icon and you get a form showing the underlying data of the DataSet object.

It turns out that the Visual Studio team has made it really easy to add this functionality to other data types. In this article, I will show how to create a debugger visualizer to view Image objects.

This visualizer is extremely helpful for anyone writing an application that manipulates images. You can place a breakpoint at any place in your algorithm and see what the image looks like at that time.

I created a class library project, added a new class and inherited it from DialogDebuggerVisualizer (placed in the Microsoft.VisualStudio.DebuggerVisualizers namespace). I added the DebuggerVisualizer attribute to the assembly so that the Visual Studio IDE will know how to use this visualizer. The customizable parameters are Target (the type to show the visualizer for) and Description.

Next, I overrode the protected method Show. This method gives us two parameters: objectProvider holds a serialized copy of the debugged object we wish to view, windowService allows us to show a dialog under the context of the IDE. In this method, I dynamically created a form with a picture box and loaded the image into it. Of course, you can add a form class to the project and create the form in design time, but for simplicity, I chose the dynamic way. As you can see, 10 lines of code is all it took (excluding attributes and boiler plate code).

Deploying the Assembly

In order for Visual Studio to use our debugger visualizer, we must drop the DLL in <Visual Studio Install Dir>\Common7\Packages\Debugger\Visualizers.

Points of Interest

The type handled by the debugger visualizer must be serializable. This is necessary because the objectProvider parameter gives you a serializable copy of the object.

It is possible to have your debugger visualizer manipulate its copy and save the changes to the debugged object. Look into the ReplaceData and ReplaceObject methods in the objectProvider parameter.

You can create multiple debugger visualizers for a single type. You choose the visualizer you want by expanding the dropdown menu beside the magnifying glass icon.

There is another way to specify which debugger visualizer will be used with a type. Just add the DebuggerVisualizer attribute to the class you want to show in the visualizer and pass the visualizer type as parameter. This way you don't have to specify at the visualizer level all the classes that it will handle. However, this means that you must be the one coding the class handled by the visualizer, and that is clearly not the case with the Image class.

You can experiment with the Debugger Visualizer template that appears in the Add New Item dialog. With this, you can quickly have a class with the necessary using statements, inheritance, and overrides.

VS2008 has a new DebuggerVisualizers Assembly. Just replace the reference to Microsoft.VisualStudio.DebuggerVisualizers version 8.0.0.0 by the one for version 9.0.0.0. and rebuild your Visualizer code. Then it works!