个人觉得Graphics就像是画布,要作画,就必须先建立Graphics对象,在这上面实现图像的操作。
能够在表面作图的控件,都支持CreateGraphics()方法
例如:
PictureBox1.CreateGraphics()
Me.CreateGraphics()
等等
能够在表面作图的控件,都支持CreateGraphics()方法
例如:
PictureBox1.CreateGraphics()
Me.CreateGraphics()
等等
如果是绘制图形,那么要用的工具主要是Pen类和Brush类
Pen主要用于线条图形类的,Brush主要用于填充图形类
声明一个Pen对象:
Dim myPen As New Pen(Color.Blue,1)
Pen主要用于线条图形类的,Brush主要用于填充图形类
声明一个Pen对象:
Dim myPen As New Pen(Color.Blue,1)
声明一个Brush对象:
Dim myBrush As New SolidBrush(Color.Red)
Dim myBrush As New SolidBrush(Color.Red)
Brush类是一个抽象类,实际使用中需要用Brush的派生类:
SolidBrush、HatchBrush、PathGrdientBrush、TextureBrush和LinearGradientBrush。
SolidBrush、HatchBrush、PathGrdientBrush、TextureBrush和LinearGradientBrush。
画 线:Graphics.DrawLine
画矩形:Graphics.DrawRectangles
画椭圆:Graphics.DrawEllipse
画弧线:Graphics.DrawArc
画多边形:Graphics.DrawPolygon
。。。。
画矩形:Graphics.DrawRectangles
画椭圆:Graphics.DrawEllipse
画弧线:Graphics.DrawArc
画多边形:Graphics.DrawPolygon
。。。。
填充椭圆:Graphics.FillEllipse
填充扇形:Graphics.FillPie
填充多边形:Graphics.FillPolygon
填充矩形:Graphics.FillRectangle
填充扇形:Graphics.FillPie
填充多边形:Graphics.FillPolygon
填充矩形:Graphics.FillRectangle
一个简单的例子:
代码如下:
Public Class Form1 Dim gCanvas As Graphics Private Sub btnLine_Click(sender As Object,e As EventArgs) Handles btnLine.Click Dim myPen As New Pen(Color.Red,2) '红色,宽度2的画笔 gCanvas.DrawLine(myPen,New Point(10,10),200)) '画线 End Sub Private Sub btnRectangle_Click(sender As Object,e As EventArgs) Handles btnRectangle.Click Dim myPen As New Pen(Color.Blue,2) '蓝色,宽度2的画笔 gCanvas.DrawRectangle(myPen,New Rectangle(20,10,150,200)) '画矩形 End Sub Private Sub btnArc_Click(sender As Object,e As EventArgs) Handles btnArc.Click Dim myPen As New Pen(Color.Green,2) '绿色,宽度2的画笔 gCanvas.DrawArc(myPen,New Rectangle(240,200),180) '画弧线 End Sub Private Sub btnFillRectangle_Click(sender As Object,e As EventArgs) Handles btnFillRectangle.Click Dim myBrush As New SolidBrush(Color.Red) '红色画刷 gCanvas.FillRectangle(myBrush,New Rectangle(21,11,148,198)) '填充矩形 End Sub Private Sub Form1_Load(sender As Object,e As EventArgs) Handles MyBase.Load gCanvas = Me.CreateGraphics() End Sub Private Sub btnFillEllipse_Click(sender As Object,e As EventArgs) Handles btnFillEllipse.Click Dim myBrush As New SolidBrush(Color.Red) '红色画刷 gCanvas.FillEllipse(myBrush,New Rectangle(241,198)) '填充椭圆 End Sub End Class
代码中创建了一个窗体级别变量 gCanvas,在窗口载入的时候,将gCanvas 绑到窗体上。
运行时如下图:
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看 vb.net 教程 目录