ARCGIS

ARCGIS
ARCGIS

///

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

///

/// 需转出的MAP

/// 输出的图片大小

/// 指定的输出范围(为Envelope类型)/// 输出的Image 具体需要保存为什么格式,可通过Image对象来实现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"))

{

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)

{

//错误信息提示

}

}

(2)添加使用地图整饰对象(图例、指北针、比例尺)

新建工具用于在PagelayoutControl上添加地图整饰对象

修改工具按钮点击相应函数

public override void OnClick()

{

// TODO: Add AddMapSurround.OnClick implementation

//get the MapControl from the hook in case the container is a ToolbarControl

if (m_hookHelper.Hook is IToolbarControl)

{

pagelayoutControl = (IPageLayoutControl3)((IToolbarControl)m_hookHelper.Hook).Buddy;

}

//In case the container is MapControl

else if (m_hookHelper.Hook is IPageLayoutControl3)

{

pagelayoutControl = (IPageLayoutControl3)m_hookHelper.Hook;

}

else

{

MessageBox.Show("Active control must be MapControl!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

return;

}

}

添加创建地图整饰对象成员函数

private IMapSurround CreateMapSurround(UID ipUID, IEnvelope ipEnv, string sName, IPageLayoutControl3 ipLayoutCtrl, IMapSurround ipStyle)

{

IMapSurround ms = null;

IMapFrame mf = ipLayoutCtrl.ActiveView.GraphicsContainer.FindFrame(

ipLayoutCtrl.ActiveView.FocusMap) as IMapFrame;

IMapSurroundFrame msf = mf.CreateSurroundFrame(ipUID, ipStyle);

IElement ele = msf as IElement;

ele.Geometry = ipEnv;

ms = msf.MapSurround;

https://www.360docs.net/doc/d52430137.html, = sName;

ipLayoutCtrl.ActiveView.GraphicsContainer.AddElement(ele, 0);

ipLayoutCtrl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);

return ms;

}

在鼠标按下事件中添加图例

public override void OnMouseDown(int Button, int Shift, int X, int Y)

{

// TODO: Add AddMapSurround.OnMouseDown implementation

IRubberBand ipRubber = new RubberEnvelopeClass();

IEnvelope env = ipRubber.TrackNew(pagelayoutControl.ActiveView.ScreenDisplay,

null) as IEnvelope;

//初始化图例信息

UID uid = new UIDClass();

uid.Value = "esriCarto.Legend";

CreateMapSurround(uid,env,"我的图例",pagelayoutControl,null);

}

添加指北针

public override void OnMouseDown(int Button, int Shift, int X, int Y)

{

// TODO: Add AddMapSurround.OnMouseDown implementation

IRubberBand ipRubber = new RubberEnvelopeClass();

IEnvelope env = ipRubber.TrackNew(pagelayoutControl.ActiveView.ScreenDisplay,

null) as IEnvelope;

//初始化图例信息

UID uid = new UIDClass();

uid.Value = "esriCarto.MarkerNorthArrow";

CreateMapSurround(uid,env,"我的指北针",pagelayoutControl,null);

}

添加比例尺条

public override void OnMouseDown(int Button, int Shift, int X, int Y)

{

// TODO: Add AddMapSurround.OnMouseDown implementation

IRubberBand ipRubber = new RubberEnvelopeClass();

IEnvelope env = ipRubber.TrackNew(pagelayoutControl.ActiveView.ScreenDisplay,

null) as IEnvelope;

//初始化图例信息

UID uid = new UIDClass();

uid.Value = "esriCarto.ScaleLine";

CreateMapSurround(uid,env,"我的比例尺",pagelayoutControl,null);

}

添加比例尺文本

public override void OnMouseDown(int Button, int Shift, int X, int Y)

{

// TODO: Add AddMapSurround.OnMouseDown implementation

IRubberBand ipRubber = new RubberEnvelopeClass();

IEnvelope env = ipRubber.TrackNew(pagelayoutControl.ActiveView.ScreenDisplay,

null) as IEnvelope;

//初始化图例信息

UID uid = new UIDClass();

uid.Value = "esriCarto.ScaleText";

CreateMapSurround(uid,env,"我的比例尺",pagelayoutControl,null);

}

在主窗口中添加菜单,使用该工具

private void mapSurroundToolStripMenuItem_Click(object sender, EventArgs e)

{

ICommand command = new AddMapSurround();

command.OnCreate(m_pagelayoutControl.Object);

command.OnClick();

axPageLayoutControl1.CurrentTool = command as ITool;

}

相关主题
相关文档
最新文档