Click here to Skip to main content
Click here to Skip to main content
Go to top

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

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:

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)

Share

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

Comments and Discussions

 
BugImage width 2x1: Unable to close window PinmemberRul Aman27-Jun-13 8:35 
GeneralMy vote of 5 PinmemberPankaj Chamria27-Jan-12 2:35 
GeneralMy vote of 5 PinmemberS.P. Tiwari12-Dec-11 1:16 
GeneralMy vote of 5 Pinmemberhoernchenmeister15-Apr-11 3:53 
GeneralMy vote of 5 Pinmemberv# guy9-Oct-10 20:09 
GeneralRe: My vote of 5 PinmemberTomer Noy16-Nov-10 4:38 
GeneralAwesome PinmemberXmen W.K.1-May-10 18:51 
GeneralAddition to ImageVisualizer Pinmemberdjb0331754-Mar-10 12:10 
GeneralVS2008 Works PinmemberTical Bowens11-Aug-09 7:40 
General2008 sucks Pinmembersofter24-Mar-09 7:44 
GeneralVS 2008 Pinmemberdfsdfsdfoyuti28-Feb-09 7:00 
AnswerRe: VS 2008 - RESOLVED! Pinmemberboops boops14-Apr-09 9:04 
Generalusing ImageVisualizer with small images Pinmemberchopeen31-Aug-07 2:40 
QuestionHDC Visualizer? PinmemberDavid Howe21-May-07 22:28 
QuestionArray Visualizer ? PinmemberAbdullah_m20-Mar-07 23:14 
AnswerRe: Array Visualizer ? PinmemberTomer Noy24-Mar-07 22:22 
QuestionVisualizer for C++ types ? PinmemberVertilka The Blue20-Nov-06 21:56 
AnswerRe: Visualizer for C++ types ? PinmemberTomer Noy26-Nov-06 2:23 
AnswerRe: Visualizer for C++ types ? PinmemberSnakefoot5-Dec-06 8:50 
QuestionIs there a visualizer in VS 2003? Pinmemberms_breeze10127-Apr-06 1:14 
AnswerRe: Is there a visualizer in VS 2003? Pinmembermwdiablo1-Aug-06 13:48 
QuestionDebuggerVisualizer ? PinmemberRdoerrer28-Mar-06 20:21 
AnswerRe: DebuggerVisualizer ? PinmemberEric_Franz30-Mar-06 8:12 
QuestionAuto deploy, and refresh visualizer after build Pinmemberlp@netpark27-Mar-06 22:04 
GeneralRe: Auto deploy, and refresh visualizer after build PinmemberRobinBlood11-Feb-08 23:31 
Question? - Make visualizer for unserializable objects Pinmemberlp@netpark22-Mar-06 3:17 
AnswerRe: ? - Make visualizer for unserializable objects PinmemberTomer Noy23-Mar-06 2:48 
GeneralCool Article Pinmemberabugov22-Mar-06 0:36 
GeneralIt's great Pinmemberradzimskik13-Mar-06 18:51 
NewsHi Everyone PinmemberTomer Noy12-Mar-06 21:42 
QuestionWhat about big images? Pinmemberlordavenger1-Mar-06 21:11 
AnswerRe: What about big images? PinsitebuilderUwe Keim11-Mar-06 4:00 
GeneralVisualizers PinmemberWcohen28-Feb-06 0:39 
GeneralI finally get it. PinmemberAshaman27-Feb-06 1:52 
QuestionC++? PinmemberPJ Arends25-Feb-06 7:37 
AnswerRe: C++? PinmemberRama Krishna Vavilala25-Feb-06 10:05 
GeneralRe: C++? Pinmemberjbeaurain27-Sep-06 3:12 
GeneralBrilliant PinmemberFabrice Vergnenegre25-Feb-06 0:49 
GeneralFor Regular Expressions, too PinsitebuilderUwe Keim22-Feb-06 18:10 
GeneralRe: For Regular Expressions, too PinmemberGreeeg4-Mar-06 9:38 
Generalyou rock !! :) PinmemberMANSATAN22-Feb-06 11:12 
GeneralCool stuff Pinmemberivix4u22-Feb-06 0:21 
GeneralGreat information! PinmemberDouglas Troy21-Feb-06 4:39 
GeneralVisualizer for XML data PinmemberNetronProject21-Feb-06 2:51 
Generalgreat tip! PinmemberJavier Gutierrez21-Feb-06 0:09 
AnswerRe: great tip! PinmemberTomer Noy21-Feb-06 19:58 
GeneralThanks for all the positive feedback PinmemberTomer Noy20-Feb-06 23:29 
GeneralWoha! Pinmemberpeterchen20-Feb-06 19:30 
GeneralShort and Sweet Pinmemberoykica20-Feb-06 17:49 
GeneralGreat! PinmemberGreeeg20-Feb-06 4:55 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 20 Feb 2006
Article Copyright 2006 by Tomer Noy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid