前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >VB.NET 实现屏幕取色器20210924

VB.NET 实现屏幕取色器20210924

作者头像
一线编程
发布2021-09-30 11:00:06
1.9K0
发布2021-09-30 11:00:06
举报
文章被收录于专栏:办公魔盒办公魔盒办公魔盒
VB.NET 实现屏幕取色器

一,先看看效果图

二,在开始前,我们先来简单(毕竟复杂的咱也不会)了解一下,颜色的各种表达方式和他们之间的转换吧!(以下内容来自网络)

  • 2.1.什么是RGB模式?

 RGB色彩模式是工业界的一种颜色标准,是通过对红(R)、绿(G)、蓝(B)三个颜色通道的变化以及它们相互之间的叠加来得到各式各样的颜色的,RGB即是代表红、绿、蓝三个通道的颜色,这个标准几乎包括了人类视力所能感知的所有颜色,是运用最广的颜色系统之一。

  • 2.2.什么是CMYK模式?

 CMYK也称作印刷色彩模式。它和RGB相比最大不同是,RGB模式是发光的色彩模式,你在一间黑暗的房间内仍然可以看见萤幕上的内容。  CMYK是一种依靠反光的色彩模式,我们能阅读报纸的内容是为什么呢?是因阳光或灯光照射到报纸上,再把内容反射到我们的眼中。CMYK是需要有外界光源的情况下才可以看到的。所以在黑暗房间内是无法阅读的。只要是在印刷品上看到的图像,就是CMYK模式表现的。比如期刊、杂志、报纸、宣传画册等,都是运用了CMYK模式。  CMY是3种印刷油墨名称的首字母:青色Cyan、品红色Magenta、黄色Yellow。而K取的是black最后一个字母,为了避免与蓝色混淆而用K

  • 2.3.什么是HSL/HSB模式?

 HSL是一种将RGB色彩模型中的点在圆柱坐标系中的表示法。这两种表示法试图做到比基于笛卡尔坐标系的几何结构RGB更加直观。是目前运用最广的颜色系统之一。  HSV即色相、饱和度、明度(英语:Hue, Saturation, Value),又称HSB,其中B即英语:Brightness

  • 2.4.什么是HSV模式?

 HSV(Hue, Saturation, Value)是根据颜色的直观特性由A. R. Smith在1978年创建的一种颜色空间, 也称六角锥体模型(Hexcone Model)。  这个模型中颜色的参数分别是:色调(H),饱和度(S),明度(V)。

三,再简单了解一下,各颜色模式之间的转换公式和VB代码吧!

  • 3.1.RGB转CMYK模式公式
R,G,B值除以255,将范围从0..255更改为0..1:
R '= R / 255
G '= G / 255
B '= B / 255

黑色键(K)颜色由红色(R'),绿色(G')和蓝色(B')计算得出:

K = 1-max(R ',G ',B ')

青色(C)由红色(R')和黑色(K)计算得出:

C =(1- R' - K)/(1- K)

洋红色(M)由绿色(G')和黑色(K)计算得出:

M =(1- G' - K)/(1- K)

黄色(Y)由蓝色(B')和黑色(K)计算得出:

Y =(1- B' - K)/(1- K)
  • 3.2.CMYK转RGB模式公式
R,G,B值在0..255的范围内给出。

红色(R)由青色(C)和黑色(K)计算得出:

R = 255×(1- C)×(1- K)

绿色(G)由洋红色(M)和黑色(K)计算得出:

G = 255×(1- M)×(1- K)

蓝色(B)由黄色(Y)和黑色(K)计算得出:

B = 255×(1- Y)×(1- K)
  • 3.3.HSL/HSB转RGB模式公式
当0≤ ħ <360,0≤小号≤1和0≤大号≤1:

C =(1-| 2L-1 |)× S

X = C ×(1-|(H / 60°)mod 2-1 |)

m = L - C / 2
(R,G,B)=((R'+ m)×255,(G'+ m)×255,(B '+ m)×255)
  • 3.4.RGB转HSL/HSB模式公式
[R ,G ^,乙值除以255来的范围内改变从0..255到0..1:

R '= R / 255

G '= G / 255

B '= B / 255

Cmax = max(R ',G ',B ')

Cmin = min(R ',G ',B ')

Δ= Cmax - Cmin
色相计算:
饱和度计算:
亮度计算:

L =(Cmax + Cmin)/ 2
  • 3.5.HSV转RGB模式公式
当0≤ ħ <360,0≤小号≤1和0≤ V ≤1:

C = V × S

X = C ×(1-|(H / 60°)mod 2-1 |)

m = V - C
(R,G,B)=((R '+ m)×255,(G '+ m)×255,(B '+ m)×255)
  • 3.6.RGB转HSV模式公式
[R ,G ^,乙值除以255来的范围内改变从0..255到0..1:

R '= R / 255

G '= G / 255

B '= B / 255

Cmax = max(R ',G ',B ')

Cmin = min(R ',G ',B ')

Δ= Cmax - Cmin
色相计算:
饱和度计算:
值计算:
V =Cmax

四,下面直接贴出,在VB.NET中,各个颜色模式转换的代码(可能会有错误,如有发现请后台告知,万分感谢)

  • 4.1.颜色转换帮助类(写得不好,仅供参考;引用转载请注明出处!)
''*******************************************
''******** 模块名:颜色转换帮助类 ****************
''******** QQ:463550067 *********************
''******** 微信:vbee_club ******************
''*******************************************

Public Class Class_ColorHelper

  ''' <summary>
  ''' CMYK色彩模式类型
  ''' </summary>
  Public Class CMYK_TYPE
    ''' <summary>
    ''' 初始化色彩模型类型
    ''' </summary>
    ''' <param name="C">青色(%)</param>
    ''' <param name="M">洋红色(%)</param>
    ''' <param name="Y">黄色(%)</param>
    ''' <param name="K">黑键颜色(%)</param>
    Public Sub New(C As Integer, M As Integer, Y As Integer, K As Integer)
      Me.C = C
      Me.M = M
      Me.Y = Y
      Me.K = K
    End Sub
    ''' <summary>
    ''' 青色
    ''' </summary>
    ''' <returns></returns>
    Public Property C As Integer
    ''' <summary>
    ''' 洋红色
    ''' </summary>
    ''' <returns></returns>
    Public Property M As Integer
    ''' <summary>
    ''' 黄色
    ''' </summary>
    ''' <returns></returns>
    Public Property Y As Integer
    ''' <summary>
    ''' 黑键颜色
    ''' </summary>
    ''' <returns></returns>
    Public Property K As Integer
    ''' <summary>
    ''' 重写string
    ''' </summary>
    ''' <returns></returns>
    Public Overrides Function Tostring() As String
      Return $"{C},{M},{Y},{K}"
    End Function
  End Class

  ''' <summary>
  ''' HSL色彩模式
  ''' </summary>
  Public Class HSL_TYPE
    ''' <summary>
    ''' HSL色彩模式初始化
    ''' </summary>
    ''' <param name="H">色相</param>
    ''' <param name="S">饱和度</param>
    ''' <param name="L">亮度</param>
    Public Sub New(H As Double, S As Double, L As Double)
      Me.H = H
      Me.S = S
      Me.L = L
    End Sub
    ''' <summary>
    ''' 色相
    ''' </summary>
    ''' <returns></returns>
    Public Property H As Double
    ''' <summary>
    ''' 饱和度
    ''' </summary>
    ''' <returns></returns>
    Public Property S As Double
    ''' <summary>
    ''' 亮度
    ''' </summary>
    ''' <returns></returns>
    Public Property L As Double
    ''' <summary>
    ''' 重写string
    ''' </summary>
    ''' <returns></returns>
    Public Overrides Function Tostring() As String
      Return $"{H},{S},{L}"
    End Function
  End Class


  ''' <summary>
  ''' HSV色彩模式
  ''' </summary>
  Public Class HSV_TYPE
    ''' <summary>
    ''' HSL色彩模式初始化
    ''' </summary>
    ''' <param name="H">色相</param>
    ''' <param name="S">饱和度</param>
    ''' <param name="V">颜色值</param>
    Public Sub New(H As Double, S As Double, V As Double)
      Me.H = H
      Me.S = S
      Me.V = V
    End Sub
    ''' <summary>
    ''' 色相
    ''' </summary>
    ''' <returns></returns>
    Public Property H As Double
    ''' <summary>
    ''' 饱和度
    ''' </summary>
    ''' <returns></returns>
    Public Property S As Double
    ''' <summary>
    ''' 颜色值
    ''' </summary>
    ''' <returns></returns>
    Public Property V As Double
    ''' <summary>
    ''' 重写string
    ''' </summary>
    ''' <returns></returns>
    Public Overrides Function Tostring() As String
      Return $"{H},{S},{V}"
    End Function
  End Class




  ''' <summary>
  ''' HSB色彩模式
  ''' </summary>
  Public Class HSB_TYPE
    ''' <summary>
    ''' HSL色彩模式初始化
    ''' </summary>
    ''' <param name="H">色相</param>
    ''' <param name="S">饱和度</param>
    ''' <param name="B">亮度值</param>
    Public Sub New(H As Double, S As Double, B As Double)
      Me.H = H
      Me.S = S
      Me.B = B
    End Sub
    ''' <summary>
    ''' 色相
    ''' </summary>
    ''' <returns></returns>
    Public Property H As Double
    ''' <summary>
    ''' 饱和度
    ''' </summary>
    ''' <returns></returns>
    Public Property S As Double
    ''' <summary>
    ''' 亮度值
    ''' </summary>
    ''' <returns></returns>
    Public Property B As Double
    ''' <summary>
    ''' 重写string
    ''' </summary>
    ''' <returns></returns>
    Public Overrides Function Tostring() As String
      Return $"{H},{S},{B}"
    End Function
  End Class


  ''' <summary>
  ''' RGB色彩模式
  ''' </summary>
  Public Class RGB_TYPE
    ''' <summary>
    ''' RGB色彩模式初始化
    ''' </summary>
    ''' <param name="R">红色</param>
    ''' <param name="G">绿色</param>
    ''' <param name="B">蓝色</param>
    Public Sub New(R As Integer, G As Integer, B As Integer)
      Me.R = R
      Me.G = G
      Me.B = B
    End Sub
    ''' <summary>
    ''' 红色
    ''' </summary>
    ''' <returns></returns>
    Public Property R As Integer
    ''' <summary>
    ''' 绿色
    ''' </summary>
    ''' <returns></returns>
    Public Property G As Integer
    ''' <summary>
    ''' 蓝色
    ''' </summary>
    ''' <returns></returns>
    Public Property B As Integer

    ''' <summary>
    ''' 重写string
    ''' </summary>
    ''' <returns></returns>
    Public Overrides Function Tostring() As String
      Return $"{R},{G},{B}"
    End Function
  End Class

  ''' <summary>
  ''' CMYK色彩模式转RGB色彩模式
  ''' </summary>
  ''' <param name="CMYK">CMYK色彩值</param>
  ''' <returns></returns>
  Public Shared Function CMYK2RGB(CMYK As CMYK_TYPE) As RGB_TYPE
    Try
      Dim R As Integer = Math.Ceiling(255 * (100 - CMYK.C) * (100 - CMYK.K) / 10000)
      Dim G As Integer = Math.Ceiling(255 * (100 - CMYK.M) * (100 - CMYK.K) / 10000)
      Dim B As Integer = Math.Ceiling(255 * (100 - CMYK.Y) * (100 - CMYK.K) / 10000)
      Dim RGBT As New RGB_TYPE(R, G, B)
      Return RGBT
    Catch ex As Exception
      Dim RGBT As New RGB_TYPE(0, 0, 0)
      Return RGBT
    End Try
  End Function

  ''' <summary>
  ''' RGB色彩模式转CMYK色彩模式
  ''' </summary>
  ''' <param name="RGBT">RGB色彩值</param>
  ''' <returns></returns>
  Public Shared Function RGB2CMYK(RGBT As RGB_TYPE) As CMYK_TYPE
    Try
      Dim NR As Integer = RGBT.R / 255 * 100
      Dim NG As Integer = RGBT.G / 255 * 100
      Dim NB As Integer = RGBT.B / 255 * 100
      Dim LST As New List(Of Integer) From {
        NR,
        NG,
        NB
        }
      ''---计算色彩值
      Dim K As Integer = Math.Ceiling(100 - LST.Max)
      Dim C As Integer = Math.Ceiling((100 - NR - K) / (100 - K))
      Dim M As Integer = Math.Ceiling((100 - NG - K) / (100 - K))
      Dim Y As Integer = Math.Ceiling((100 - NB - K) / (100 - K))
      Dim CMYK As New CMYK_TYPE(C, M, Y, K)
      Return CMYK
    Catch ex As Exception
      Dim CMYK As New CMYK_TYPE(0, 0, 0, 0)
      Return CMYK
    End Try
  End Function

  ''' <summary>
  ''' RGB色彩值转16进制值
  ''' </summary>
  ''' <param name="RGBT">RGB色彩值</param>
  ''' <returns></returns>
  Public Shared Function RGB2HEX(RGBT As RGB_TYPE) As String
    Try
      Dim hexR As String = String.Format("{0:X}", RGBT.R)
      Dim hexG As String = String.Format("{0:X}", RGBT.G)
      Dim hexB As String = String.Format("{0:X}", RGBT.B)
      Dim HexStr As String = $"#{hexR}{hexG}{hexB}"
      Return HexStr
    Catch ex As Exception
      Return String.Empty
    End Try
  End Function

  ''' <summary>
  ''' 16进制值转RGB色彩值
  ''' </summary>
  ''' <param name="hexStr">16进制值</param>
  ''' <returns></returns>
  Public Shared Function HEX2RGB(hexStr As String) As RGB_TYPE
    Try
      ''取出数据
      Dim newhex As String = hexStr.Replace("#", "")
      Dim hexR As String = newhex.Substring(0, 2)
      Dim hexG As String = newhex.Substring(2, 2)
      Dim hexB As String = newhex.Substring(4, 2)
      ''16进制字符串转整数
      Dim R As Integer = Convert.ToInt32(hexR, 16)
      Dim G As Integer = Convert.ToInt32(hexG, 16)
      Dim B As Integer = Convert.ToInt32(hexB, 16)
      ''输出数据
      Dim RGBT As New RGB_TYPE(R, G, B)
      Return RGBT
    Catch ex As Exception
      Dim RGBT As New RGB_TYPE(0, 0, 0)
      Return RGBT
    End Try
  End Function

  ''' <summary>
  ''' RGB模式值转HSL模式值
  ''' </summary>
  ''' <param name="RGBT"></param>
  ''' <returns></returns>
  Public Shared Function RGB2HSL(RGBT As RGB_TYPE) As HSL_TYPE
    Try
      Dim CORLORT As Color = Color.FromArgb(RGBT.R, RGBT.G, RGBT.B)
      Dim max As Integer = Math.Max(CORLORT.R, Math.Max(CORLORT.G, CORLORT.B))
      Dim min As Integer = Math.Min(CORLORT.R, Math.Min(CORLORT.G, CORLORT.B))
      ''计算颜色值
      Dim H As Double = Math.Ceiling(CORLORT.GetHue())
      Dim S As Double = Math.Round(If(max = 0, 0, 1.0 - (1.0 * min / max)) * 100, 1)
      Dim L As Double = Task.Run(Function()
                                   Dim power = Math.Pow(10, 3)
                                   Return Math.Floor((max / 255 + min / 255) / 2 * power) / power
                                 End Function).Result

      ''输出HSL
      Dim EH As Double = Math.Round(H, 1)
      Dim ES As Double = Math.Round(S, 1)
      Dim EL As Double = Math.Round(L * 100, 1)
      ''返回hsl
      Dim HSL As New HSL_TYPE(EH, ES, EL)
      Return HSL
    Catch ex As Exception
      Dim HSL As New HSL_TYPE(0, 0, 0)
      Return HSL
    End Try
  End Function


  ''' <summary>
  ''' HSL模式值转RGB模式值
  ''' </summary>
  ''' <param name="HSLS"></param>
  ''' <returns></returns>
  Public Shared Function HSL2RGB(HSLS As HSL_TYPE) As RGB_TYPE
    Try

      Dim C = (1 - Math.Abs(2 * HSLS.L / 100 - 1)) * HSLS.S / 100
      Dim X = C * (1 - Math.Abs(((HSLS.H / 60) Mod 2) - 1))
      Dim m = HSLS.L / 100 - C / 2
      Dim vRGB(2) As Double

      If (HSLS.H >= 0 And HSLS.H < 60) Then
        vRGB(0) = C : vRGB(1) = X : vRGB(2) = 0
      ElseIf (HSLS.H >= 60 And HSLS.H < 120) Then
        vRGB(0) = X : vRGB(1) = C : vRGB(2) = 0
      ElseIf (HSLS.H >= 120 And HSLS.H < 180) Then
        vRGB(0) = 0 : vRGB(1) = C : vRGB(2) = X
      ElseIf (HSLS.H >= 180 And HSLS.H < 240) Then
        vRGB(0) = 0 : vRGB(1) = X : vRGB(2) = C
      ElseIf (HSLS.H >= 240 And HSLS.H < 300) Then
        vRGB(0) = X : vRGB(1) = 0 : vRGB(2) = C
      ElseIf (HSLS.H >= 300 And HSLS.H < 360) Then
        vRGB(0) = C : vRGB(1) = 0 : vRGB(2) = X
      End If

      Dim R = Math.Floor(255 * (vRGB(0) + m))
      Dim G = Math.Ceiling(255 * (vRGB(1) + m))
      Dim B = Math.Ceiling(255 * (vRGB(2) + m))

      Dim rgbs As New RGB_TYPE(R, G, B)
      Return rgbs
    Catch ex As Exception
      Dim rgbs As New RGB_TYPE(0, 0, 0)
      Return rgbs
    End Try
  End Function

  ''' <summary>
  ''' RGB模式值转HSV模式值
  ''' </summary>
  ''' <param name="RGBT">RGB模式值</param>
  ''' <returns></returns>
  Public Shared Function RGB2HSV(RGBT As RGB_TYPE) As HSV_TYPE
    Try
      Dim CORLORT As Color = Color.FromArgb(RGBT.R, RGBT.G, RGBT.B)
      Dim max As Integer = Math.Max(CORLORT.R, Math.Max(CORLORT.G, CORLORT.B))
      Dim min As Integer = Math.Min(CORLORT.R, Math.Min(CORLORT.G, CORLORT.B))
      ''计算颜色值
      Dim H As Double = Math.Ceiling(CORLORT.GetHue())
      Dim S As Double = Math.Round(If(max = 0, 0, 1.0 - (1.0 * min / max)) * 100, 1)
      Dim V As Double = Math.Round(max / 255.0 * 100, 1)
      ''生成颜色值
      Dim hsvobj As New HSV_TYPE(H, S, V)
      Return hsvobj

    Catch ex As Exception
      Dim hsvobj As New HSV_TYPE(0, 0, 0)
      Return hsvobj
    End Try

  End Function

  ''' <summary>
  ''' HSV模式值转RGB模式值
  ''' </summary>
  ''' <param name="HSV">HSV模式值</param>
  ''' <returns></returns>
  Public Shared Function HSV2RGB(HSV As HSV_TYPE) As RGB_TYPE
    Try

      Dim hi As Integer = Convert.ToInt32(Math.Floor(HSV.H / 60)) Mod 6
      Dim f As Double = HSV.H / 60 - Math.Floor(HSV.H / 60)
      ''计算rgb
      Dim vl As Double = HSV.V * 255 / 100
      Dim v As Integer = Convert.ToInt32(vl)
      Dim p As Integer = Convert.ToInt32(vl * (100 - HSV.S)) / 100
      Dim q As Integer = Convert.ToInt32(vl * (100 - f * HSV.S)) / 100
      Dim t As Integer = Convert.ToInt32(vl * (100 - (100 - f) * HSV.S)) / 100
      ''取值
      Dim Colort As RGB_TYPE
      If hi = 0 Then
        Colort = New RGB_TYPE(v, t, p)
      ElseIf hi = 1 Then
        Colort = New RGB_TYPE(q, v, p)
      ElseIf hi = 2 Then
        Colort = New RGB_TYPE(p, v, t)
      ElseIf hi = 3 Then
        Colort = New RGB_TYPE(p, q, v)
      ElseIf hi = 4 Then
        Colort = New RGB_TYPE(t, p, v)
      Else
        Colort = New RGB_TYPE(v, p, q)
      End If
      Dim RGBT As New RGB_TYPE(Colort.R, Colort.G, Colort.B)
      Return RGBT
    Catch ex As Exception
      Dim RGBT As New RGB_TYPE(0, 0, 0)
      Return RGBT
    End Try
  End Function

  ''' <summary>
  ''' RGB模式值转HSB模式值
  ''' </summary>
  ''' <param name="RGBT">RGB模式值</param>
  ''' <returns></returns>
  Public Shared Function RGB2HSB(RGBT As RGB_TYPE) As HSB_TYPE
    Try
      Dim CORLORT As Color = Color.FromArgb(RGBT.R, RGBT.G, RGBT.B)
      Dim max As Integer = Math.Max(CORLORT.R, Math.Max(CORLORT.G, CORLORT.B))
      Dim min As Integer = Math.Min(CORLORT.R, Math.Min(CORLORT.G, CORLORT.B))
      ''计算颜色值
      Dim H As Double = Math.Ceiling(CORLORT.GetHue())
      Dim S As Double = Math.Round(If(max = 0, 0, 1.0 - (1.0 * min / max)) * 100, 1)
      Dim B As Double = Math.Round(max / 255.0 * 100, 1)
      ''生成颜色值
      Dim hsbobj As New HSB_TYPE(H, S, B)
      Return hsbobj
    Catch ex As Exception
      Dim hsbobj As New HSB_TYPE(0, 0, 0)
      Return hsbobj
    End Try
  End Function

  ''' <summary>
  ''' HSB模式值转RGB模式值
  ''' </summary>
  ''' <param name="HSB"></param>
  ''' <returns></returns>
  Public Shared Function HSB2RGB(HSB As HSB_TYPE) As RGB_TYPE
    Try
      Dim hi As Integer = Convert.ToInt32(Math.Floor(HSB.H / 60)) Mod 6
      Dim f As Double = HSB.H / 60 - Math.Floor(HSB.H / 60)
      ''计算rgb
      Dim vl As Double = HSB.B * 255 / 100
      Dim v As Integer = Convert.ToInt32(vl)
      Dim p As Integer = Convert.ToInt32(vl * (100 - HSB.S)) / 100
      Dim q As Integer = Convert.ToInt32(vl * (100 - f * HSB.S)) / 100
      Dim t As Integer = Convert.ToInt32(vl * (100 - (100 - f) * HSB.S)) / 100
      ''取值
      Dim Colort As RGB_TYPE
      If hi = 0 Then
        Colort = New RGB_TYPE(v, t, p)
      ElseIf hi = 1 Then
        Colort = New RGB_TYPE(q, v, p)
      ElseIf hi = 2 Then
        Colort = New RGB_TYPE(p, v, t)
      ElseIf hi = 3 Then
        Colort = New RGB_TYPE(p, q, v)
      ElseIf hi = 4 Then
        Colort = New RGB_TYPE(t, p, v)
      Else
        Colort = New RGB_TYPE(v, p, q)
      End If
      Dim RGBT As New RGB_TYPE(Colort.R, Colort.G, Colort.B)
      Return RGBT
    Catch ex As Exception
      Dim RGBT As New RGB_TYPE(0, 0, 0)
      Return RGBT
    End Try
  End Function

End Class

五,最后再看看窗体代码

  • 5.1.用到的WinApi如下
  ''' <summary>
  ''' 取设备场景
  ''' </summary>
  ''' <param name="hwnd">目的DC的句柄</param>
  ''' <returns></returns>
  <DllImport("user32.dll")>
  Private Shared Function GetDC(hwnd As IntPtr) As IntPtr
  End Function

  ''' <summary>
  ''' 取指定点颜色
  ''' </summary>
  ''' <param name="hdc">源DC的句柄</param>
  ''' <param name="p">目的坐标</param>
  ''' <returns></returns>
  <DllImport("gdi32.dll")>
  Private Shared Function GetPixel(hdc As IntPtr, p As Point) As UInteger
  End Function

  <DllImport("gdi32.dll")>
  Public Shared Function GetPixel(hdc As IntPtr, xPos As Integer, yPos As Integer) As UInteger
  End Function

  ''' <summary>
  ''' 取屏幕图片
  ''' </summary>
  ''' <param name="hdcDest">目的DC的句柄</param>
  ''' <param name="nXDest">目的图形的左上角的x坐标</param>
  ''' <param name="nYDest">目的图形的左上角的y坐标</param>
  ''' <param name="nWidth">目的图形的矩形宽度</param>
  ''' <param name="nHeight">目的图形的矩形高度</param>
  ''' <param name="hdcSrc">源DC的句柄</param>
  ''' <param name="nXSrc">源图形的左上角的x坐标</param>
  ''' <param name="nYSrc">源图形的左上角的x坐标</param>
  ''' <param name="dwRop">光栅操作代码</param>
  ''' <returns></returns>
  <DllImport("gdi32.dll")>
  Private Shared Function BitBlt(hdcDest As IntPtr, nXDest As Integer, nYDest As Integer, nWidth As Integer, nHeight As Integer, hdcSrc As IntPtr, nXSrc As Integer, nYSrc As Integer, dwRop As Integer) As Boolean
  End Function
  • 5.2.不做过多的描述了自己看完整代码吧!
Imports System.ComponentModel
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices

Public Class FormGetColor

#Region "WinAPI"

  ''' <summary>
  ''' 取设备场景
  ''' </summary>
  ''' <param name="hwnd">目的DC的句柄</param>
  ''' <returns></returns>
  <DllImport("user32.dll")>
  Private Shared Function GetDC(hwnd As IntPtr) As IntPtr
  End Function

  ''' <summary>
  ''' 取指定点颜色
  ''' </summary>
  ''' <param name="hdc">源DC的句柄</param>
  ''' <param name="p">目的坐标</param>
  ''' <returns></returns>
  <DllImport("gdi32.dll")>
  Private Shared Function GetPixel(hdc As IntPtr, p As Point) As UInteger
  End Function

  <DllImport("gdi32.dll")>
  Public Shared Function GetPixel(hdc As IntPtr, xPos As Integer, yPos As Integer) As UInteger
  End Function

  ''' <summary>
  ''' 取屏幕图片
  ''' </summary>
  ''' <param name="hdcDest">目的DC的句柄</param>
  ''' <param name="nXDest">目的图形的左上角的x坐标</param>
  ''' <param name="nYDest">目的图形的左上角的y坐标</param>
  ''' <param name="nWidth">目的图形的矩形宽度</param>
  ''' <param name="nHeight">目的图形的矩形高度</param>
  ''' <param name="hdcSrc">源DC的句柄</param>
  ''' <param name="nXSrc">源图形的左上角的x坐标</param>
  ''' <param name="nYSrc">源图形的左上角的x坐标</param>
  ''' <param name="dwRop">光栅操作代码</param>
  ''' <returns></returns>
  <DllImport("gdi32.dll")>
  Private Shared Function BitBlt(hdcDest As IntPtr, nXDest As Integer, nYDest As Integer, nWidth As Integer, nHeight As Integer, hdcSrc As IntPtr, nXSrc As Integer, nYSrc As Integer, dwRop As Integer) As Boolean
  End Function

#End Region

  Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim p As New Point(MousePosition.X, MousePosition.Y)
    Get_pc_color(p)
    Get_pc_img(Pic_img, p)
  End Sub

  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Draw_centetPoint(Pic_img) ''绘制耙点
    Bt_istop.LinkBehavior = LinkBehavior.HoverUnderline
    Bt_islock.LinkBehavior = LinkBehavior.HoverUnderline
    ''-----------
    Timer1.Enabled = True
    Timer1.Interval = 100
    Timer1.Start()

  End Sub

  ''' <summary>
  ''' 获取屏幕颜色
  ''' </summary>
  ''' <param name="p"></param>
  Sub Get_pc_color(p As Point)
    Try
      Txt_point.Text = p.X & "," & p.Y '把坐标显示到窗口上
      Dim hdc As IntPtr = GetDC(New IntPtr(0)) '取到设备场景(0就是全屏的设备场景)
      Dim c As Integer = GetPixel(hdc, p.X, p.Y) '取指定点颜色
      Dim r As Integer = c And &HFF '转换R
      Dim g As Integer = (c And &HFF00) / 256  '转换G
      Dim b As Integer = (c And &HFF0000) / 65536 '转换B
      ''---------------------------
      Dim RGB As New Class_ColorHelper.RGB_TYPE(r, g, b)
      ''----------------------------
      Txt_tend.Text = c.ToString() '输出10进制颜色
      Txt_16d.Text = Class_ColorHelper.RGB2HEX(RGB)
      Txt_rgb.Text = RGB.Tostring
      Txt_hsl.Text = Class_ColorHelper.RGB2HSL(RGB).Tostring
      Txt_hsv.Text = Class_ColorHelper.RGB2HSV(RGB).Tostring
      Txt_hsb.Text = Class_ColorHelper.RGB2HSB(RGB).Tostring
      Txt_cmyk.Text = Class_ColorHelper.RGB2CMYK(RGB).Tostring
      PicColor.BackColor = Color.FromArgb(r, g, b)  '设置颜色框

    Catch ex As Exception
      Return
    End Try
  End Sub

  ''' <summary>
  ''' 获取屏幕图片
  ''' </summary>
  ''' <param name="p"></param>
  Sub Get_pc_img(picbox As PictureBox, p As Point)
    Try
      Dim img As New Bitmap(100, 100)
      Dim pcg As Graphics = Graphics.FromImage(img)
      pcg.CopyFromScreen(p.X - 50, p.Y - 50, 0, 0, New Size(100, 100))
      picbox.BackgroundImage = img
    Catch ex As Exception
      Return
    End Try
  End Sub
  ''' <summary>
  ''' 绘制中心点
  ''' </summary>
  Sub Draw_centetPoint(picbox As PictureBox)
    Try
      Dim bmp As New Bitmap(picbox.ClientRectangle.Width, picbox.ClientRectangle.Height)
      Dim dg As Graphics = Graphics.FromImage(bmp)
      dg.DrawLine(New Pen(Color.Blue, 1), New Point(0, 50), New Point(100, 50))
      dg.DrawLine(New Pen(Color.Blue, 1), New Point(50, 0), New Point(50, 100))
      dg.DrawEllipse(New Pen(Color.Blue, 1), New Rectangle(40, 40, 20, 20))
      dg.DrawEllipse(New Pen(Color.Blue, 1), New Rectangle(30, 30, 40, 40))
      picbox.Image = bmp
      dg.Dispose()
    Catch ex As Exception
      Return
    End Try
  End Sub

  Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles Bt_istop.LinkClicked
    If TopMost = True Then
      TopMost = False
      Bt_istop.Text = "置顶"
    Else
      TopMost = True
      Bt_istop.Text = "取消"
    End If
  End Sub

  Private Sub LinkLabel2_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles Bt_islock.LinkClicked
    If Timer1.Enabled = True Then
      Timer1.Stop()
      Timer1.Enabled = False
      Bt_islock.Text = "解锁"
    Else
      Timer1.Enabled = True
      Timer1.Interval = 100
      Timer1.Start()
      Bt_islock.Text = "锁定"
    End If
  End Sub

  Private Sub Form1_HelpButtonClicked(sender As Object, e As CancelEventArgs) Handles Me.HelpButtonClicked
    MessageBox.Show($"1.点击置顶按钮可以把窗体置于屏幕的最上层;{vbCrLf}2.点击空格键或者锁定按钮,可以锁定当前点坐标的颜色值;{vbCrLf}{vbCrLf} 联系方式:{vbCrLf}QQ:463550067{vbCrLf}微信:vbee_club{vbCrLf}微信公众号:VB小源码", "帮助")
    e.Cancel = True
  End Sub

  Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
    If e.KeyCode = Keys.Space Then
      LinkLabel2_LinkClicked(Nothing, Nothing)
    End If
  End Sub

End Class

六,完整工程项目文件下载

https://vbee.lanzouw.com/i37dqu4z4fi
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-09-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 办公魔盒 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • VB.NET 实现屏幕取色器
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档