Click here to Skip to main content
16,016,391 members
Articles / Programming Languages / C#
Article

Create a Debugger Visualizer in 10 Lines of Code

Rate me:
Please Sign up or sign in to vote.
4.90/5 (131 votes)
20 Feb 2006CPOL3 min read 281.3K   2.6K   257   51
An article on debugger visualizers in Visual Studio 2005.

Sample Image - SampleImage.jpg

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.

Using the code

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

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

[assembly: System.Diagnostics.DebuggerVisualizer(
typeof(ImageVisualizer.DebuggerSide),
typeof(VisualizerObjectSource),
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;

            windowService.ShowDialog(form);
        }
    }
}

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.

License

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


Written By
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).

Comments and Discussions

 
BugImage width 2x1: Unable to close window Pin
Rul Aman27-Jun-13 8:35
Rul Aman27-Jun-13 8:35 
GeneralMy vote of 5 Pin
Pankaj Chamria27-Jan-12 2:35
Pankaj Chamria27-Jan-12 2:35 
GeneralMy vote of 5 Pin
S.P.Tiwari12-Dec-11 1:16
professionalS.P.Tiwari12-Dec-11 1:16 
GeneralMy vote of 5 Pin
hoernchenmeister15-Apr-11 3:53
hoernchenmeister15-Apr-11 3:53 
GeneralMy vote of 5 Pin
v# guy9-Oct-10 20:09
v# guy9-Oct-10 20:09 
GeneralRe: My vote of 5 Pin
Tomer Noy16-Nov-10 4:38
Tomer Noy16-Nov-10 4:38 
GeneralAwesome Pin
Xmen Real 1-May-10 18:51
professional Xmen Real 1-May-10 18:51 
GeneralAddition to ImageVisualizer Pin
djb0331754-Mar-10 12:10
djb0331754-Mar-10 12:10 
GeneralVS2008 Works Pin
Calvin B11-Aug-09 7:40
Calvin B11-Aug-09 7:40 
General2008 sucks Pin
DotNetWise24-Mar-09 7:44
DotNetWise24-Mar-09 7:44 
GeneralVS 2008 Pin
dfsdfsdfoyuti28-Feb-09 7:00
dfsdfsdfoyuti28-Feb-09 7:00 
AnswerRe: VS 2008 - RESOLVED! Pin
boops boops14-Apr-09 9:04
boops boops14-Apr-09 9:04 
Generalusing ImageVisualizer with small images Pin
Marek Grzenkowicz31-Aug-07 2:40
Marek Grzenkowicz31-Aug-07 2:40 
QuestionHDC Visualizer? Pin
David Howe21-May-07 22:28
David Howe21-May-07 22:28 
QuestionArray Visualizer ? Pin
Abdullah_m20-Mar-07 23:14
Abdullah_m20-Mar-07 23:14 
AnswerRe: Array Visualizer ? Pin
Tomer Noy24-Mar-07 22:22
Tomer Noy24-Mar-07 22:22 
QuestionVisualizer for C++ types ? Pin
Vertilka The Blue20-Nov-06 21:56
Vertilka The Blue20-Nov-06 21:56 
AnswerRe: Visualizer for C++ types ? Pin
Tomer Noy26-Nov-06 2:23
Tomer Noy26-Nov-06 2:23 
AnswerRe: Visualizer for C++ types ? Pin
Rolf Kristensen5-Dec-06 8:50
Rolf Kristensen5-Dec-06 8:50 
QuestionIs there a visualizer in VS 2003? Pin
Micha3ldg7-Apr-06 1:14
Micha3ldg7-Apr-06 1:14 
AnswerRe: Is there a visualizer in VS 2003? Pin
KiwiPiet1-Aug-06 13:48
KiwiPiet1-Aug-06 13:48 
QuestionDebuggerVisualizer ? Pin
Rdoerrer28-Mar-06 20:21
Rdoerrer28-Mar-06 20:21 
AnswerRe: DebuggerVisualizer ? Pin
Eric_Franz30-Mar-06 8:12
Eric_Franz30-Mar-06 8:12 
QuestionAuto deploy, and refresh visualizer after build Pin
lp@gmail27-Mar-06 22:04
lp@gmail27-Mar-06 22:04 
GeneralRe: Auto deploy, and refresh visualizer after build Pin
RobinBlood11-Feb-08 23:31
RobinBlood11-Feb-08 23:31 

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.