• ArcGIS Engine开发系列:将地图导出为图片的两种方法


    本文转自:http://blog.163.com/ljm1113@126/blog/static/5798445220104362242817/

    在ArcGIS的开发中,我们经常需要将当前地图打印(或是转出)到图片文件中。将Map或Layout中的图象转出有两种方法,一种为通过IActiveView的OutPut函数,另外一种是通过IExport接口来实现。第一种方法导出速度较快,实现也比较方便,但该方法对于图片的行或列数超过10000左右时,导出经常会失败(具体原因未知),第二种方法导出速度较慢,但效果较好,且可以在导出过程中通过ITrackCancel来中止导出操作。

      通过IActiveView的方式导出是通过创建Graphics对象来实现,具体示例代码如下:
     1 /// <summary>
     2 
     3 /// 将Map上指定范围(该范围为规则区域)内的内容输出到Image,注意,当图片的行数或列数超过10000左右时,出现原因示知的失败
     4 
     5 /// </summary>
     6 
     7 /// <param name="pMap">需转出的MAP</param>
     8 /// <param name="outRect">输出的图片大小</param>
     9 /// <param name="pEnvelope">指定的输出范围(为Envelope类型)</param>
    10 /// <returns>输出的Image 具体需要保存为什么格式,可通过Image对象来实现</returns>
    11 public static Image SaveCurrentToImage(IMap pMap, Size outRect, IEnvelope pEnvelope)
    12  {
    13       //赋值
    14       tagRECT rect = new tagRECT();
    15       rect.left = rect.top = 0;
    16       rect.right = outRect.Width;
    17       rect.bottom = outRect.Height;
    18       try
    19       {                
    20           //转换成activeView,若为ILayout,则将Layout转换为IActiveView
    21           IActiveView pActiveView = (IActiveView)pMap;
    22           // 创建图像,为24位色
    23           Image image = new Bitmap(outRect.Width, outRect.Height); //, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    24           System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
    25 
    26           // 填充背景色(白色)
    27           g.FillRectangle(Brushes.White, 0, 0, outRect.Width, outRect.Height);
    28 
    29           int dpi = (int)(outRect.Width / pEnvelope.Width);
    30 
    31           pActiveView.Output(g.GetHdc().ToInt32(), dpi, ref rect, pEnvelope, null);
    32 
    33           g.ReleaseHdc();            
    34 
    35           return image;
    36      }
    37 
    38      catch (Exception excp)
    39      {
    40         MessageBox.Show(excp.Message + "将当前地图转出出错,原因未知", "出错提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
    41 
    42           return null;
    43       }
    44  }

     通过IExport接口实现的导出,也需要通过IActiveView的OutPut来实现,但其转出句柄为IExport的StartExporting函数返回的DC,具体示例代码如下:

    //输出当前地图至指定的文件    
    public void ExportMapExtent(IActiveView pView, Size outRect,string outPath)
    {           
        try
        {
            //参数检查
            if pView == null )
            {
                throw new Exception("输入参数错误,无法生成图片文件!");
            }  
            //根据给定的文件扩展名,来决定生成不同类型的对象
            ESRI.ArcGIS.Output.IExport export = null;
            if (outPath.EndsWith(".jpg"))
            {
                export = new ESRI.ArcGIS.Output.ExportJPEGClass();
            }
            else if (outPath.EndsWith(".tiff"))
            {
                export = new ESRI.ArcGIS.Output.ExportTIFFClass();
            }
            else if (outPath.EndsWith(".bmp"))
            {
                export = new ESRI.ArcGIS.Output.ExportBMPClass();
            }
            else if (outPath.EndsWith(".emf"))
            {
                export = new ESRI.ArcGIS.Output.ExportEMFClass();
            }
            else if (outPath.EndsWith(".png"))
            {
                export = new ESRI.ArcGIS.Output.ExportPNGClass();
            }
            else if (outPath.EndsWith(".gif"))
            {
                export = new ESRI.ArcGIS.Output.ExportGIFClass();
            }
    
            export.ExportFileName = outPath;
            IEnvelope pEnvelope = pView.Extent;
            //导出参数           
            export.Resolution = 300;
            tagRECT exportRect = new tagRECT();
            exportRect.left = exportRect.top = 0;
            exportRect.right = outRect.Width;
            exportRect.bottom = (int)(exportRect.right * pEnvelope.Height / pEnvelope.Width);
            ESRI.ArcGIS.Geometry.IEnvelope envelope = new ESRI.ArcGIS.Geometry.EnvelopeClass();
            //输出范围
            envelope.PutCoords(exportRect.left, exportRect.top, exportRect.right, exportRect.bottom);
            export.PixelBounds = envelope;
            //可用于取消操作
            ITrackCancel pCancel = new CancelTrackerClass();
            export.TrackCancel = pCancel;
            pCancel.Reset();
            //点击ESC键时,中止转出
            pCancel.CancelOnKeyPress = true;
            pCancel.CancelOnClick = false;
            pCancel.ProcessMessages = true;
            //获取handle
            System.Int32 hDC = export.StartExporting();
            //开始转出
            pView.Output(hDC, (System.Int16)export.Resolution, ref exportRect, pEnvelope, pCancel);
            bool bContinue = pCancel.Continue();
            //捕获是否继续
            if (bContinue)
            {                              
                export.FinishExporting();
                export.Cleanup();
            }
            else
            {                  
                export.Cleanup();
            }
            bContinue = pCancel.Continue();               
        }
        catch (Exception excep)
        {
            //错误信息提示
        }
    
    }
  • 相关阅读:
    基本计数方法
    每天工作4小时的程序员
    明星软件工程师的10种特质(转)
    IT高薪者所具备的人格魅力
    Unity_Shader开发_图形学基础(五)--------2016.1.9
    unity 架构设计的学习
    深入浅出聊优化:从Draw Calls到GC
    PG+mask替代透明Png(转)
    基于战斗重演的全校验---- 塔防大师PVP反外挂设计
    Unity项目开发准则
  • 原文地址:https://www.cnblogs.com/daidaigua/p/2471436.html
Copyright © 2020-2023  润新知