星空网 > 软件开发 > ASP.net

.net中使用GDI+组件绘制图形图像(二)从绘制线条轮廓开始

本文继续上一篇文章,继续探讨下《.net中使用GDI+组件绘制图形图像》,主要内容是通过GDI+组件来绘制各式的线条!!

我们知道,无论任何的窗体上的控件都是有其各式形状的,然后内部填充颜色或者图像。所以个人认为:在使用GDI+组件绘制界面前,首先得学会绘制不同的线条,绘制不同形状。下面将直接通过代码的形式给大家展示下如何绘制各式线条样式。这里着重介绍的是最最常用的一些线条,形状的绘制。

下面代码要执行,请如上篇文章一样,建立个windows窗体项目,在窗体Form的Paint事件中,将下面的示例代码复制进去执行,详细请看上篇文章。

 

绘制线条前,先来定义下画板和画笔。直接贴出代码如下:

Graphics g = e.Graphics;g.Clear(Color.White);//定义画笔特征(颜色、线形、粗细等)Pen p = new Pen(Color.Blue);p.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;p.Width = 1;

 

1、绘制一条直线

 可以通过DrawLine方法

g.DrawLine(p, 10, 10, 200, 200);

  

 2、三点绘制一个三角形

 

g.DrawPolygon(p, new Point[] {   new Point(50,30),  new Point(50,160),  new Point(200,80) });

  

 3、四点绘制一个四边形

g.DrawPolygon(p, new Point[] {   new Point(50,50),   new Point(50,200),   new Point(100,200),   new Point(100,50)});

4、绘制一个多边行

g.DrawPolygon(p, new Point[] {   new Point(50,50),  new Point(50,200),  new Point(100,200),  new Point(150,125),  new Point(100,50)});

5、绘制一段弧线

g.DrawArc(p, new Rectangle(10, 10, 100, 100), -10, 100);

6、绘制贝塞尔曲线  

g.DrawBezier(p, new Point(10, 10), new Point(30, 50), new Point(100, 200), new Point(200, 50));

7、绘制椭圆

g.DrawEllipse(p, new Rectangle(10, 10, 200, 100));

8、绘制圆角矩形

//将下面代码复制到Paint事件中using (GraphicsPath path = CreateRoundedRectanglePath(new Rectangle(10, 10, 100, 30), 5)){  g.DrawPath(p, path);}

将下面方法放在Form窗体类下

//此方法供上面调用,从网络搜索而来,仅做参考!internal static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius){  GraphicsPath roundedRect = new GraphicsPath();  roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);  roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);  roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);  roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);  roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);  roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);  roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);  roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);  roundedRect.CloseFigure();  return roundedRect;}

  

OK,最常见的一些线条可以复制到VS中运行下看看!还是比较容易的哦。熟练使用才是作重要的!

 

本文示例代码文件,点击“GDI+绘制线条”

  

如果觉得还可以,就在下面点个赞吧~   欢迎来留言探讨,转载请注明本文出处!

  




原标题:.net中使用GDI+组件绘制图形图像(二)从绘制线条轮廓开始

关键词:.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流