5,696,576 members and growing! (13,410 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Miscellaneous Controls     Intermediate

C# Windows Forms ImageMap Control

By Ryan LaNeve

An ImageMap control for use in Windows Forms applications
C#, Windows, .NET 1.0, .NETVisual Studio, VS.NET2002, Dev

Posted: 31 Aug 2002
Updated: 31 Aug 2002
Views: 108,712
Bookmarked: 54 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
29 votes for this Article.
Popularity: 6.56 Rating: 4.49 out of 5
1 vote, 3.8%
1
0 votes, 0.0%
2
1 vote, 3.8%
3
4 votes, 15.4%
4
20 votes, 76.9%
5

Mouse hovering on red polygon  Mouse clicked on white rectangle

Introduction

I noticed a few requests on various newsgroups for an image map control which could be used in a Windows Forms application. I had not worked much with the System.Drawing namespace, so I decided to give it a try. This control is the result.

How It Works

The control uses a standard PictureBox control internally, specifically it's ability to load and display an image as well as it's inherited MouseMove, MouseLeave, and Click events. A ToolTip component is used as well to display tooltips for defined regions. Currently, the key specified for a region is used for the tooltip text, though it would be pretty easy to allow an additional "tooltip" property to be assigned for each region.

The bulk of the logic is in the private getActiveIndexAtPoint method shown below. The method is called whenever the mouse moves within the control to determine which region, if any, the mouse is within. If the mouse is within a region, the region's index is returned by the method. This index is used to lookup the region's key, which is then used to set the text displayed by the tooltip. The cursor is also changed to a hand and the index is stored in a private property to be re-used if the mouse is clicked, avoiding the necessity to call this method again. If the method does not find a region, a -1 is returned and the cursor is set to it's default.

private int getActiveIndexAtPoint(Point point)
{
    System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
    System.Drawing.Drawing2D.GraphicsPathIterator iterator = 
                new System.Drawing.Drawing2D.GraphicsPathIterator(_pathData);
    iterator.Rewind();
    for(int current=0; current < iterator.SubpathCount; current++)
    {
        iterator.NextMarker(path);
        if(path.IsVisible(point, this._graphics))
            return current;
    }
    return -1;
}

How To Use It

You can add this control to your toolbox just as you would any other .NET control. Once, added, simply drag-and-drop an instance of the control onto your form. Use the Image property to assign the desired image to the control.

Once you've added the control to your form, you can begin to call the various Add- methods to define the "hot-spots" within your image. The available methods are AddEllipse, AddRectangle, and AddPolygon. I tried to follow the conventions of HTML image maps with respect to defining the various shapes, and I overloaded the AddElipse and AddRectangle methods to accept ".NET-friendly" types such as Point and Rectangle. Since the methods follow the convention of HTML image maps, you should be able to use any existing image map generation software to determine exactly what points to specify for the desired regions within your image.

Here is the code required to define an HTML image map using the image in the first screenshot:

<!--Text BUTTON-->
   <area shape="rect" coords="140,20,280,60">

<!--Triangle BUTTON-->
   <area shape="poly" coords="100,100,180,80,200,140">

<!--Face BUTTON-->
   <area shape="circle" coords="80,100,60">

Here are the equivalent regions defined using this control:

this.imageMap1.AddRectangle("Rectangle", 140, 20, 280, 60);
this.imageMap1.AddPolygon("Polygon", new Point[] {new Point(100, 100), 
                          new Point(180, 80), new Point(200, 140)});
this.imageMap1.AddElipse("Ellipse", 80, 100, 60);

The control will raise an event - RegionClick - whenever the mouse is clicked within a defined region. The event passes two values denoting the index and the key of the clicked region. You would then take whatever action you wished based on the region clicked.

That's it! I hope this meets the needs of those who had requested an image map control for use in a Windows Forms application. Comments and requests are welcome.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Ryan LaNeve



Occupation: Web Developer
Location: United States United States

Other popular Miscellaneous articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
GeneralWant Image Editor Source code in C# windows application whose work like MS Paintmemberdheeraj.mittal5:20 24 Nov '08  
GeneralNeed some inputs for GraphicsPath.IsVisible(point pt) methodmemberkritti1023:36 26 Jun '08  
GeneralGurumemberutehn2:06 29 Mar '07  
GeneralWhat if resize?memberhuyhk17:57 12 Feb '07  
GeneralAwesomememberonnadi323:10 15 Nov '06  
GeneralHimemberpannujagwinder11:00 6 Oct '06  
QuestionWhat about hover color?membermackles13:03 14 Jul '06  
AnswerRe: What about hover color?memberMember 44219795:01 2 Oct '08  
GeneralImage Mapper -memberchuck crego6:41 7 Jul '06  
QuestionNo Controlmemberalainrod19:31 27 May '06  
AnswerRe: No ControlmemberShof17:36 12 Aug '06  
GeneralModified RemoveRegions()memberMoongster22:39 5 Feb '06  
GeneralGreat Work!memberHero Lala20:35 2 Dec '05  
GeneralRe: Great Work!memberseattle711:19 3 Dec '05  
GeneralMethods signatures a little weird...memberHeath Stewart12:18 9 Oct '02  
GeneralRe: Methods signatures a little weird...memberRyan LaNeve15:11 9 Oct '02  
GeneralRe: Methods signatures a little weird...memberHeath Stewart4:48 10 Oct '02  
GeneralRe: Methods signatures a little weird...memberRyan LaNeve9:37 10 Oct '02  
GeneralRe: Methods signatures a little weird...memberRad Seitis2:03 9 Jan '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 31 Aug 2002
Editor: Chris Maunder
Copyright 2002 by Ryan LaNeve
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project