通过ArcGISsilverlight API实现输出地图到图片常用的有如下两种方式:向服务器端发送http请求、通过WriteableBitmap类在客户端直接对地图控件截图。
向服务器端发送http请求方式 虽然可以做到输出指定矩形范围的地图、设置控件参考、支持Png/jpg/bmp等图片格式,但这种方式每次只能对单一的地图服务进行截图,换句话讲,当我们叠加了多个地图服务时没有办法将输出区域内所有的地图服务一起输出;这种方式也不支持对Graphiclayer图层输出。
选择地图输出区域
服务器端返回的图片
privatevoid ExportImage(ESRI.ArcGIS.Client.Geometry.Geometry extent)
{
try
{
EnvelopesEnv = extent.Extent;
StringBuilder sUrl =newStringBuilder();
sUrl.Append(“http://lingy/ArcGIS/rest/services/dxmap/MapServer”);
sUrl.Append(“/Export?”);
sUrl.Append(“f=json”);
sUrl.Append(String.Format(“&bbox={0},{1},{2},{3}”, sEnv.XMin, sEnv.YMin, sEnv.XMax, sEnv.YMax));
sUrl.Append(“&format=”);
sUrl.Append(“png”);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(sbUrl.ToString());
request.BeginGetResponse(newAsyncCallback(ExportCall), request);
}
catch (Exception ex)
{
MessageBox.Show(“导出图片失败:“ + ex.ToString());
}
}
privatevoid ExportCall(IAsyncResult asynchronousResult)
{
this.Dispatcher.BeginInvoke(delegate()
{
try
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Streamstream = response.GetResponseStream();
StreamReader reader =newStreamReader(stream);
stringresult = reader.ReadToEnd();
stringhref = result.Split(newchar[] {“”" })[3];
linkImageResult.NavigateUri= newUri(href,UriKind.Absolute);
}
catch (Exception ex)
{
MessageBox.Show(“导出图片失败:“ + ex.ToString());
}
});
}
WriteableBitmap可以在客户端直接对地图控件截图保存,保存的图片格式需要自己编写格式编码。
通过WriteableBitmap截图源码:
privatevoid button1_Click(object sender,RoutedEventArgs e)
{
try
{
cMapExportToImageimageExporter =newcMapExportToImage();
bool bResult =imageExporter.ExportPNG(mainMapasUIElement);
string strResult =bResult ?“成功!“ : “失败!“;
MessageBox.Show(“图片导出“ +strResult);
}
catch (Exception pEr)
{
string msg =pEr.Message;
}
}