C#+ArcEngine“ 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。

数据较小时偶尔出现,数据较大时经常出现(经常一次性添加多个图层),有时是在添加时报错,有时是在放大缩小时报错,以下是代码:
private IActiveViewEvents_Event activiewEvent;
public FrmMain()
        {            
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
            InitializeComponent();
            activiewEvent = axMapControl1.Map as IActiveViewEvents_Event;
            activiewEvent.ItemAdded += activiewEvent_ItemAdded;
            activiewEvent.ItemDeleted += activiewEvent_ItemDeleted;
            activiewEvent.ItemReordered += activiewEvent_ItemReordered;
            activiewEvent.ViewRefreshed += activiewEvent_ViewRefreshed;
        }
void activiewEvent_ViewRefreshed(IActiveView View, esriViewDrawPhase phase, object Data, IEnvelope envelope)
        {
            int i;
            axMapControl2.ClearLayers();
            for (i = axMapControl1.LayerCount - 1; i >= 0; i--)
            {
                axMapControl2.AddLayer(axMapControl1.get_Layer(i));
            }
            RefreshView();
        }

        void activiewEvent_ItemReordered(object Item, int toIndex)
        {
            int i;
            axMapControl2.ClearLayers();
            for (i = axMapControl1.LayerCount - 1; i >= 0; i--)
            {
                axMapControl2.AddLayer(axMapControl1.Map.get_Layer(i));
            }
            RefreshView();
        }

        void activiewEvent_ItemDeleted(object Item)
        {
            int i;
            axMapControl2.ClearLayers();
            for (i = axMapControl1.LayerCount - 1; i >= 0; i--)
            {
                axMapControl2.AddLayer(axMapControl1.Map.get_Layer(i));
            }
            RefreshView();
        }

        void activiewEvent_ItemAdded(object Item)
        {
            int i;
            axMapControl2.ClearLayers();
            for (i = axMapControl1.LayerCount - 1; i >= 0; i--)
            {
                axMapControl2.AddLayer(axMapControl1.Map.get_Layer(i));
            }
            RefreshView();
        }

        private void RefreshView()
        {
            //创建鹰眼中线框pElement
            IEnvelope envelope = (IEnvelope)axMapControl1.Extent;
            IRectangleElement rectangleElement = new RectangleElementClass();
            IElement element = rectangleElement as IElement;
            element.Geometry = envelope;

            //设置线框的边线对象lineColor,包括颜色和线宽
            IRgbColor lineColor = new RgbColorClass();
            lineColor.Red = 255;
            lineColor.Green = 0;
            lineColor.Blue = 0;
            lineColor.Transparency = 255;
            #endregion

            //产生一个线符号对象lineSymbol
            ILineSymbol lineSymbol = new SimpleLineSymbolClass();
            lineSymbol.Width = 1;
            lineSymbol.Color = lineColor;

            //设置填充颜色属性fillColor
            IRgbColor fillColor = new RgbColorClass();
            fillColor.Red = 255;
            fillColor.Green = 0;
            fillColor.Blue = 0;
            fillColor.Transparency = 0;

            //设置线框填充符号的属性
            IFillSymbol fillSymbol = new SimpleFillSymbolClass();
            fillSymbol.Color = fillColor;
            fillSymbol.Outline = lineSymbol;
            IFillShapeElement fillShapeEle = element as IFillShapeElement;
            fillShapeEle.Symbol = fillSymbol;

            //鹰眼视图中添加线框
            IGraphicsContainer graContainer = axMapControl2.Map as IGraphicsContainer; 
            graContainer.DeleteAllElements(); 
            graContainer.AddElement((IElement)fillShapeEle, 0); 

            axMapControl2.Extent = axMapControl1.FullExtent;
        }

        private void axMapControl1_OnExtentUpdated(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnExtentUpdatedEvent e)
        {
            RefreshView();
        }

        //当鼠标点击鹰眼窗体时,主窗体Extent随之改变
        private void axMapControl2_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
        {
            if (axMapControl2.Map.LayerCount != 0)
            {
                //按下鼠标左键移动矩形框,按下鼠标右键绘制矩形框
                if (e.button == 1)
                {
                    IPoint point = new PointClass();
                    point.PutCoords(e.mapX, e.mapY);
                    IEnvelope envelope = axMapControl1.Extent;
                    envelope.CenterAt(point);
                    axMapControl1.Extent = envelope;
                    axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
                }
                else if (e.button == 2)
                {
                    IEnvelope envelope = axMapControl2.TrackRectangle();
                    axMapControl1.Extent = envelope;
                    axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
                }
            }
        }

        //当鼠标在鹰眼窗体移动时,主窗体Extent随之改变。
        private void axMapControl2_OnMouseMove(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e)
        {
            if (e.button == 1)
            {
                IPoint point = new PointClass();
                point.PutCoords(e.mapX, e.mapY);
                axMapControl1.CenterAt(point);
                axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
            }
        }

        //主窗口地图替换后
        private void axMapControl1_OnMapReplaced(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMapReplacedEvent e)
        {
            if (axMapControl1.LayerCount != 0)
            {
                axMapControl2.Map = new MapClass();
                for (int i = 0; i <= axMapControl1.LayerCount - 1; i++)
                {
                    axMapControl2.AddLayer(axMapControl1.get_Layer(axMapControl1.LayerCount - i - 1));
                }
                axMapControl2.Extent = axMapControl1.FullExtent;
                axMapControl2.Refresh();
            }
        }

error.JPG

 
已邀请:

xiaosi9524 - GISER

赞同来自:

检查下逻辑,是不是图面还是缩放时,你clear了图层之类的操作

要回复问题请先登录注册