Click here to Skip to main content
15,867,832 members
Articles / Web Development / ASP.NET
Article

ImageMap.NET

Rate me:
Please Sign up or sign in to vote.
4.54/5 (21 votes)
3 Jul 20024 min read 317.4K   6.7K   102   50
An ImageMap control for ASP.NET.

Sample Image - example.png

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


Written By
Software Developer (Senior) Farfetch
Portugal Portugal
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralCannot create an abstract class Pin
Anonymous16-Jul-02 5:34
Anonymous16-Jul-02 5:34 
GeneralRe: Cannot create an abstract class Pin
aalmada16-Jul-02 5:51
aalmada16-Jul-02 5:51 
GeneralRe: Cannot create an abstract class Pin
Anonymous16-Jul-02 6:02
Anonymous16-Jul-02 6:02 
GeneralRe: Cannot create an abstract class Pin
aalmada16-Jul-02 6:08
aalmada16-Jul-02 6:08 
GeneralRe: Cannot create an abstract class Pin
aalmada31-Jul-02 3:07
aalmada31-Jul-02 3:07 
QuestionHow can this be rated 4.91 Pin
Joaquín M López Muñoz8-Jul-02 11:19
Joaquín M López Muñoz8-Jul-02 11:19 
AnswerRe: How can this be rated 4.91 Pin
aalmada9-Jul-02 2:24
aalmada9-Jul-02 2:24 
AnswerRe: How can this be rated 4.91 Pin
Paul Watson15-Sep-02 3:20
sitebuilderPaul Watson15-Sep-02 3:20 
Joaquín M López Muñoz wrote:
At the time of writing this, the article was rated 4.91 with two votes. This seems quite impossible to me.

Simply put not everyone has the same "power" when voting. i.e. A Platinum member who votes 5 influences the total score more than a Bronze member who votes 5 (i.e. When Platinum votes 5 it is actually worth 5.5 while when Bronze votes it is actually worth 5.1 (naturally this is not the actual figures, just an example.)) This is why you can see two votes have such an odd total score.

Paul Watson
Bluegrass
Cape Town, South Africa

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

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