首页 > 解决方案 > 如何获得自定义笔颜色?

问题描述

我试过了,但它在这里不起作用是我的代码:

Private Sub PictureBox3_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox3.Paint
    Dim pencolor As String
    pencolor = "89; 179; 105"
    Dim s(1) As Integer
    Dim f(1) As Integer
    Dim pen As Pen = Pens.pencolor
End Sub

谢谢!

标签: vb.net

解决方案


为了在您的笔上使用自定义颜色,您需要先为您的笔创建画笔。

对于此示例,我假设 RGB 颜色值是您的字符串中的序列。

    Dim r As Integer = 89
    Dim g As Integer = 179
    Dim b As Integer = 105
    Dim customColour As Color = Color.FromArgb(r, g, b)
    Using customBrush As New SolidBrush(customColour)
        Using customPen As New Pen(customBrush)
            ' use the custom pen...
        End Using
    End Using

使用自定义颜色创建画笔,然后使用画笔创建自定义笔。当您使用 Pen 时,它的颜色将由您的 R、G、B 值定义

编辑 (感谢 Ahmed Abdelhameed 提醒其他构造函数)

APen也可以在没有 Brush 的情况下创建,只需在其构造函数中指定所需的颜色:

Dim customPen As New Pen(Color.FromArgb(r, g, b))

推荐阅读