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 
GeneralMy vote of 5memberS.P. Tiwari12 Dec '11 - 1:16 
GeneralMy vote of 5memberhoernchenmeister15 Apr '11 - 3:53 
GeneralMy vote of 5memberv# guy9 Oct '10 - 20:09 
GeneralRe: My vote of 5memberTomer Noy16 Nov '10 - 4:38 
GeneralAwesomememberXmen W.K.1 May '10 - 18:51 
GeneralAddition to ImageVisualizermemberdjb0331754 Mar '10 - 12:10 
GeneralVS2008 WorksmemberTical Bowens11 Aug '09 - 7:40 
General2008 sucksmembersofter24 Mar '09 - 7:44 
GeneralVS 2008memberdfsdfsdfoyuti28 Feb '09 - 7:00 
AnswerRe: VS 2008 - RESOLVED!memberboops boops14 Apr '09 - 9:04 
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, 100), Math.Max(image.Height, 100));
form.FormBorderStyle = FormBorderStyle.FixedToolWindow;

PictureBox pictureBox = new PictureBox();
pictureBox.Image = image;
pictureBox.Parent = form;
pictureBox.Dock = DockStyle.Fill;
pictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
 
windowService.ShowDialog(form);

 
However, it adds one line to the code. Wink | ;)
QuestionHDC Visualizer?memberDavid Howe21 May '07 - 22:28 
QuestionArray Visualizer ?memberAbdullah_m20 Mar '07 - 23:14 
AnswerRe: Array Visualizer ?memberTomer Noy24 Mar '07 - 22:22 
QuestionVisualizer for C++ types ?memberVertilka The Blue20 Nov '06 - 21:56 
AnswerRe: Visualizer for C++ types ?memberTomer Noy26 Nov '06 - 2:23 
AnswerRe: Visualizer for C++ types ?memberSnakefoot5 Dec '06 - 8:50 
QuestionIs there a visualizer in VS 2003?memberms_breeze10127 Apr '06 - 1:14 
AnswerRe: Is there a visualizer in VS 2003?membermwdiablo1 Aug '06 - 13:48 
QuestionDebuggerVisualizer ?memberRdoerrer28 Mar '06 - 20:21 
AnswerRe: DebuggerVisualizer ?memberEric_Franz30 Mar '06 - 8:12 
QuestionAuto deploy, and refresh visualizer after buildmemberlp@netpark27 Mar '06 - 22:04 
GeneralRe: Auto deploy, and refresh visualizer after buildmemberRobinBlood11 Feb '08 - 23:31 
Question? - Make visualizer for unserializable objectsmemberlp@netpark22 Mar '06 - 3:17 
AnswerRe: ? - Make visualizer for unserializable objectsmemberTomer Noy23 Mar '06 - 2:48 
GeneralCool Articlememberabugov22 Mar '06 - 0:36 
GeneralIt's greatmemberradzimskik13 Mar '06 - 18:51 
NewsHi EveryonememberTomer Noy12 Mar '06 - 21:42 
QuestionWhat about big images?memberlordavenger1 Mar '06 - 21:11 
AnswerRe: What about big images?sitebuilderUwe Keim11 Mar '06 - 4:00 
GeneralVisualizersmemberWcohen28 Feb '06 - 0:39 
GeneralI finally get it.memberAshaman27 Feb '06 - 1:52 
QuestionC++?memberPJ Arends25 Feb '06 - 7:37 
AnswerRe: C++?memberRama Krishna Vavilala25 Feb '06 - 10:05 
GeneralRe: C++?memberjbeaurain27 Sep '06 - 3:12 
GeneralBrilliantmemberFabrice Vergnenegre25 Feb '06 - 0:49 
GeneralFor Regular Expressions, toositebuilderUwe Keim22 Feb '06 - 18:10 
GeneralRe: For Regular Expressions, toomemberGreeeg4 Mar '06 - 9:38 
Generalyou rock !! :)memberMANSATAN22 Feb '06 - 11:12 
GeneralCool stuffmemberivix4u22 Feb '06 - 0:21 
GeneralGreat information!memberDouglas Troy21 Feb '06 - 4:39 
GeneralVisualizer for XML datamemberNetronProject21 Feb '06 - 2:51 
Generalgreat tip!memberJavier Gutierrez21 Feb '06 - 0:09 
AnswerRe: great tip!memberTomer Noy21 Feb '06 - 19:58 
GeneralThanks for all the positive feedbackmemberTomer Noy20 Feb '06 - 23:29 
GeneralWoha!memberpeterchen20 Feb '06 - 19:30 
GeneralShort and Sweetmemberoykica20 Feb '06 - 17:49 
GeneralGreat!memberGreeeg20 Feb '06 - 4:55 
GeneralVery slickmemberyrodrigu20 Feb '06 - 4:54 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 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