当前位置: 代码迷 >> CGI >> ArcGIS Engine开发系列:将map导出为图片的两种方法
  详细解决方案

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

热度:518   发布时间:2012-07-04 19:33:54.0
ArcGIS Engine开发系列:将地图导出为图片的两种方法

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

Java代码 复制代码
  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. ?}??
/// <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,具体示例代码如下:

Java代码 复制代码
  1. //输出当前地图至指定的文件???? ??
  2. public ? void ?ExportMapExtent(IActiveView?pView,?Size?outRect,string?outPath) ??
  3. {??????????? ??
  4. ???? try ??
  5. ????{ ??
  6. ???????? //参数检查 ??
  7. ???????? if ?pView?==? null ?) ??
  8. ????????{ ??
  9. ???????????? throw ? new ?Exception( "输入参数错误,无法生成图片文件!" ); ??
  10. ????????}?? ??
  11. ???????? //根据给定的文件扩展名,来决定生成不同类型的对象 ??
  12. ????????ESRI.ArcGIS.Output.IExport?export?=? null ; ??
  13. ???????? if ?(outPath.EndsWith( ".jpg" )) ??
  14. ????????{ ??
  15. ????????????export?=? new ?ESRI.ArcGIS.Output.ExportJPEGClass(); ??
  16. ????????} ??
  17. ???????? else ? if ?(outPath.EndsWith( ".tiff" )) ??
  18. ????????{ ??
  19. ????????????export?=? new ?ESRI.ArcGIS.Output.ExportTIFFClass(); ??
  20. ????????} ??
  21. ???????? else ? if ?(outPath.EndsWith( ".bmp" )) ??
  22. ????????{ ??
  23. ????????????export?=? new ?ESRI.ArcGIS.Output.ExportBMPClass(); ??
  24. ????????} ??
  25. ???????? else ? if ?(outPath.EndsWith( ".emf" )) ??
  26. ????????{ ??
  27. ????????????export?=? new ?ESRI.ArcGIS.Output.ExportEMFClass(); ??
  28. ????????} ??
  29. ???????? else ? if ?(outPath.EndsWith( ".png" )) ??
  30. ????????{ ??
  31. ????????????export?=? new ?ESRI.ArcGIS.Output.ExportPNGClass(); ??
  32. ????????} ??
  33. ???????? else ? if ?(outPath.EndsWith( ".gif" )) ??
  34. ????????{ ??
  35. ????????????export?=? new ?ESRI.ArcGIS.Output.ExportGIFClass(); ??
  36. ????????} ??
  37. ??
  38. ????????export.ExportFileName?=?outPath; ??
  39. ????????IEnvelope?pEnvelope?=?pView.Extent; ??
  40. ???????? //导出参数??????????? ??
  41. ????????export.Resolution?=? 300 ; ??
  42. ????????tagRECT?exportRect?=? new ?tagRECT(); ??
  43. ????????exportRect.left?=?exportRect.top?=? 0 ; ??
  44. ????????exportRect.right?=?outRect.Width; ??
  45. ????????exportRect.bottom?=?( int )(exportRect.right?*?pEnvelope.Height?/?pEnvelope.Width); ??
  46. ????????ESRI.ArcGIS.Geometry.IEnvelope?envelope?=? new ?ESRI.ArcGIS.Geometry.EnvelopeClass(); ??
  47. ???????? //输出范围 ??
  48. ????????envelope.PutCoords(exportRect.left,?exportRect.top,?exportRect.right,?exportRect.bottom); ??
  49. ????????export.PixelBounds?=?envelope; ??
  50. ???????? //可用于取消操作 ??
  51. ????????ITrackCancel?pCancel?=? new ?CancelTrackerClass(); ??
  52. ????????export.TrackCancel?=?pCancel; ??
  53. ????????pCancel.Reset(); ??
  54. ???????? //点击ESC键时,中止转出 ??
  55. ????????pCancel.CancelOnKeyPress?=? true ; ??
  56. ????????pCancel.CancelOnClick?=? false ; ??
  57. ????????pCancel.ProcessMessages?=? true ; ??
  58. ???????? //获取handle ??
  59. ????????System.Int32?hDC?=?export.StartExporting(); ??
  60. ???????? //开始转出 ??
  61. ????????pView.Output(hDC,?(System.Int16)export.Resolution,?ref?exportRect,?pEnvelope,?pCancel); ??
  62. ????????bool?bContinue?=?pCancel.Continue(); ??
  63. ???????? //捕获是否继续 ??
  64. ???????? if ?(bContinue) ??
  65. ????????{?????????????????????????????? ??
  66. ????????????export.FinishExporting(); ??
  67. ????????????export.Cleanup(); ??
  68. ????????} ??
  69. ???????? else ??
  70. ????????{?????????????????? ??
  71. ????????????export.Cleanup(); ??
  72. ????????} ??
  73. ????????bContinue?=?pCancel.Continue();??????????????? ??
  74. ????} ??
  75. ???? catch ?(Exception?excep) ??
  76. ????{ ??
  77. ???????? //错误信息提示 ??
  78. ????} ??
  79. }?
  相关解决方案