Pen钢笔

用线宽为1像素的钢笔绘制矩形:

Pen p = new Pen(Color.Black);

g.DrawRectangle(p,3,3,8,7);

 

网格中左上角的单元格表示像素(0,0),这个矩形的左上角坐标是(3,3),其长度为8个像素,宽度为7个像素。

Pen p = new Pen(Color.Black,3);

p.Alignment = PenAlignment.Center;

g.DrawRectangle(p,3,3,8,7);

 

结果图中矩形的左上角由像素(3,3)制定,但可以看出矩形在外观上的角点在像素(2,2)。如果改变alignment的属性,比如改为Inset,其效果如下:

DashStyle的5个预定义样式:Solid、Dash、DashDot、DashDotDot和Dot。

p.DashStyle = DashStyle.Solid; 

g.DrawLine(p,3,2,100,2);

 

p.DashStyle = DashStyle.Dash;

g.DrawLine(p,3,6,100,6);

 

p.DashStyle  = DashStyle.DashDot;

g.DrawLine(p,3,10,100,10);

 

p.DashStyle = DashStyle.DashDotDot;

g.DrawLine(p,3,14,100,14);

 

p.DashStyle = DashStyle.Dot;

g.DrawLine(p,3,18,100,18);

p.Dispose();其结果如下图:

定制的短划线

创建自己的定制的短划线的样式方法是使用一个整数数组,描述短线以及短线之间距离的像素长度。

pen p = new pen(color.black,1);

float[] f = {15,5,10,5};

p.dashpattern = f;

这个例子中,短划线模式是用数组指定的,我们选择了一个包含4元素的数组,所以短划线模式就是这4个元素的循环:15像素长的短线,5像素长的空隙,10像素长的短线,5像素长的空隙,且这个样式从左下角开始。

如果钢笔的线宽设置为大于1像素的值,每条短线和空隙的实际长度就要用数组中指定的值乘以钢笔的线宽。

使用linecap可以指定GDI+如何装饰线段的开始和结尾,事实上这表示把值赋予pen对象的startcap和endcap属性。如:

Graphics g = e.Graphics;

g.smoothingmode = smoothingmode.antialias;

g.fillrectangle(brushes.white,this.clientrectangle);

pen p = new pen(color.black,10);

p.startcap = linecap.round;

p.endcap = linecap.arrowanchor;

g.drawline(p,30,30,80,30);

p.dispose();

可用的线段端部图形和它们的外观:

 
只要执行了一个涉及到显示连接线段的操作,就可以设置连接线断的样式,为此,可使用drawing2d命名空间中的LineJoin枚举的值,其中有4个值:miter(默认值)、beveled、miterclipped和round。

GDI+提供了141个预定义的彩色钢笔,每个钢笔的线宽都是1像素,为了访问这些钢笔,需要使用pens类,如:

Graphics g = e.Graphics;

g.FillRectangle(Brushes.White,this.ClientRectangle);

g.DrawRectangl(Pens.MistyRose,20,20,40,40);

posted @ 2012-03-14 20:03  ttssrs  阅读(431)  评论(0编辑  收藏  举报