在Visual Basic中,我有用于笔将黑色线条涂成颜色的代码,我需要将其随机化。这支钢笔需要是随机的颜色:g.DrawLine(Pens.Black,x * y,50,x * y,250)
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
Dim x, y As Integer
Dim bmp As Bitmap = New Bitmap(1350, 750)
Dim g As Graphics = Graphics.FromImage(bmp)
g.DrawLine(Pens.Black, 0, 100, 1500, 100)
g.DrawLine(Pens.Black, 0, 200, 1500, 200)
y = 20
For x = 1 To 150
g.DrawLine(Pens.Black, x * y, 50, x * y, 250)
Next x
PictureBox1.Image = bmp
End Sub
Pens不是枚举,因此枚举Pens类的唯一方法可能是通过反射。考虑创建一些基于颜色的RGB随机化的东西:
Dim rng as New Random()
For x = 1 To 150
Using pen = new Pen(Color.FromArgb(rng.Next(256), rng.Next(256), rng.Next(256)))
g.DrawLine(pen, x * y, 50, x * y, 250)
End Using
Next x
如果您确实只希望使用系统定义的颜色名称(如AliceBlue或AntiqueWhite)的笔,则可能需要使用 Color.FromKnownColor(CType(rng.Next(28, 168),KnownColor))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句