Click here to Skip to main content
Click here to Skip to main content

Create a Debugger Visualizer in 10 Lines of Code

By , 20 Feb 2006
 
Prize winner in Competition "C# Jan 2006"

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)

About the Author

Tomer Noy
Web Developer
Israel Israel
Member
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).

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberPankaj Chamria27 Jan '12 - 2:35 
Good one
GeneralMy vote of 5memberS.P. Tiwari12 Dec '11 - 1:16 
good one
GeneralMy vote of 5memberhoernchenmeister15 Apr '11 - 3:53 
lovely, really usefull, thanks a lot!
GeneralMy vote of 5memberv# guy9 Oct '10 - 20:09 
Very help full, thanks Tomer Noy. modified on Sunday, October 10, 2010 2:38 AM
GeneralRe: My vote of 5memberTomer Noy16 Nov '10 - 4:38 
Thanks   It's nice to keep getting good feedback on this article.
GeneralAwesomememberXmen W.K.1 May '10 - 18:51 
This is just an awesome thing you have wrote.... voted 5 and bookmarked. TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%...
GeneralAddition to ImageVisualizermemberdjb0331754 Mar '10 - 12:10 
Wanted to share this in case someone else wanted to know how to include scroll bars etc... Great original post!!!   using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Globalization; using...
GeneralVS2008 WorksmemberTical Bowens11 Aug '09 - 7:40 
Works for me in VS2008, after I upgraded it to VS2008 (.NET 3.5).
General2008 sucksmembersofter24 Mar '09 - 7:44 
In 2008 your example and all the examples I have searched on google simply don't work!
GeneralVS 2008memberdfsdfsdfoyuti28 Feb '09 - 7:00 
In 2005 -- works KO!   In 2008 -- gives some type of casting error when I click to view an image during debug time.   Can you provide 2008-project to build dll that would work in VS2008?   Thanks
AnswerRe: VS 2008 - RESOLVED!memberboops boops14 Apr '09 - 9:04 
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!   all the best, Vic
Generalusing ImageVisualizer with small imagesmemberchopeen31 Aug '07 - 2:40 
I made a small change that makes working with small images easier:   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(Math.Max(image.Width,...
QuestionHDC Visualizer?memberDavid Howe21 May '07 - 22:28 
Is it possible to make an HDC visualizer?
QuestionArray Visualizer ?memberAbdullah_m20 Mar '07 - 23:14 
hi what kind of objects can be visualized ? is array accepted ? thanks
AnswerRe: Array Visualizer ?memberTomer Noy24 Mar '07 - 22:22 
Any serializable type can be visualized.   I'm pretty sure that arrays are serializable, so they should be fine.
QuestionVisualizer for C++ types ?memberVertilka The Blue20 Nov '06 - 21:56 
Can i create a debbuger visualizer for C++ types ? How ? I tried to program with C++/CLI but couldn't get visualizer to be activated.   Please post a short code that works.   Tnx, Vertilka
AnswerRe: Visualizer for C++ types ?memberTomer Noy26 Nov '06 - 2:23 
Hi,   Unfortunately, this feature isn't supported for native C++ classes. You can do it with ref classes written in C++/CLI, but that's not saying much since these are managed classes.   The reason that native C++ classes aren't supported is that the object that is viewed must...
AnswerRe: Visualizer for C++ types ?memberSnakefoot5 Dec '06 - 8:50 
One can use the autoexp.dat to create custom representations of C++ types. Though they can only change the text-representation of a C++ type when shown in the debug-watch (also changes tooltip at mouse-over).   This is useful if having a complex datatypes, where only one or two attributes...
QuestionIs there a visualizer in VS 2003?memberms_breeze10127 Apr '06 - 1:14 
Is there a visualizer in VS 2003? How will i Code it? Thankz.
AnswerRe: Is there a visualizer in VS 2003?membermwdiablo1 Aug '06 - 13:48 
No there is not, this is a VS2005 feature.   Just another rocket scientist, NOT!
QuestionDebuggerVisualizer ?memberRdoerrer28 Mar '06 - 20:21 
Any tip on how to use DebuggerVisualizer attribute with a class of my own? Or how to make the visualizer work for more than one class (Image + some of my own classes that contain Bitmaps)   Thanks     Rainer
AnswerRe: DebuggerVisualizer ?memberEric_Franz30 Mar '06 - 8:12 
To use the DebuggerVisualizer on a class of your own:   Write your own UserControl and populate it with your custom class. For example, let's say you had a custom collection. Your UserControl should accept the collection via a method (let's say: Attach(CustomCollection _collection)) and...
QuestionAuto deploy, and refresh visualizer after buildmemberlp@netpark27 Mar '06 - 22:04 
Created an Post Build event that fires on Successful Build   copy "$(TargetPath)" "C:\Program Files\Microsoft Visual Studio 8\Common7\Packages\Debugger\Visualizers\"   That deploys the dll directly.   But is there any way to refresh the visualizers in an already open...
GeneralRe: Auto deploy, and refresh visualizer after buildmemberRobinBlood11 Feb '08 - 23:31 
Hi, I used -> properties, Build, Output path and set this directly to die Visualizer Folder. In this case it is overwritten each time you build the visualizer. But is there any way to debug a visualizer while using it?
Question? - Make visualizer for unserializable objectsmemberlp@netpark22 Mar '06 - 3:17 
Great sample!!   Tried to make an visualizer that displays the parameters collection in the System.Data.SqlClient.SqlCommand.Parameters collection, but get an error:   ---snip--- Type "System.Data.SqlClient.SqlCommand" in assembly "System.Data, version .... is not marked as...

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

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