All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
c#中GDI+图形图像:GDI+中的区域使用方法

Writer: qq Article type: Programming skills(编程技巧) Time: 2014/7/11 9:16:32 Browse times: 494 Comment times: 0

c#中GDI+图形图像:GDI+中的区域使用方法


Head photo

Go homepage
Upload pictures
Write articles

c#中GDI+图形图像:GDI+中的区域使用方法|实例

GDI+中的区域
区域是输出设备显示区域的一部分,它可以是简单的(例如,单个矩形),也可以是复杂的(例如,多边形和闭合曲线的组合)。
区域常用于剪辑和命中检测。剪辑需要将要绘制的图形限制到显示区域的一个特定部分,通常是需要更新的部分,而命中检测则需要通过检查来确定按下鼠标时,光标是否在屏幕的特定区域中。
可以从矩形或路径中构造区域,也可以通过合并现有的区域来创建复杂区域。Region类提供以下区域合并方法:Intersect、Union、Xor、Exclude和Complement,其中,Intersect方法用来将Region对象更新为其自身与指定Region对象的交集,Union方法用来将Region对象更新为其自身与指定GraphicsPath(或Region)对象的并集,Xor方法用来将Region对象更新为其自身与指定GraphicsPath(或Region)对象的并集减去两者的交集,Exclude方法用来将Region对象更新为其自身与指定GraphicsPath(或Region)对象不相交的部分,Complement方法用来将Region对象更新为指定GraphicsPath(或Region)对象与此Region对象相交的部分。
两个区域的交集是同时属于这两个区域的所有点的集合。并集是属于一个或另一个或两个区域的所有点的集合。区域的补集是不在该区域的所有点的集合。
若要填充区域,需要有Graphics 对象、Brush对象和Region对象。Graphics对象提供FillRegion方法,该方法用来填充Region区域的内部,其语法格式如下:
public void FillRegion (
Brush brush,
Region region)
参数说明如下。
bursh:确定填充特性的Brush对象。
region:Region对象,表示要填充的区域。
示例
区域截取
本示例中,当程序运行时,单击【区域截取】按钮,截取窗体中的一组指定文本并显示出来。示例运行结果如图1所示。

图1 区域截取
Form1窗体中,单击【区域截取】按钮,首先在窗体中绘制一个多边形,然后声明Region类的一个对象,用来指定截取区域的大小;同时调用Graphics对象的SetClip方法,将该对象的剪辑区域设置为当前剪辑区域与指定Region对象的组合结果;最后调用Graphics对象的DrawString方法在窗体中绘制一组文本(该文本的一部分内容包含在截取区域中,另一部分不在其中)。【区域截取】按钮的Click事件代码如下:
private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
Point[] mypoints = {new Point(50, 20),new Point(120, 20), new Point(160, 80),new Point(50, 100)};
GraphicsPath mygraphicsPath = new GraphicsPath();
mygraphicsPath.AddPolygon(mypoints);
Region myregion = new Region(mygraphicsPath);
Pen mypen = new Pen(Color.Blue,2);
graphics.DrawPath(mypen, mygraphicsPath);
graphics.SetClip(myregion, CombineMode.Replace);
graphics.DrawString("GDI+区域", new Font(new FontFamily("楷体_GB2312"),36,
FontStyle.Regular,GraphicsUnit.Pixel), new SolidBrush(Color.Red), new PointF(40, 50));
}
注意:使用GraphicsPath类和CombineMode枚举时,需要添加using System.Drawing. Drawing2D命名空间。
完整程序代码如下:
★ ★★★★Form1.cs窗体代码文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace _6_14
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
Point[] mypoints = {new Point(50, 20),new Point(120, 20), new Point(160, 80),new Point(50, 100)};
GraphicsPath mygraphicsPath = new GraphicsPath();
mygraphicsPath.AddPolygon(mypoints);
Region myregion = new Region(mygraphicsPath);
Pen mypen = new Pen(Color.Blue,2);
graphics.DrawPath(mypen, mygraphicsPath);
graphics.SetClip(myregion, CombineMode.Replace);
graphics.DrawString("GDI+区域", new Font(new FontFamily("楷体_GB2312"),36,
FontStyle.Regular,GraphicsUnit.Pixel), new SolidBrush(Color.Red), new PointF(40, 50));
}
}
}
★ ★★★★Form1.Designer.cs窗体设计文件完整程序代码★★★★★
namespace _6_14
{
partial class Form1





There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.