使用钢笔,画笔用来填充图形内部,钢笔则用来绘制带有一定宽度,样式和色彩的线条和曲线.

可以使用标准的pens类

<%@ Page ContentType="image/gif" Language="C#" %>
<!--ContentType设置页面类型-->
<%@ Import namespace="System.Drawing" %>
<%@ Import namespace="System.Drawing.Imaging" %>
<%@ Import namespace="System.Drawing.Drawing2D" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(600, 120);
 //创建一个宽400,高200的实例
Color mycolor;
mycolor = Color.FromArgb(0, 0, 255);
//FromArgb方法设置RGB值
Pen mypen = new Pen(Color.Blue, 5);
//创建自定义钢笔,color,float,参数为颜色和宽度
Graphics gph;
//从指定的Image对象创建新Graphics对象
gph = Graphics.FromImage(bmp);
//清除整个绘图面并以指定背景色填充
gph.Clear(Color.Red);
//绘制由坐标对、宽度和高度指定的矩形
gph.DrawRectangle(Pens.Green, 10, 10, 100, 100);
gph.DrawRectangle(mypen, 120, 10, 100, 100);
bmp.Save(Response.OutputStream, ImageFormat.Gif);//ImageFormat 对象,它指定保存的图像的格式
//向客户端输出数据流,并以此数据流形成Gif图片 
}
</script>

 

自定义钢笔样式

DashStyle枚举 指定用 Pen 对象绘制的虚线的样式

成员名称 说明 
Custom 指定用户定义的自定义划线段样式。 
Dash 指定由划线段组成的直线。 
DashDot 指定由重复的划线点图案构成的直线。 
DashDotDot 指定由重复的划线点点图案构成的直线。 
Dot 指定由点构成的直线。 
Solid 指定实线。 

示例

<%@ Page ContentType="image/gif" Language="C#" %>
<!--ContentType设置页面类型-->
<%@ Import namespace="System.Drawing" %>
<%@ Import namespace="System.Drawing.Imaging" %>
<%@ Import namespace="System.Drawing.Drawing2D" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(600, 120);
 //创建一个宽400,高200的实例
Graphics gph;
//从指定的Image对象创建新Graphics对象
gph = Graphics.FromImage(bmp);
//清除整个绘图面并以指定背景色填充
gph.Clear(Color.Green);
Pen mypen = new Pen(Color.Red, 2);
//创建自定义钢笔,color,float,参数为颜色和宽度
mypen.DashStyle = DashStyle.Custom;
//自定义钢笔样式
gph.DrawLine(mypen, 10, 10, 300, 10);
//绘制直线
mypen.DashStyle = DashStyle.Dash;
gph.DrawLine(mypen, 10, 30, 300, 30);
//(Pen, int, int, int, int)pen Pen 起点x坐标,起点y坐标,终点x坐标,终点y坐标
mypen.DashStyle = DashStyle.DashDot;
gph.DrawLine(mypen, 10, 50, 300, 50);
mypen.DashStyle = DashStyle.DashDotDot;
gph.DrawLine(mypen, 10, 70, 300, 70);
mypen.DashStyle = DashStyle.Dot;
gph.DrawLine(mypen, 10, 90, 300, 90);
mypen.DashStyle = DashStyle.Solid;
gph.DrawLine(mypen, 10, 110, 300, 110);
bmp.Save(Response.OutputStream, ImageFormat.Gif);//ImageFormat 对象,它指定保存的图像的格式
//向客户端输出数据流,并以此数据流形成Gif图片 
}
</script>

 

设置钢笔线帽样式,可以设置线条的起始和结束的线帽样式

StartCap 获取或设置用在通过此 Pen 对象绘制的直线起点的帽样式 
EndCap 获取或设置用在通过此 Pen 对象绘制的直线终点的帽样式 
DashCap 获取或设置用在短划线终点的帽样式,这些短划线构成通过此 Pen 对象绘制的虚线 

LineCap 枚举
成员名称 说明 
AnchorMask 指定用于检查线帽是否为锚头帽的掩码。 
ArrowAnchor 指定箭头状锚头帽。 
Custom 指定自定义线帽。 
DiamondAnchor 指定菱形锚头帽。 
Flat 指定平线帽。 
NoAnchor 指定没有锚。 
Round 指定圆线帽。 
RoundAnchor 指定圆锚头帽。 
Square 指定方线帽。 
SquareAnchor 指定方锚头帽。 
Triangle 指定三角线帽。 

示例

<%@ Page ContentType="image/gif" Language="C#" %>
<%@ Import namespace="System.Drawing" %>
<%@ Import namespace="System.Drawing.Imaging" %>
<%@ Import namespace="System.Drawing.Drawing2D" %>
<script language="C#" runat=server>
void Page_Load(Object sender , EventArgs e) 
{
  Bitmap objBitmap;
  Graphics objGraphics;
  Pen objPen;
  objBitmap = new Bitmap( 400, 200 );
  objGraphics = Graphics.FromImage( objBitmap );
  objPen = new Pen( Color.White );
  objPen.Width = 6;
  objPen.EndCap = LineCap.ArrowAnchor;
  objGraphics.DrawLine( objPen, 10, 20, 350, 20 );
  objPen.EndCap = LineCap.DiamondAnchor;
  objGraphics.DrawLine( objPen, 10, 40, 350, 40 );
  objPen.EndCap = LineCap.Flat;
  objGraphics.DrawLine( objPen, 10, 60, 350, 60 );
  objPen.EndCap = LineCap.Round;
  objGraphics.DrawLine( objPen, 10, 80, 350, 80 );
  objPen.EndCap = LineCap.RoundAnchor;
  objGraphics.DrawLine( objPen, 10, 100, 350, 100 );
  objPen.EndCap = LineCap.Square;
  objGraphics.DrawLine( objPen, 10, 120, 350, 120 );
  objPen.EndCap = LineCap.SquareAnchor;
  objGraphics.DrawLine( objPen, 10, 140, 350, 140 );
  objPen.EndCap = LineCap.Triangle;
  objGraphics.DrawLine( objPen, 10, 160, 350, 160 );
  objBitmap.Save( Response.OutputStream, ImageFormat.Gif );
}
</Script>

 

设置钢笔的线条接头样式,通过修改LineJoin属性值,控制钢笔绘制的线条接头的外观.
LineJoin 枚举

成员名称 说明 
Bevel 指定成斜角的联接。这将产生一个斜角。 
Miter 指定斜联接。这将产生一个锐角或切除角,具体取决于斜联接的长度是否超过斜联接限制。 
MiterClipped 指定斜联接。这将产生一个锐角或斜角,具体取决于斜联接的长度是否超过斜联接限制。 
Round 指定圆形联接。这将在两条线之间产生平滑的圆弧。 

示例

<%@ Page ContentType="image/gif" Language="C#" %>
<%@ Import namespace="System.Drawing" %>
<%@ Import namespace="System.Drawing.Imaging" %>
<%@ Import namespace="System.Drawing.Drawing2D" %>
<script language="C#" runat=server>
void Page_Load(Object sender , EventArgs e) 
{
  Bitmap objBitmap;
  Graphics objGraphics; 
  Pen objPen;
  objBitmap = new Bitmap( 400, 300 );
  objGraphics = Graphics.FromImage( objBitmap );
  objPen = new Pen( Color.White );
  objPen.Width = 10;
  objPen.LineJoin = LineJoin.Bevel;
  objGraphics.DrawRectangle( objPen, 10, 20, 350, 40 );
  objPen.LineJoin = LineJoin.Miter;
  objGraphics.DrawRectangle( objPen, 10, 80, 350, 40 );
 objPen.LineJoin = LineJoin.MiterClipped;
 objGraphics.DrawRectangle(objPen, 10, 140, 350, 40);
  objPen.LineJoin = LineJoin.Round;
  objGraphics.DrawRectangle( objPen, 10, 200, 350, 40 );
  objBitmap.Save( Response.OutputStream, ImageFormat.Gif );
}
</Script>

GDI+基础(2)_对象创建