首先在页面上添加菜单控件:

  <esri:ContextMenu ID="ContextMenu1" runat="server" BackColor="White" OnItemClicked="ContextMenu1_ItemClicked1" />

在后台pageload页面添加显示全图和另存为的按钮:

//右击事件
public string StringResultCallBack;

 StringResultCallBack = Page.ClientScript.GetCallbackEventReference(this, "message", "processCallbackResult", "context", "postBackError", true);

if (IsPostBack)
return;
ContextMenu1.Items.Clear();
ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem fullextMenuItem = new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/fullextent.jpg", "全图", null);
ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem saveMenuItem = new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/save.jpg", "另存为图片", null);
ContextMenu1.Items.Add(fullextMenuItem);
ContextMenu1.Items.Add(saveMenuItem);

string format = "esriShowContextMenu(event,'{0}','{1}','{2}');return false;";
string showContextMenu = string.Format(format, ContextMenu1.ClientID, Map1.UniqueID, "");
Map1.Attributes.Add("oncontextmenu", showContextMenu);

右击触发的事件:

protected void ContextMenu1_ItemClicked1(object sender, ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItemEventArgs args)
{
switch (args.Item.Text)
{
case "全图":
{
MapHelper.ShowFullExtent(Map1);
ContextMenu1.CallbackResults.CopyFrom(Map1.CallbackResults);
break;
}
case "另存为图片":
{

MapHelper.SaveAsPicture(Map1);
ContextMenu1.CallbackResults.CopyFrom(Map1.CallbackResults);
break;
}
}
}

在app_code中添加MapHelper类,代码如下:

/// <summary>
/// 全图
/// </summary>
/// <param name="map"></param>
public static void ShowFullExtent(Map map)
{
ESRI.ArcGIS.ADF.Web.Geometry.Envelope fullExtent = null;
foreach (IMapFunctionality imf in map.GetFunctionalities())
{
MapFunctionality mf = imf as MapFunctionality;
ESRI.ArcGIS.ADF.ArcGISServer.MapDescription mapDescription = mf.MapDescription;
ESRI.ArcGIS.ADF.ArcGISServer.Envelope envServer = mapDescription.MapArea.Extent;
ESRI.ArcGIS.ADF.Web.Geometry.Envelope env = null;
env = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.ToAdfGeometry(envServer) as ESRI.ArcGIS.ADF.Web.Geometry.Envelope;
if (fullExtent == null)
{
fullExtent = env;
}
else
{
fullExtent.Union(env);
}
}
map.Extent = fullExtent;
map.Refresh();
}
/// <summary>
/// 地图另存为
/// </summary>
/// <param name="map"></param>
public static void SaveAsPicture(Map map)
{
int imgHeight = (int)map.Height.Value;//获得地图的高
int imgWidth = (int)map.Width.Value;//获得地图的宽

//将地图中的所有绘图功能进行循环,将生成的图片放入bitmap
System.Collections.Generic.IList<Bitmap> bitmaps = new System.Collections.Generic.List<Bitmap>();
foreach (IMapFunctionality mf in map.GetFunctionalities())
{
ESRI.ArcGIS.ADF.Web.MapImage mi = mf.DrawExtent(map.Extent);
if (mi != null)
bitmaps.Add(mi.GetImage());
}
Bitmap newImg = new Bitmap(imgWidth, imgHeight);//根据地图的宽、高创建一个新的图片
Graphics imgGraphics = Graphics.FromImage(newImg);//封装一个新的GDI+ 绘图图面,并将bitmap列表中的图片放入
imgGraphics.FillRectangle(new SolidBrush(System.Drawing.Color.LightGray), 0, 0, imgWidth, imgHeight);

for (int j = bitmaps.Count - 1; j >= 0; j--)
{
Bitmap bmp = bitmaps[j];
imgGraphics.DrawImage(bmp, 0, 0, imgWidth, imgHeight);
}
string fileNameVirtual = System.DateTime.Now.ToString() + ".jpg";//图片的名字
fileNameVirtual = fileNameVirtual.Replace(":", "-");
string fileName = map.Page.MapPath("Output\\" + fileNameVirtual);//存放路径
newImg.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);//保存
imgGraphics.Dispose();//释放资源
newImg.Dispose();//释放资源

//构造javascript代码
string functionValue = "open('SavePicture.aspx?FileName=" + fileNameVirtual + "', 'SavePicture');";
object[] oa = new object[1];
oa[0] = functionValue;
CallbackResult cr = new CallbackResult(null, null, "javascript", functionValue);
map.CallbackResults.Add(cr);


}

记得要在项目中添加一个Output文件夹喔,用于放保存的地图图片

posted on 2012-10-27 18:23  求知的我  阅读(282)  评论(0编辑  收藏  举报