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

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

/// <summary>

/// 将Map上指定范围(该范围为规则区域)内的内容输出到Image,注意,当图片的行数或列数超过10000左右时,出现原因示知的失败

/// </summary>

/// <param name="pMap">需转出的MAP</param>
/// <param name="outRect">输出的图片大小</param>
/// <param name="pEnvelope">指定的输出范围(为Envelope类型)</param>
/// <returns>输出的Image 具体需要保存为什么格式,可通过Image对象来实现</returns>
public static Image SaveCurrentToImage(IMap pMap, Size outRect, IEnvelope pEnvelope)
 {
      //赋值
      tagRECT rect = new tagRECT();
      rect.left = rect.top = 0;
      rect.right = outRect.Width;
      rect.bottom = outRect.Height;
      try
      {                
          //转换成activeView,若为ILayout,则将Layout转换为IActiveView
          IActiveView pActiveView = (IActiveView)pMap;

          // 创建图像,为24位色
          Image image = new Bitmap(outRect.Width, outRect.Height); //, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
          System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);

          // 填充背景色(白色)
          g.FillRectangle(Brushes.White, 0, 0, outRect.Width, outRect.Height);

          int dpi = (int)(outRect.Width / pEnvelope.Width);

          pActiveView.Output(g.GetHdc().ToInt32(), dpi, ref rect, pEnvelope, null);

          g.ReleaseHdc();            

          return image;
     }

     catch (Exception excp)
     {
        MessageBox.Show(excp.Message + "将当前地图转出出错,原因未知", "出错提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

          return null;
      }
 }

   通过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"))
    

转载于:https://www.cnblogs.com/wenchang_2008/archive/2009/03/30/1424712.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值