实现效果如下:
最开始说要写这个功能的时候,还挺烦躁的。因为好久没有没有动脑子写AO功能了,习惯了搬砖~(万年也写一篇博客,今天实在没啥事觉得写一篇,嘿嘿嘿~)
然后百度了网上的一些案例,发现多多少烧走到某一步的时候,就发现不是少了这个参数就是少了哪个参数。然后自己要去补全这些东西,它们到底是怎么来的,然后就补全了。
因为soe测试比较麻烦,其他的功能我都再测试程序调试好,再保class放到真正的soe程序里边直接运行的。
一、既然是导出server中的指定图层指定范围的图片,那soe所挂载的这个server必须有这个图层名称,名称必须是SDE库原来的名称!
二、再然后就是导出的时候,控制server图层的显示。
三、就是范围了,不能直接整个指定范围导出吧,就像我上边的截图,也需要周边的一些内容,还需要画出范围本身的这个红线。
/// <summary>
/// 调用soe挂载的server服务地图,并导出为图片流
/// </summary>
/// <param name="serverObjectHelper">当前SOE挂载在的server服务</param>
/// <param name="pGeometry">当前地块的几何范围</param>
/// <param name="pFeatClsNames">当前调用服务使用到的图层名称</param>
/// <returns></returns>
public static string fnExportImgbyte(IServerObjectHelper serverObjectHelper, IGeometry pGeometry, List<string> pFeatClsNames)
{
try
{
string imgbyte = string.Empty;
//--------1、获取soe挂载的地图服务
IMapServer3 pMapServer = (IMapServer3)serverObjectHelper.ServerObject;
//--------2、设置输出图片格式,并导出服务图片流----功能有效
#region 设置输出图片格式
IImageType imgtype = new ImageTypeClass();
imgtype.Format = esriImageFormat.esriImagePNG;
imgtype.ReturnType = esriImageReturnType.esriImageReturnMimeData;
//imgtype.ReturnType = esriImageReturnType.esriImageReturnURL;
IImageDisplay imgdisp = new ImageDisplayClass();
imgdisp.Height = 400;
imgdisp.Width = 500;
imgdisp.DeviceResolution = 150;
IImageDescription imgdesc = new ImageDescriptionClass();
imgdesc.Display = imgdisp;
imgdesc.Type = imgtype;
#endregion
IMapServerInfo pMapServerInfo = pMapServer.GetServerInfo(pMapServer.DefaultMapName);
IMapDescription pMapDescription = pMapServerInfo.DefaultMapDescription;
IMapLayerInfos layerInfos = pMapServer.GetServerInfo(pMapServer.DefaultMapName).MapLayerInfos;//获取当前服务的所有图层
//IMapServerDataAccess dataAccess = (IMapServerDataAccess)pMapServer;//获取指定id的图层的矢量数据
//IFeatureClass pfeatureclass = (IFeatureClass)dataAccess.GetDataSource(pMapServer.DefaultMapName, Layerindex);//获取指定id的图层的矢量数据
//--------3、设置导出图片的范围为地块范围
#region 设置导出图片的范围为地块范围----功能有效
IMapArea pMapArea = pMapDescription.MapArea;
IEnvelope pExtent = (pGeometry as IPolygon).Envelope;
pExtent.Expand(1.1, 1.1, true);
IMapExtent pMapExtent = (IMapExtent)pMapArea;
pMapExtent.Extent = pExtent;
pMapDescription.MapArea = pMapArea;
#endregion
//--------4、控制图层显示
#region 控制图层显示----功能有效
//--------4-1、获取每个矢量图层的名称SDE.--
List<int> pLayerIDs = new List<int>();//当前调用服务参数,使用到的图层id
IMapLayerInfo layerInfo;
for (int i = 0; i < layerInfos.Count; i++)
{
layerInfo = layerInfos.get_Element(i);
foreach (string str in pFeatClsNames)
{
if (str.Trim().ToUpper() == layerInfo.Name.ToUpper())
{
pLayerIDs.Add(i);
}
}
}
//--------4-2、当前soe使用的图层显示,其他隐藏
ILayerDescriptions pLayerDescriptions = pMapDescription.LayerDescriptions;
for (int i = 0; i < pLayerDescriptions.Count; i++)
{
bool flage = false;
foreach (int id in pLayerIDs)
{
if (i == id)
{
flage = true;
}
}
ILayerDescription pLayerDescription = pLayerDescriptions.get_Element(i);
pLayerDescription.Visible = flage;
}
#endregion
//5、导出图片前,添加地块元素
#region 添加地块元素
IPolygonElement PolygonElement = new PolygonElementClass();
IElement pElement = PolygonElement as IElement;
pElement.Geometry = pGeometry;
//设置地块元素样式
IFillShapeElement pFillShapeElement = (IFillShapeElement)pElement;
ISymbol pSymbol = CreateSimpleFillSymbol(Color.Red, 100, esriSimpleFillStyle.esriSFSNull);
pFillShapeElement.Symbol = (IFillSymbol)pSymbol;
//新增到地图服务
IGraphicElements pGraphicElements = new GraphicElementsClass();
pGraphicElements.Add(pElement as IGraphicElement);
pMapDescription.CustomGraphics = pGraphicElements;
#endregion
IImageResult pImageResult = pMapServer.ExportMapImage(pMapDescription, imgdesc);
byte[] pMimeData = pImageResult.MimeData;//图片流
imgbyte = Convert.ToBase64String(pMimeData);//图片流转字符串
//imgbyte = pImageResult.URL;//图片地址
return imgbyte;
}
catch (Exception ex) { return "fnExportImgbyte方法报错:" + ex.ToString(); }
}
/// <summary>
/// 设置面元素样式
/// </summary>
/// <param name="fillColor"></param>
/// <param name="oLineWidth"></param>
/// <param name="fillStyle"></param>
/// <returns></returns>
public static ISymbol CreateSimpleFillSymbol(Color fillColor, int oLineWidth, esriSimpleFillStyle fillStyle)
{
ISimpleFillSymbol pSimpleFillSymbol;
pSimpleFillSymbol = new SimpleFillSymbol();
pSimpleFillSymbol.Style = fillStyle;
pSimpleFillSymbol.Color = GetColor(fillColor.R, fillColor.G, fillColor.B);
pSimpleFillSymbol.Outline = (ILineSymbol)CreateSimpleLineSymbol(fillColor, 1, esriSimpleLineStyle.esriSLSDash);
return (ISymbol)pSimpleFillSymbol;
}
/// <summary>
/// 获取颜色
/// </summary>
/// <param name="r"></param>
/// <param name="g"></param>
/// <param name="b"></param>
/// <returns></returns>
public static IRgbColor GetColor(int r, int g, int b)
{
RgbColor color = new RgbColor();
color.Red = r;
color.Green = g;
color.Blue = b;
return color;
}
/// <summary>
/// 新建线样式
/// </summary>
/// <param name="color"></param>
/// <param name="width"></param>
/// <param name="style"></param>
/// <returns></returns>
public static ISymbol CreateSimpleLineSymbol(Color color, int width, esriSimpleLineStyle style)
{
ISimpleLineSymbol pSimpleLineSymbol;
pSimpleLineSymbol = new SimpleLineSymbol();
pSimpleLineSymbol.Width = width;
pSimpleLineSymbol.Color = GetColor(color.R, color.G, color.B);
pSimpleLineSymbol.Style = style;
return (ISymbol)pSimpleLineSymbol;
}