An ImageMap Control for ASP.NET.

xiaoxiao2021-03-06  54

http://www.codeproject.com/aspnet/imagemap.asp

Download Source Files - 13 KB Download Demo Project - 23 KB

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 does not supply any control That Can Generate Image Maps. We Have We Have We Have Weed A Control That Does Just That.

This Article The Source Code CAN ALSOBE. We................

ImageButton vs. imageMap

ASP.NET comes with an ImageButton control that handles mouse clicks on images but it does not 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 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 Area Handled by The ORDER That In The Point That The User Click ON, THE FIRST AREA IS USED.IMAGEMAPRECTANGLEAA

Allows The Creation of Rectangular Sensitive Areas.

Public Constructionors:

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 sensive isas.

Public Constructionors:

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 Constructionors:

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:

.

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, The Mouse Cursor Will BE. The Mouse Cursor Will BE.

Image Maps with Server-Side Event Handling

If iF you need the area, but also the coordinates of the point where the user copy on, you can send these to the server and have.

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 ess ess an the control and assign a null url to all the all the service ..

....................... ..

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 ("