pen主要画线(直线、矩形、圆等)
brush主要用于填充
Imports System.Drawing
Imports System.Math
Public Class Form1
Dim gr As Graphics
Dim pen1 As New System.Drawing.Pen(Color.Black, 3)
Dim bp As System.Drawing.Bitmap
Const pi As Single = 3.14159
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PictureBox1.Refresh()
pen1.DashStyle = Drawing2D.DashStyle.Dot
pen1.Color = Color.Blue
gr = PictureBox1.CreateGraphics
gr.DrawLine(pen1, 0, 0, 100, 300)
'画椭圆
pen1.DashStyle = Drawing2D.DashStyle.DashDotDot '重设线型
pen1.Color = Color.BlueViolet
gr.DrawEllipse(pen1, 10, 10, 200, 200)
gr.Dispose()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
PictureBox1.Refresh()
pen1.Color = Color.Green
pen1.DashStyle = Drawing2D.DashStyle.Dot
pen1.Width = 1
Dim x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, r, s, t, u As Single
For a As Single = 0 To 2 * pi Step pi / 120
x1 = 240 + 30 * Cos(a)
y1 = 160 + 15 * Sin(a)
r = 60 * (1 + 1 / 6 * Sin(3 * a))
s = 80 * (1 + 1 / 6 * Sin(4 * a))
t = 100 * (1 + 1 / 6 * Sin(5 * a))
u = 140 * (1 + 1 / 8 * Sin(6 * a))
x2 = r * Cos(a + pi / 20) + 240
y2 = r * Sin(a + pi / 20) + 160
x3 = s * Cos(a) + 240
y3 = s * Sin(a) + 160
x4 = t * Cos(a + pi / 20) + 240
y4 = t * Sin(a + pi / 20) + 160
x5 = 1.5 * u * Cos(a) + 240
y5 = u * Sin(a) + 160
pen1.Color = Color.Red
gr = PictureBox1.CreateGraphics
gr.DrawLine(pen1, x1, y1, x2, y2)
pen1.Color = Color.BlueViolet
gr.DrawLine(pen1, x2, y2, x3, y3)
pen1.Color = Color.Red
gr.DrawLine(pen1, x3, y3, x4, y4)
pen1.Color = Color.GreenYellow
gr.DrawLine(pen1, x4, y4, x5, y5)
Next
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'HatchBrush() 阴影笔刷
'LinearGradientBrush 线性渐变笔刷
'SolidBrush 单色笔刷
'TextureBrush 纹理笔刷
'PathGradientBrus 渐变色填充
'阴影笔刷(十字图案)
Dim br1 As System.Drawing.Drawing2D.HatchBrush
br1 = New System.Drawing.Drawing2D.HatchBrush(Drawing2D.HatchStyle.Cross, Color.Red, Color.Black)
gr = PictureBox1.CreateGraphics
gr.FillEllipse(br1, 250, 0, 100, 150)
br1.Dispose()
'线性渐变笔刷
Dim br2 As System.Drawing.Drawing2D.LinearGradientBrush
Dim p1 As New Point(140, 140) 'p1,p2控制渐变频率
Dim p2 As New Point(160, 170)
br2 = New System.Drawing.Drawing2D.LinearGradientBrush(p1, p2, Color.Red, Color.Black)
gr.FillRectangle(br2, 150, 150, 100, 50)
br2.Dispose()
'渐变色填充(不规则或多边形)
Dim p(2) As Point '建立path
p(0).X = 0 : p(0).Y = 0
p(1).X = 20 : p(1).Y = 10
p(2).X = 11 : p(2).Y = 22
Dim br3 As System.Drawing.Drawing2D.PathGradientBrush
br3 = New System.Drawing.Drawing2D.PathGradientBrush(p, Drawing.Drawing2D.WrapMode.Tile)
br3.CenterColor = Color.Blue '中心色
br3.SurroundColors = New Color() {Color.Red, Color.Green, Color.Black} '边沿色对应各点数,除非是圆(一种色)
br3.CenterPoint = New Point(11, 15) '指定中心点
gr.FillEllipse(br3, 0, 0, 150, 100)
br3.Dispose()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
PictureBox1.Refresh()
End Sub
End Class
仔细一看上面PathGradientBrush笔刷,发现每个不规则封闭图形中好像有“间距”,
我们放大看一下,原来,并不是间距,而是以这个不规则图外沿作为一个矩形,“平辅”到整个图形中。
添加一下辅助线,就可以清晰看出它成图的原因了: