ASP.NET 2.0 -- ImageMap






3.94/5 (7 votes)
Sep 8, 2006
4 min read

77413

1903
ASP.NET 2.0 -- ImageMap
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
if
s 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...