Sacar el color del Pixel que se ha pulsado con VB.NET

Iniciado por z3nth10n, 17 Julio 2013, 22:19 PM

0 Miembros y 1 Visitante están viendo este tema.

z3nth10n

Hola buenas, es posible lo que intento hacer o es una locura?

Un saludo. ;)

Interesados hablad por Discord.

Eleкtro

Cita de: Ikillnukes en 17 Julio 2013, 22:19 PMHola buenas, es posible lo que intento hacer o es una locura?

Se puede hacer -> [SOURCE] Color.NET Autor: EleKtro H@cker



Código (vbnet) [Seleccionar]

#Region " Get Pixel Color "

    ' [ Get Pixel Color Function ]
    '
    ' // By Elektro H@cker
    '
    ' Examples :
    '
    ' Dim RGB As Color = Get_Pixel_Color(MousePosition.X, MousePosition.Y, ColorType.RGB)
    ' MsgBox(Get_Pixel_Color(100, 100, ColorType.RGB).ToString)
    ' MsgBox(Get_Pixel_Color(100, 100, ColorType.HEX))
    ' MsgBox(Get_Pixel_Color(100, 100, ColorType.HTML))

    <System.Runtime.InteropServices.DllImport("user32.dll")> Shared Function GetDC(hwnd As IntPtr) As IntPtr
    End Function

    <System.Runtime.InteropServices.DllImport("user32.dll")> Shared Function ReleaseDC(hwnd As IntPtr, hdc As IntPtr) As Int32
    End Function

    <System.Runtime.InteropServices.DllImport("gdi32.dll")> Shared Function GetPixel(hdc As IntPtr, nXPos As Integer, nYPos As Integer) As UInteger
    End Function

    Public Enum ColorType
        RGB
        HEX
        HTML
    End Enum

    Public Function Get_Pixel_Color(ByVal x As Int32, ByVal y As Int32, ByVal ColorType As ColorType)

        Dim hdc As IntPtr = GetDC(IntPtr.Zero)
        Dim pixel As UInteger = GetPixel(hdc, x, y)
        ReleaseDC(IntPtr.Zero, hdc)

        Dim RGB As Color = Color.FromArgb(CType((pixel And &HFF), Integer), CType((pixel And &HFF00), Integer) >> 8, CType((pixel And &HFF0000), Integer) >> 16)
        Dim R As Int16 = RGB.R, G As Int16 = RGB.G, B As Int16 = RGB.B
        Dim HEX_R As String, HEX_G As String, HEX_B As String

        Select Case ColorType
            Case ColorType.RGB : Return RGB
            Case ColorType.HEX
                If Hex(R) = Hex(0) Then HEX_R = "00" Else HEX_R = Hex(R)
                If Hex(G) = Hex(0) Then HEX_G = "00" Else HEX_G = Hex(G)
                If Hex(B) = Hex(0) Then HEX_B = "00" Else HEX_B = Hex(B)
                Return (HEX_R & HEX_G & HEX_B)
            Case ColorType.HTML : Return ColorTranslator.ToHtml(RGB)
            Case Else : Return Nothing
        End Select

    End Function

#End Region