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

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

}
版权说明

  如果标题未标有<转载、转>等字则属于作者原创,欢迎转载,其版权归作者和博客园共有。
  作      者:温景良
  文章出处:http://wenjl520.cnblogs.com/  或  http://www.cnblogs.com/

posted @ 2009-03-30 15:13 温景良(Jason) Views( 874) Comments( 1) Edit 收藏

  
#1楼 1493996 2009/4/2 13:03:42 2009-04-02 13:03 | typeb
不能超过10000,可能是因为DC的原因

公告

hidden hit counter
 
 
本文转自  

我的程序人生博客园博客,原文链接:http://www.cnblogs.com/wenjl520/archive/2009/03/30/1425157.html,如需转载请自行联系原作者

 
相关文章
|
12月前
|
人工智能 NoSQL 定位技术
标准地图的矢量模板,ArcGIS可打开
标准地图的矢量模板,ArcGIS可打开
139 0
|
定位技术
ArcGIS:地图单位和视图单位(显示单位)的区别?
ArcGIS:地图单位和视图单位(显示单位)的区别?
147 0
|
5月前
|
定位技术 Python
ArcGIS中ArcMap通过模型构建器ModelBuilder导出地理与投影坐标系转换Python代码的方法
ArcGIS中ArcMap通过模型构建器ModelBuilder导出地理与投影坐标系转换Python代码的方法
107 2
|
5月前
|
人工智能 编解码 定位技术
ArcGIS导出AI或EPS格式的地图图片并在Adobe Illustrator中继续编辑
ArcGIS导出AI或EPS格式的地图图片并在Adobe Illustrator中继续编辑
273 1
|
5月前
|
存储 定位技术
ArcGIS中ArcMap导入mxd地图文档文件出现红色感叹号、地图空白的解决
ArcGIS中ArcMap导入mxd地图文档文件出现红色感叹号、地图空白的解决
154 1
|
5月前
|
编解码 定位技术 Python
Python中ArcPy实现ArcGIS自动批量制图与地图要素批量设置
Python中ArcPy实现ArcGIS自动批量制图与地图要素批量设置
162 1
|
定位技术 数据格式
GIS开发:arcgis server发布CGCS2000切片
GIS开发:arcgis server发布CGCS2000切片
219 0
|
SQL 开发框架 数据可视化
ArcGIS Engine学习系列1 AE基础介绍
市面上AE教程大致到ArcGIS10.2,ESRI宣布从ArcGIS10.5开始便停止AE的更新,使用AO做开发,初学者可以在学习C#语言后,从AE入门,逐渐过度到AO。不同版本下AE数据类型数量Enums:枚举类型,用于实现一些定义的内容Structs:结构体Interfaces:接口Classes:类AE开发中,为了更好地管理COM对象,ESRI将这些COM对象放到不同的组件库中。
285 0
如何解决GEE导出影像的Nodata值在ArcGIS中无法正常显示?
如何解决GEE导出影像的Nodata值在ArcGIS中无法正常显示?
785 0
|
12月前
|
人工智能 数据可视化 API
ArcGIS API for Python
ArcGIS API for Python
60 0

相关实验场景

更多
下一篇
无影云桌面