`
NickWar
  • 浏览: 73372 次
  • 来自: 南京
文章分类
社区版块
存档分类

用GDI+进行地物矩形剪裁

 
阅读更多

最近要做个裁减的小程序,一开始本来从底层考虑算法,后来查资料发现GDI+中的Region类可以完成这个功能,于是乎,懒惰一下.下面的文字摘自说明书,也懒得改序号,大家都能看懂,哈哈

1. 主要程序及注释

3.1 运行程序

用右键拖出一个矩形,用于裁减,用左键在界面上的绘图区域绘制出一个多边形。如图1 所示。<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"></shapetype>

<shapetype stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 414.75pt; HEIGHT: 330pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:/DOCUME~1/Game/LOCALS~1/Temp/msohtml1/12/clip_image001.png"></imagedata></shape>

程序界面

1 程序界面

<chsdate w:st="on" isrocdate="False" islunardate="False" day="30" month="12" year="1899"><strong style="mso-bidi-font-weight: normal"><span lang="EN-US" style="FONT-SIZE: 14pt; LINE-HEIGHT: 150%; FONT-FAMILY: 黑体; mso-hansi-font-family: 宋体">3.1.1</span></strong></chsdate> 绘图主要代码

<chsdate w:st="on" isrocdate="False" islunardate="False" day="30" month="12" year="1899">3.1.1</chsdate>.1 在类的构造函数中添加以下代码:

//声明一个用于存储点的列表----------

//----------------------------------

ArrayList pList = new ArrayList();

//用于绘制图形的点数组--------------

Point[] p;

//用于图形重绘的graphics

Graphics gra ;

//定义用于裁减的区域和绘图区-------------

//---------------------

System.Drawing.Region reg,excludeReg,unionReg,intersectReg,complementReg,xorReg;

Graphics excludeGra,unionGra,intersectGra,complementGra,xorGra;

Rectangle rect;

GraphicsPath gp = new GraphicsPath();

//设置重绘标志-----------------

//-----------------------------

int x = -1;

//绘制矩形指示标志------------------

//----------------------------------

bool nRect = false;

//定义用于绘制裁减矩形的点---------

//---------------------------------

Point starpt;

Point endpt ;

//定义画笔------------------------

//--------------------------------

Pen pen = new Pen(Color.Red);

Form_Load事件中:

private void Form1_Load(object sender, System.EventArgs e)

{

gra = panel1.CreateGraphics();

}

<chsdate w:st="on" isrocdate="False" islunardate="False" day="30" month="12" year="1899"><strong style="mso-bidi-font-weight: normal"><span lang="EN-US" style="FONT-SIZE: 12pt; LINE-HEIGHT: 150%; FONT-FAMILY: 黑体; mso-hansi-font-family: 宋体">3.1.1</span></strong></chsdate>.2 在绘图区的Paint事件中添加一下代码:

if(this.nRect == true)

{

rect = new Rectangle(this.starpt.X,this.starpt.Y,this.endpt.X - this.starpt.X,this.endpt.Y - this.starpt.Y);

gra.DrawRectangle(new Pen(Color.Red),rect);

}

if(pList.Count>=1)

{

this.p = (Point[])pList.ToArray(this.pList[0].GetType());

if(pList.Count>5)

{

this.gp.Reset();

gra.DrawPolygon(new Pen(Color.WhiteSmoke),this.p);

this.gp.AddPolygon(this.p);

reg = new System.Drawing.Region(gp);

}

for(int i = 0;i<pList.Count;i++)

{

gra.DrawEllipse(new Pen(Color.HotPink),this.p[i].X-1,p[i].Y-1,2,2);

}

}

//根据标志重绘裁减区域-------------------

//-----------------------------------------

if(this.x == 5)

this.intersectGra.FillRegion(Brushes.Yellow,this.intersectReg);

if(this.x == 3)

this.unionGra.FillRegion(Brushes.Green,this.unionReg);

if(this.x == 2)
this.complementGra.FillRegion(Brushes.Aqua,this.complementReg);

if(x == 4)

this.excludeGra.FillRegion(Brushes.Red,this.excludeReg);

if(this.x == 1)

this.xorGra.FillRegion(Brushes.Black,this.xorReg);

<chsdate w:st="on" isrocdate="False" islunardate="False" day="30" month="12" year="1899"><strong style="mso-bidi-font-weight: normal"><span lang="EN-US" style="FONT-SIZE: 12pt; LINE-HEIGHT: 150%; FONT-FAMILY: 黑体; mso-font-kerning: 0pt; mso-hansi-font-family: 宋体">3.1.1</span></strong></chsdate>.3 mouseDown事件中处理代码如下:

if(e.Button == MouseButtons.Left)

{

this.pList.Add(new Point(e.X,e.Y));

this.panel1.Invalidate();

}

if(e.Button == MouseButtons.Right)

{

this.starpt = new Point(e.X,e.Y);

this.endpt = new Point(e.X,e.Y);

}

<chsdate w:st="on" isrocdate="False" islunardate="False" day="30" month="12" year="1899"><strong style="mso-bidi-font-weight: normal"><span lang="EN-US" style="FONT-SIZE: 12pt; LINE-HEIGHT: 150%; FONT-FAMILY: 黑体; mso-font-kerning: 0pt; mso-hansi-font-family: 宋体">3.1.1</span></strong></chsdate>.4 mouseMove事件的处理函数如下:

private void panel1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)

{

if(e.Button == MouseButtons.Right)

{ //实时绘制矩形效果

Pen clearpen = new Pen(this.panel1.BackColor);

this.gra.DrawRectangle(clearpen,this.starpt.X,this.starpt.Y,this.endpt.X - this.starpt.X,this.endpt.Y - this.starpt.Y);

this.endpt = new Point(e.X,e.Y);

this.gra.DrawRectangle(this.pen,this.starpt.X,this.starpt.Y,this.endpt.X - this.starpt.X,this.endpt.Y - this.starpt.Y);

}

}

<chsdate w:st="on" isrocdate="False" islunardate="False" day="30" month="12" year="1899"><strong style="mso-bidi-font-weight: normal"><span lang="EN-US" style="FONT-SIZE: 12pt; LINE-HEIGHT: 150%; FONT-FAMILY: 黑体; mso-font-kerning: 0pt; mso-hansi-font-family: 宋体">3.1.1</span></strong></chsdate>.5 mouseUp事件处理函数代码:

private void panel1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)

{

if(e.Button == MouseButtons.Right)

{

this.endpt = new Point(e.X,e.Y);

this.gra.DrawRectangle(this.pen,this.starpt.X,this.starpt.Y,this.endpt.X - this.starpt.X,this.endpt.Y - this.starpt.Y);

rect = new Rectangle(this.starpt.X,this.starpt.Y,this.endpt.X - this.starpt.X,this.endpt.Y - this.starpt.Y);

this.nRect = true;

}

}

3.2 命令菜单如图2

命令菜单

<shape id="_x0000_i1026" style="WIDTH: 116.25pt; HEIGHT: 113.25pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:/DOCUME~1/Game/LOCALS~1/Temp/msohtml1/12/clip_image003.png"></imagedata></shape>

2 命令菜单

<chsdate w:st="on" isrocdate="False" islunardate="False" day="30" month="12" year="1899"><strong style="mso-bidi-font-weight: normal"><span lang="EN-US" style="FONT-SIZE: 14pt; FONT-FAMILY: 黑体; mso-hansi-font-family: 宋体">3.2.1</span></strong></chsdate> 求交处理效果见图3,函数代码如下

求交效果

<shape id="_x0000_i1027" style="WIDTH: 414.75pt; HEIGHT: 330pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:/DOCUME~1/Game/LOCALS~1/Temp/msohtml1/12/clip_image005.png"></imagedata></shape>

3 求交效果
private void menuItem4_Click(object sender, System.EventArgs e)

{

this.intersectReg = new System.Drawing.Region(this.gp);

this.intersectGra = panel1.CreateGraphics();

try

{

//r1.Intersect(r2); r1更新为r1r2的交集(即同时包含在r1r2中的部分)

this.intersectReg.Intersect(rect);

//清除其他区域以突出显示目标区域--------------------------

//---------------------------------------------------------

if(this.excludeReg != null)

this.excludeGra.FillRegion(Brushes.Gray,this.excludeReg);

if(this.unionReg != null)

this.unionGra.Clear(this.panel1.BackColor);

if(this.xorReg != null)

this.xorGra.Clear(this.panel1.BackColor);

if(this.complementReg != null)

this.complementGra.Clear(this.panel1.BackColor);

//填充目标区域-------------------------------------

//-------------------------------------------------

this.intersectGra.FillRegion(Brushes.Yellow,this.intersectReg);

this.x = 5;

}

catch(Exception ex)

{

MessageBox.Show(ex.Message.ToString());

}

}

<chsdate w:st="on" isrocdate="False" islunardate="False" day="30" month="12" year="1899"><strong style="mso-bidi-font-weight: normal"><span lang="EN-US" style="FONT-SIZE: 14pt; LINE-HEIGHT: 150%; FONT-FAMILY: 黑体; mso-font-kerning: 0pt; mso-hansi-font-family: 宋体">3.2.2</span></strong></chsdate> 裁减效果见图4,代码如下

private void menuItem6_Click(object sender, System.EventArgs e)

{

this.excludeReg = new System.Drawing.Region(this.gp);

this.excludeReg.Exclude(this.rect);

// r1.Exclude(r2) 更新r1,使之不包含任何位于r2中的部分

this.excludeGra = panel1.CreateGraphics();

//清除其他区域以突出显示目标区域--------------------------

//---------------------------------------------------------

if(this.intersectReg != null)

this.intersectGra.FillRegion(Brushes.Gray,this.intersectReg);

if(this.unionReg != null)

this.unionGra.Clear(this.panel1.BackColor);

if(this.xorReg != null)

this.xorGra.Clear(this.panel1.BackColor);

if(this.complementReg != null)

this.complementGra.Clear(this.panel1.BackColor);

//填充目标区域-------------------------------------

//-------------------------------------------------

this.excludeGra.FillRegion(Brushes.Red,this.excludeReg);

this.x = 4;

}

裁减效果

<shape id="_x0000_i1028" style="WIDTH: 414.75pt; HEIGHT: 328.5pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:/DOCUME~1/Game/LOCALS~1/Temp/msohtml1/12/clip_image007.png"></imagedata></shape>

4 裁减效果

<chsdate w:st="on" isrocdate="False" islunardate="False" day="30" month="12" year="1899"><strong style="mso-bidi-font-weight: normal"><span lang="EN-US" style="FONT-SIZE: 14pt; LINE-HEIGHT: 150%; FONT-FAMILY: 黑体; mso-font-kerning: 0pt; mso-hansi-font-family: 宋体">3.2.3</span></strong></chsdate> 并集效果见图5,代码如下

<shape id="_x0000_i1029" style="WIDTH: 414.75pt; HEIGHT: 327.75pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:/DOCUME~1/Game/LOCALS~1/Temp/msohtml1/12/clip_image009.png"></imagedata></shape>

5 并集运算效果

private void menuItem7_Click(object sender, System.EventArgs e)

{

this.unionReg = new System.Drawing.Region(this.gp);

this.unionReg.Union(this.rect);

//r1.Union(r2); r1更新为r1r2的并集(即包含在r1r2中的部分,或同

//时包含在r1r2中的部分)

this.unionGra = panel1.CreateGraphics();

//清除其他区域以突出显示目标区域--------------------------

//---------------------------------------------------------

if(this.intersectReg != null)

this.intersectGra.FillRegion(Brushes.Gray,this.intersectReg);

if(this.complementReg != null)

this.complementGra.Clear(this.panel1.BackColor);

if(this.xorReg != null)

this.xorGra.Clear(this.panel1.BackColor);

if(this.excludeReg != null)

this.excludeGra.Clear(this.panel1.BackColor);

//填充目标区域-------------------------------------

//-------------------------------------------------

this.unionGra.FillRegion(Brushes.Green,this.unionReg);

this.x = 3;

} .

<chsdate w:st="on" isrocdate="False" islunardate="False" day="30" month="12" year="1899"><strong style="mso-bidi-font-weight: normal"><span lang="EN-US" style="FONT-SIZE: 14pt; LINE-HEIGHT: 200%; FONT-FAMILY: 黑体; mso-font-kerning: 0pt; mso-hansi-font-family: 宋体">3.2.4</span></strong></chsdate> 异并集效果如图6,代码如下

异并集效果

<shape id="_x0000_i1030" style="WIDTH: 414.75pt; HEIGHT: 327.75pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:/DOCUME~1/Game/LOCALS~1/Temp/msohtml1/12/clip_image011.png"></imagedata></shape>

6 异并集效果

private void menuItem8_Click(object sender, System.EventArgs e)

{

this.xorReg = new System.Drawing.Region(this.gp);

this.xorReg.Xor(this.rect);

//r1.Xor(r2) r1更新为r1r2的异并集(即包含在r1r2中,但没有同时包含在//两者中的部分)

this.xorGra = this.panel1.CreateGraphics();

//清除其他区域以突出显示目标区域--------------------------

//---------------------------------------------------------

if(this.intersectReg != null)

this.intersectGra.FillRegion(Brushes.Gray,this.intersectReg);

if(this.unionReg != null)

this.unionGra.Clear(this.panel1.BackColor);

if(this.complementReg != null)

this.complementGra.Clear(this.panel1.BackColor);

if(this.excludeReg != null)

this.excludeGra.Clear(this.panel1.BackColor);

//填充目标区域-------------------------------------

//-------------------------------------------------

this.xorGra.FillRegion(Brushes.Black,this.xorReg);

this.x = 1;

}

<chsdate w:st="on" isrocdate="False" islunardate="False" day="30" month="12" year="1899"><strong style="mso-bidi-font-weight: normal"><span lang="EN-US" style="FONT-SIZE: 14pt; LINE-HEIGHT: 150%; FONT-FAMILY: 黑体; mso-font-kerning: 0pt; mso-hansi-font-family: 宋体">3.2.5</span></strong></chsdate> 补集效果如图7,代码如下

private void menuItem9_Click(object sender, System.EventArgs e)

{

this.complementReg = new System.Drawing.Region(this.gp);

this.complementReg.Complement(this.rect);

//r1.Complement(r2):更新r1使之包含位于r2中的部分,但不包含最初位于r1中的//部分。

this.complementGra = this.panel1.CreateGraphics();

//清除其他区域以突出显示目标区域--------------------------

//---------------------------------------------------------

if(this.intersectReg != null)

this.intersectGra.FillRegion(Brushes.Gray,this.intersectReg);

if(this.unionReg != null)

this.unionGra.Clear(this.panel1.BackColor);

if(this.xorReg != null)

this.xorGra.Clear(this.panel1.BackColor);

if(this.excludeReg != null)

this.excludeGra.Clear(this.panel1.BackColor);

//填充目标区域-------------------------------------

//-------------------------------------------------

this.complementGra.FillRegion(Brushes.Aqua,this.complementReg);

this.x = 2;

}

补集效果

<shape id="_x0000_i1031" style="WIDTH: 414.75pt; HEIGHT: 327.75pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:/DOCUME~1/Game/LOCALS~1/Temp/msohtml1/12/clip_image013.png"></imagedata></shape>

7 补集效果

好了,完工了

分享到:
评论

相关推荐

    使用CART自动决策树在ENVI中对高分辨率遥感影像进行地物分类.rar

    适合在校大学生、遥感初学者学习使用。资料包括高分辨率遥感影像处理过程文件、处理过程截图、各个处理工具的说明、ROI的选择结果、多波段数据的构建、遥感地物分类的结果。可以直接拿去作为遥感课程设计的文档,...

    AvaSoft地物采集操作手册

    在您连接好光谱仪,使用 在您连接好光谱仪,使用 在您连接好光谱仪,使用 在您连接好光谱仪,使用 在您连接好光谱仪,使用 在您连接好光谱仪,使用 AvaSoftAvaSoftAvaSoft AvaSoftAvaSoft 软件进行地物光谱测量时,...

    arcgis计算线状地物面积.docx

    具体来说,可以使用“相交”工具(Intersect tool)将要计算的图斑与现状地物进行叠加分析。这样会生成一个新的图层,包含了原始图斑和线状地物的交集部分。这个步骤确保了线状地物的宽度被纳入到计算范围内。 然后...

    应用ENVI 进行高光谱波谱分析和地物识别

    主要介绍用ENVI如何实现地物识别,以求在此过程中更好地熟悉和理解高光谱遥感图像的处理方法和步骤。本章选用的实验数据是一幅经过校准的AVIRIS图像,处理的结果用于地质学应用,这主要是考虑到,到目前为止地质学...

    2020贵州省面状地物线状地物点状地物gdb图形.rar

    这些数据的详细性意味着用户可以进行深度分析,比如通过点状地物了解贵州省的餐饮业分布状况,或者结合线状地物分析交通网络的可达性。同时,面状地物的数据可以用来研究不同行政区域之间的差异,或者进行环境影响...

    基于CNN深度学习的遥感landsat影像地物分类方法python源码+项目说明.zip

    基于CNN深度学习的遥感landsat影像地物分类方法python源码+项目说明.zip 基于CNN深度学习的遥感landsat影像地物分类方法python源码+项目说明.zip 基于CNN深度学习的遥感landsat影像地物分类方法python源码+项目说明....

    基于语义分割实现无人机拍摄的高分辨率矿区影像图进行地物提取实验python源码+文档说明.zip

    基于语义分割实现无人机拍摄的高分辨率矿区影像图进行地物提取实验python源码+文档说明.zip个人大四的毕业设计、经导师指导并认可通过的高分设计项目,评审分96.5分。主要针对计算机相关专业的正在做毕设的学生和...

    基于PyTorch实现的高分遥感语义分割(地物分类)+数据集+说明.zip

    基于PyTorch实现的高分遥感语义分割(地物分类)+数据集+说明.zip基于PyTorch实现的高分遥感语义分割(地物分类)+数据集+说明.zip基于PyTorch实现的高分遥感语义分割(地物分类)+数据集+说明.zip基于PyTorch实现的...

    基于CNN深度学习的遥感landsat 影像地物分类python源码+h5模型.zip

    【资源说明】 基于CNN深度学习的遥感landsat 影像地物分类python源码+...3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!

    CASS地物代码快速查找

    《CASS地物代码快速查找:提升编图效率的秘密武器》 CASS,全称“Computer Aided Surveying and Design System”,是一款广泛应用于测绘行业的计算机辅助设计软件。它结合了地形测量、工程设计、地籍测绘等多种功能...

    遥感地物分类识别

    总之,通过综合使用多光谱遥感数据、光谱特征提取、神经网络和半监督修正策略,遥感地物分类识别技术在处理复杂地表覆盖类型和提供准确的土地使用信息方面展现出强大的应用潜力。随着遥感技术和数据分析方法的不断...

    对高分辨率光学遥感图像中各类地物光谱信息和空间信息进行分析pytohn源码+模型+数据.zip

    对高分辨率光学遥感图像中各类地物光谱信息和空间信息进行分析pytohn源码+模型+数据.zip对高分辨率光学遥感图像中各类地物光谱信息和空间信息进行分析pytohn源码+模型+数据.zip对高分辨率光学遥感图像中各类地物光谱...

    three.rar_matlab 遥感分类_地物分类_遥感 识别_遥感 地物_遥感图像分类

    遥感图像的识别与分类是通过训练模型来学习地物的特征,然后将新图像的特征与模型进行匹配,从而确定地物类型。"three.m"代码可能涉及了训练样本的选择、模型的构建以及分类结果的评估。 四、遥感地物的多样性及其...

    南方CASS地物编辑--符号填充与缩放、图形删剪与地物打散教程.doc

    在实际应用过程中,用户常常需要对地物进行编辑,包括符号填充与缩放、图形删剪以及地物打散等操作。本文将详细介绍这些功能的具体操作方法及应用场景。 #### 二、系统环境 - **操作系统**:Windows XP - **应用...

    基于AutoCAD的复杂线状地物符号开发

    AutoCAD是一种广泛使用的计算机辅助设计软件,它主要用于工程设计领域进行二维绘图、详细设计、三维模型设计等。AutoCAD提供了强大的二次开发接口,使得用户能够根据自身需求定制软件功能,AutoLISP语言便是其中的一...

    基于卷积神经网络的高光谱遥感地物多分类识别

    在进行遥感图像多分类识别时, 针对使用传统方法遇到的分类模型特征提取困难、分类精度不理想、分类种类少等问题, 研究了卷积神经网络(CNN)模型在高光谱遥感地物多分类识别中的可行性及不同CNN 模型对高光谱遥感地物...

Global site tag (gtag.js) - Google Analytics