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

ImageMap.NET

, 3 Jul 2002
Rate this:
Please Sign up or sign in to vote.
An ImageMap control for ASP.NET.
<!-- Download Links --> <!-- Article image -->

Sample Image - example.png

<!-- Add the rest of your HTML here -->

Introduction

The .NET framework comes with a nice set of web server controls that allows you to code, for ASP.NET, in a object-oriented manner and hiding the complexity of HTML, scripts and browser versioning. Unfortunately it doesn't supply any control that can generate image maps. We have developed a control that does just that.

This article explains how you can use this control. The supplied source code can also be used as an example on how to develop your own web server controls.

ImageButton vs. ImageMap

ASP.NET comes with an ImageButton control that handles mouse clicks on images but it doesn't behave exactly like an image map. The main difference is that you can not specify sensitive areas using the ImageButton. The consequences of this are:

  • You have to check if the coordinates of the click are within certain ranges. That's relatively easy for rectangular areas but for circles and polygons that's a lot more complicated. Not talking about the number of ifs that you have to maintain.
  • The mouse cursor, while over the image, is always a pointing finger. On image maps the pointing finger only shows while over the sensitive areas.
  • You can not have different tool tips within the same image.

The ImageMap control solves all these problems and can do everything a ImageButton can do.

Creating sensitive areas

The control supplies three classes for the creation of rectangular, circular and polygon areas. To add these to the control you just have to create instances of the areas and add them to the areas list using the Areas property.

The sensitive areas are handled by the order that they are added. If the areas overlap on the point that the user clicks on, then the first area is used.

ImageMapRectangleArea

Allows the creation of rectangular sensitive areas.

Public constructors:

public ImageMapRectangleArea(Rectangle rectangle)
public ImageMapRectangleArea(string hRef, Rectangle rectangle)
public ImageMapRectangleArea(string hRef, Rectangle rectangle, 
                             string toolTip)

Parameters:

hRef - Hyperlink associated with the area. Default is null.
rectangle - location, width and heigth of the rectangular area, in local coordinates.
toolTip - String displayed when the mouse cursor stops over the area. Default is null.

ImageMapCircleArea

Allows the creation of circular sensitive areas.

Public constructors:

public ImageMapCircleArea(Point center, int radius)
public ImageMapCircleArea(string hRef, Point center, int radius)
public ImageMapCircleArea(string hRef, Point center, int radius, 
                          string toolTip)
        

Parameters:

hRef - Hyperlink associated with the area. Default is null.
center - Center of the circle, in local coordinates.
radius - The radius of the circle.
toolTip - String displayed when the mouse cursor stops over the area. Default is null.

ImageMapPolygonArea

Allows the creation of polygon sensitive areas.

Public constructors:

public ImageMapPolygonArea()
public ImageMapPolygonArea(string hRef)
public ImageMapPolygonArea(string hRef, string toolTip)
        

Parameters:

hRef - Hyperlink associated with the area. Default is null.
toolTip - String displayed when the mouse cursor stops over the area. Default is null.

Remarks:

The polygon points are added using the Points parameter of the ImageMapPolygonArea.

Image maps with hyperlinks

You can create regular image maps with hyperlinks associated to the sensitive area. When the user clicks on them the browser jumps to the specified URL. Just add the areas with non-empty URLs.

private void Page_Load(object sender, System.EventArgs e)
{
    // add a rectangular area
    imageMap.Areas.Add(
          new ImageMapRectangleArea("http://www.ydreams.com/", 
                                    new Rectangle(405, 188, 15, 15), 
                                    "Click to go to YDreams"));
    
    // add a circular area
    imageMap.Areas.Add(
         new ImageMapCircleArea("http://www.codeproject.com/", 
                                new Point(210, 197), 100, 
                                "Click to go to CodeProject"));
    
    // add a polygon area
    ImageMapPolygonArea polygonArea
             = new ImageMapPolygonArea("", "This area has no link");

    polygonArea.Areas.Add(new Point(0, 0));
    polygonArea.Areas.Add(new Point(100, 0));
    polygonArea.Areas.Add(new Point(0, 100));
    imageMap.Areas.Add(polygonArea);
}
        

If the sensitive area has the URL set to the empty string, then it won't have an hyperlink. The mouse cursor will be an arrow but the tooltip will show up.

Image maps with server-side event handling

If you need to process, not only the area, but also the coordinates of the point where the user clicked on, you can send these to the server and have them handled there.

private void Page_Load(object sender, System.EventArgs e)
{
    imageMap.Click += new ImageMapClickEventHandler(imageMap_Click);

    imageMap.Areas.Add(new ImageMapRectangleArea(null, 
             new Rectangle(405, 188, 15, 15), "Click to go to YDreams"));
    imageMap.Areas.Add(new ImageMapRectangleArea(null,
             new Rectangle(405, 379, 15, 15), "Click to go to CodeProject"));
}

private void ImageMap_Click(object sender, ImageMapClickEventArgs args)
{
    ImageMapArea area = imageMap.Areas[args.AreaIndex];
    int x = args.X;
    int y = args.Y
    
    // add event handling code here
}
        

To do this, you just need to assign an event handler to the Click event of the control and assign a null URL to all the areas that you want to be handled on the server.

The event will only be raised if the user clicks on a sensitive area. To simulate the ImageButton you just need to add an area that occupies all the image.

Image maps with client-side event handling

If you want to save on traffic to the server, and the browser allows client-side scripting, you can have the events handled on the client.

private void Page_Load(object sender, System.EventArgs e)
{
    // create an instance of the area
    ImageMapArea area
         = new ImageMapRectangleArea("http://www.codeproject.com/", 
                                     new Rectangle(405, 379, 15, 15), 
                                     "Click to go to CodeProject");    
    // add the area to the map
    imageMap.Areas.Add(area);
    
    // add OnMouseOver and OnMouseOut event handlers to the area
    area.Attributes.Add("onMouseOver", "javascript:DisplayImage(image2)");
    area.Attributes.Add("onMouseOut", "javascript:DisplayImage(image1)");
    
    // add the script called by the event handlers
    StringBuilder script = new StringBuilder();
    script.AppendFormat("<script language="\""javascript\">{0}", 
                        Environment.NewLine);
    script.AppendFormat("var image1 = new Image(291, 112);{0}", 
                        Environment.NewLine);
    script.AppendFormat("image1.src = \"image1.png\";{0}", 
                        Environment.NewLine);
    script.AppendFormat("var image2 = new Image(291, 112);{0}", 
                        Environment.NewLine);
    script.AppendFormat("image2.src = \"image2.png\";{0}", 
                        Environment.NewLine);
    script.AppendFormat("function DisplayImage(image) {0}{1}", "{", 
                        Environment.NewLine);
    script.AppendFormat("document.imageMap.src = image.src;{0}", 
                        Environment.NewLine);
    script.AppendFormat("return true;{0}", Environment.NewLine);
    script.AppendFormat("\t{0}{1}", "}", Environment.NewLine);
    script.Append("</script>");

    Page.RegisterClientScriptBlock("DisplayImage", script.ToString());
}
        

Mixing image map types

You can mix all the three types of image maps described above. The only restriction is that you can not assign an onClick client-side event handler to the areas that you want to have handled on the server (that have the URL set to null).

Supported browsers

  • Internet Explorer 6.0

What's next?

Support more browsers. Any help is 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

Share

About the Author

aalmada
Software Developer (Senior) LusoVU
Portugal Portugal
No Biography provided
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 3 PinmemberMainak Roy Chowdhury22-Sep-11 5:40 
GeneralA Little Complex Pinmembertsingroo30-Jul-10 22:55 
GeneralHotspots color or Backroung Image Pinmembershashi198614-Jul-10 3:42 
GeneralAdd Multiple Polygon HotSpots Pinmemberdmgroup198415-May-09 7:36 
Hello All,
 
I was wondering if anyone could help me out with adding multiple polygon hotspots to one image. I am able to add multiple polygon hotspots to my aspx page but having some difficulty with editing the C# code behind. If someone could please help out it would be much appreciated.
 
Thanks,
 
Dale
GeneralImageMap with mobile web form Pinmembercodeprojid11-Aug-07 1:46 
GeneralimageMap hotspot color Pinmembermastjabs25-Apr-06 12:47 
GeneralMultiple ImageMaps within the same form [fix] Pinmemberflipflargin30-Mar-06 9:54 
GeneralNice Job PinmemberMike Elliott3-Mar-06 4:52 
NewsImageMap.NET 2.0 PinmemberYDreams2-Mar-06 7:26 
GeneralImagemap cssstyle PinmemberLeftyServ31-Jan-06 23:02 
GeneralRe: Imagemap cssstyle PinmemberYDreams2-Mar-06 8:07 
Questionhighlighting a hotspot on mouseover? PinmemberKieft2-Jan-06 18:21 
AnswerRe: highlighting a hotspot on mouseover? PinmemberAntao2-Jan-06 23:01 
GeneralRe: highlighting a hotspot on mouseover? PinmemberVTMiddie3122-Feb-06 7:09 
GeneralImage map PinmemberAnushaAjit15-Dec-05 20:22 
GeneralRe: Image map PinmemberAntao6-Jan-06 8:26 
QuestionAnybody has significantly improved this? PinsussGps Touch1-Aug-05 8:36 
AnswerRe: Anybody has significantly improved this? PinmemberAntao2-Jan-06 23:20 
AnswerRe: Anybody has significantly improved this? PinmemberAntao3-Jan-06 3:50 
AnswerRe: Anybody has significantly improved this? PinmemberYDreams3-Mar-06 2:17 
QuestionHow to link to a regular image map from a table Pinmemberiampedro5-Jul-05 11:01 
GeneralConvert to VB.Net Pinmemberwsigns328-Jun-05 6:16 
Generali want to know where the size infomation works Pinmemberbestwsfz3j2-Jun-05 0:06 
Questionhow to get rid of padding below image Pinmemberlilon10-Mar-05 15:28 
QuestionPolygonAreas? Pinsusshenkomannen22-Feb-05 21:48 

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
Web03 | 2.8.140916.1 | Last Updated 4 Jul 2002
Article Copyright 2002 by aalmada
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid