Click here to Skip to main content
13,551,001 members
Click here to Skip to main content
Add your own
alternative version


254 bookmarked
Posted 20 Feb 2006
Licenced CPOL

Create a Debugger Visualizer in 10 Lines of Code

, 20 Feb 2006
Rate this:
Please Sign up or sign in to vote.
An article on debugger visualizers in Visual Studio 2005.

Sample Image - SampleImage.jpg


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.

Using the code

Below, you can see the entire code for my image debugger visualizer:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.DebuggerVisualizers;
using System.Windows.Forms;
using System.Drawing;

[assembly: System.Diagnostics.DebuggerVisualizer(
Target = typeof(System.Drawing.Image),
Description = "Image Visualizer")]
namespace ImageVisualizer
    public class DebuggerSide : DialogDebuggerVisualizer
        override protected void Show(IDialogVisualizerService windowService, 
                           IVisualizerObjectProvider objectProvider)
            Image image = (Image)objectProvider.GetObject();
            Form form = new Form();
            form.Text = string.Format("Width: {0}, Height: {1}", 
                                     image.Width, image.Height);
            form.ClientSize = new Size(image.Width, image.Height);
            form.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            PictureBox pictureBox = new PictureBox();
            pictureBox.Image = image;
            pictureBox.Parent = form;
            pictureBox.Dock = DockStyle.Fill;


Let's explain what we have here.

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.


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


About the Author

Tomer Noy
Web Developer
Israel Israel
Tomer has been a software developer since 1997. His main professional interests include .Net and C#. His main unprofessional interests include reading, movies and skiing (whenever he finds the time).

You may also be interested in...


Comments and Discussions

GeneralRe: C++? Pin
jbeaurain27-Sep-06 3:12
memberjbeaurain27-Sep-06 3:12 
GeneralBrilliant Pin
Fabrice Vergnenegre25-Feb-06 0:49
memberFabrice Vergnenegre25-Feb-06 0:49 
GeneralFor Regular Expressions, too Pin
Uwe Keim22-Feb-06 18:10
sitebuilderUwe Keim22-Feb-06 18:10 
GeneralRe: For Regular Expressions, too Pin
Greeeg4-Mar-06 9:38
memberGreeeg4-Mar-06 9:38 
Generalyou rock !! :) Pin
MANSATAN22-Feb-06 11:12
memberMANSATAN22-Feb-06 11:12 
GeneralCool stuff Pin
ivix4u22-Feb-06 0:21
memberivix4u22-Feb-06 0:21 
GeneralGreat information! Pin
Douglas Troy21-Feb-06 4:39
memberDouglas Troy21-Feb-06 4:39 
GeneralVisualizer for XML data Pin
NetronProject21-Feb-06 2:51
memberNetronProject21-Feb-06 2:51 
Nice article, thanks. Roll eyes | :rolleyes:

You can find also a nice Xml debugger visualizer here[^].

I thought about it,
I stood up
and I did it.
The Netron Project

Generalgreat tip! Pin
Javier Gutierrez21-Feb-06 0:09
memberJavier Gutierrez21-Feb-06 0:09 
AnswerRe: great tip! Pin
Tomer Noy21-Feb-06 19:58
memberTomer Noy21-Feb-06 19:58 
GeneralThanks for all the positive feedback Pin
Tomer Noy20-Feb-06 23:29
memberTomer Noy20-Feb-06 23:29 
GeneralWoha! Pin
peterchen20-Feb-06 19:30
memberpeterchen20-Feb-06 19:30 
GeneralShort and Sweet Pin
oykica20-Feb-06 17:49
memberoykica20-Feb-06 17:49 
GeneralGreat! Pin
Greeeg20-Feb-06 4:55
memberGreeeg20-Feb-06 4:55 
GeneralVery slick Pin
yrodrigu20-Feb-06 4:54
memberyrodrigu20-Feb-06 4:54 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04-2016 | 2.8.180515.1 | Last Updated 20 Feb 2006
Article Copyright 2006 by Tomer Noy
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid