Add-in for Arcgis Desktop框选指定区域生成图片

要求

在地图上框选一个区域,生成图片,图片默认存在mxd所在的目录下

开发环境

ArcGis 10.1
.net2010

实现思路

1、自定义Base Tool
2、在OnmouseDown事件中实现地图框功能
3、获得mxd路径
4、基于获得的矩形范围生成图片

部分核心代码

在OnmouseDown事件中实现地图框功能

public void DrawPolygon(ESRI.ArcGIS.Carto.IActiveView activeView)
{

    if (activeView == null)
    {
        return ;
    }

    ESRI.ArcGIS.Display.IScreenDisplay screenDisplay = activeView.ScreenDisplay;

    // Constant.
    screenDisplay.StartDrawing(screenDisplay.hDC, (System.Int16)
        ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache); // Explicit cast.
    ESRI.ArcGIS.Display.IRgbColor rgbColor = new ESRI.ArcGIS.Display.RgbColorClass();
    rgbColor.Red = 255;

    ESRI.ArcGIS.Display.IColor color = rgbColor; // Implicit cast.
    ESRI.ArcGIS.Display.ISimpleFillSymbol simpleFillSymbol = new
        ESRI.ArcGIS.Display.SimpleFillSymbolClass();
    simpleFillSymbol.Color = color;

    ESRI.ArcGIS.Display.ISymbol symbol = simpleFillSymbol as
        ESRI.ArcGIS.Display.ISymbol; // Dynamic cast.
    ESRI.ArcGIS.Display.IRubberBand rubberBand = new
        ESRI.ArcGIS.Display.RubberEnvelopeClass();
    ESRI.ArcGIS.Geometry.IEnvelop geometry = rubberBand.TrackNew(screenDisplay,
       (ESRI.ArcGIS.Display.ISymbol) symbol);
    screenDisplay.SetSymbol(symbol);
    screenDisplay.DrawPolygon(geometry);
    screenDisplay.FinishDrawing();
}

获得mxd路径

Application.Templates.Item(Application.Templates.count - 1)

基于获得的矩形范围生成图片

///   
/// 输出当前显示范围为JPG图像  
///   
/// 显示范围  
/// 保存路径  
/// 显示控件  
private void PRV_ExportToJPG(IEnvelope IN_Envolope, string IN_SaveFile,AxMapControl IN_AxmapControl) 
{  
    //默认为当前显示区域  
    if (IN_Envolope == null)  
        IN_Envolope = IN_AxmapControl.ActiveView.Extent;  
    //输出为JPG  
    IExport Temp_Exporter = new ExportJPEGClass(); ;  
    //获取活动窗体  
    IActiveView Temp_CurrentActiveView = IN_AxmapControl.ActiveView;  
    //要素实际宽高  
    IPoint pntMin = new PointClass();
    pntMin.X= IN_Envolope.XMin;
    pntMin.Y= IN_Envolope.YMin;
    IPoint  pntMax=new PointClass();
    pntMax.X= IN_Envolope.XMax;
    pntMax.Y= IN_Envolope.YMax;
    int xmin= 0, ymin =0, xmax=0, ymax=0;
    Temp_CurrentActiveView.ScreenDisplay.DisplayTransformation.FromMapPoint (pntMax,out xmax, out ymax);
Temp_CurrentActiveView.ScreenDisplay.DisplayTransformation.FromMapPoint (pntMin, out xmin, out ymin);
    int Temp_featureWidth = Math.Abs(xmin-xmax);  
    int Temp_featureHeight =Math.Abs(ymin-ymax);  
    //根据要素实际情况确定图片实际大小  
    tagRECT Temp_TtagRECT;  
    Temp_TtagRECT.left = 0;  
    Temp_TtagRECT.top = 0;  
    Temp_TtagRECT.right = IN_Envolope.Width;
    Temp_TtagRECT.bottom = IN_Envolope.Height;  
    //定义DPI  
    int Temp_DPI = 100;  
    //设定输出参数  
    IEnvelope Temp_Envelope = new EnvelopeClass();  
    Temp_Envelope.PutCoords(Temp_TtagRECT.left, Temp_TtagRECT.top, Temp_TtagRECT.right, Temp_TtagRECT.bottom);  
    Temp_Exporter.Resolution = Temp_DPI;  
    Temp_Exporter.ExportFileName = IN_SaveFile;  
    Temp_Exporter.PixelBounds = Temp_Envelope;  
    //输出  
    Temp_CurrentActiveView.Output(Temp_Exporter.StartExporting(), Temp_DPI, ref Temp_TtagRECT, IN_Envolope, null);  
    Temp_Exporter.FinishExporting();  
    //释放资源  
    System.Runtime.InteropServices.Marshal.ReleaseComObject(Temp_Exporter);  
}  

参考
画矩形
ArcEngine输出当前显示范围为JPG图像
如何获取MXD文件的路径

你可能感兴趣的:(Add-in for Arcgis Desktop框选指定区域生成图片)