How do I get the RBG properties of a pixel? (in Visual Basic 6)

I need to know how to get the [red]Red[/red], [green]Green[/green] and [blue]Blue[/blue] properties of a pixel on the screen so I can do fading.

[b]Thank you![/b]

:-)

Comments

  • : I need to know how to get the [red]Red[/red], [green]Green[/green] and [blue]Blue[/blue] properties of a pixel on the screen so I can do fading.
    :
    : [b]Thank you![/b]
    :
    : :-)
    :

    I got this from the API-Guide.

    [code]Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long[/code]

    hdc
    Identifies the device context.

    nXPos
    Specifies the logical x-coordinate of the pixel to be examined.

    nYPos
    Specifies the logical y-coordinate of the pixel to be examined.

    If the function succeeds, the return value is an RGB value. If the pixel is outside of the current clipping region, the return value is CLR_INVALID.

    I don't know how to break the long rgb number into its three elements, but if anyone knows where to get a function for that let me know please.

    hope this helps,
    James
  • Thanks James! :-)
  • : I need to know how to get the [red]Red[/red], [green]Green[/green] and [blue]Blue[/blue] properties of a pixel on the screen so I can do fading.
    :
    : [b]Thank you![/b]
    :
    : :-)
    :
    I am new to vb so i doubt this will help, you probably already know this anyway. This is from the MS VB6 programmers guide.

    The Point method is closely related to the PSET method, but it returns the color value at a particular location:

    PointColor = Point (500, 500)

    I dont know if you can break down the color value to the RGB or not. Sorry if this is no help.
  • : I need to know how to get the [red]Red[/red], [green]Green[/green] and [blue]Blue[/blue] properties of a pixel on the screen so I can do fading.
    :
    : [b]Thank you![/b]
    :
    : :-)
    :
    You can use "Point" to retrieve the color of the pixel. This code will get the pixel and split it up:
    [code]
    Public Type RGB_Short
    Red As Byte
    Green As Byte
    Blue As Byte
    End Type
    Public Declare Sub ltrCopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
    (lpDest As RGB_Short, lpSource As Long, ByVal cBytes As Long)
    Public Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
    Public Function LongToRGB(ByVal lRGB As Long) As RGB_Short

    If (lRGB And &H80000000) = &H80000000 Then lRGB = GetSysColor(lRGB Xor &H80000000)
    ltrCopyMemory LongToRGB, lRGB, 3

    End Function
    [/code]
    Use:
    [code]
    Dim l As RGB_Short
    l = LongToRGB(Point([italic]x[/italic], [italic]y[/italic]))
    MsgBox "Red = " & l.Red & vbCrLf & "Blue = " & l.Blue & vbCrLf & "Green = " & l.Green
    [/code]
  • [b][red]This message was edited by ColdShine at 2002-8-1 3:55:14[/red][/b][hr]
    [code]
    [blue]Declare Function[/blue] OleTranslateColor [blue]Lib[/blue] "OlePro32" ( _
    [blue]ByVal[/blue] clr [blue]As Long[/blue], _
    [blue]ByVal[/blue] hPalette [blue]As Long[/blue], _
    ret [blue]As Long[/blue]) _
    [blue]As Long

    Function[/blue] GetR([blue]ByVal[/blue] clr [blue]As Long[/blue]) [blue]As Byte[/blue]
    [blue]Call[/blue] OleTranslateColor(clr, 0, clr)
    GetR = clr [blue]And[/blue] &HFF&
    [blue]End Function

    Function[/blue] GetG([blue]ByVal[/blue] clr [blue]As Long[/blue]) [blue]As Byte[/blue]
    [blue]Call[/blue] OleTranslateColor(clr, 0, clr)
    GetG = (clr &H100&) [blue]And[/blue] &HFF&
    [blue]End Function

    Function[/blue] GetB([blue]ByVal[/blue] clr [blue]As Long[/blue]) [blue]As Byte[/blue]
    [blue]Call[/blue] OleTranslateColor(clr, 0, clr)
    GetB = (clr &H10000) [blue]And[/blue] &HFF&
    [blue]End Function[/blue]
    [/code]
    So you just call one of the three functions giving it the color you want; it will return you the desired color component.

    Note this will handle [b]any[/b] color, including system colors and palette-relative colors.
    ________________
    [size=1][b][grey]Cold[/grey][blue]Shine[/blue][/b][/size]
  • : [b][red]This message was edited by ColdShine at 2002-8-1 3:55:14[/red][/b][hr]
    : [code]
    : [blue]Declare Function[/blue] OleTranslateColor [blue]Lib[/blue] "OlePro32" ( _
    : [blue]ByVal[/blue] clr [blue]As Long[/blue], _
    : [blue]ByVal[/blue] hPalette [blue]As Long[/blue], _
    : ret [blue]As Long[/blue]) _
    : [blue]As Long
    :
    : Function[/blue] GetR([blue]ByVal[/blue] clr [blue]As Long[/blue]) [blue]As Byte[/blue]
    : [blue]Call[/blue] OleTranslateColor(clr, 0, clr)
    : GetR = clr [blue]And[/blue] &HFF&
    : [blue]End Function
    :
    : Function[/blue] GetG([blue]ByVal[/blue] clr [blue]As Long[/blue]) [blue]As Byte[/blue]
    : [blue]Call[/blue] OleTranslateColor(clr, 0, clr)
    : GetG = (clr &H100&) [blue]And[/blue] &HFF&
    : [blue]End Function
    :
    : Function[/blue] GetB([blue]ByVal[/blue] clr [blue]As Long[/blue]) [blue]As Byte[/blue]
    : [blue]Call[/blue] OleTranslateColor(clr, 0, clr)
    : GetB = (clr &H10000) [blue]And[/blue] &HFF&
    : [blue]End Function[/blue]
    : [/code]
    : So you just call one of the three functions giving it the color you want; it will return you the desired color component.
    :
    : Note this will handle [b]any[/b] color, including system colors and palette-relative colors.
    : ________________
    : [size=1][b][grey]Cold[/grey][blue]Shine[/blue][/b][/size]
    :

    Why the complicated way with API's?

    Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

    Dim color As Long

    color= Picture1.Point(X, Y)
    Label1(0).Caption = color And &HFF&
    Label1(1).Caption = color &H100& And &HFF&
    Label1(2).Caption = color &H10000 And &HFF&
    End Sub


    ------------------------------------------
    Only stupidity of mankind and the universe
    are infinite, but i'm not sure concerning
    the universe. A. Einstein

  • : : [b][red]This message was edited by ColdShine at 2002-8-1 3:55:14[/red][/b][hr]
    : : [code]
    : : [blue]Declare Function[/blue] OleTranslateColor [blue]Lib[/blue] "OlePro32" ( _
    : : [blue]ByVal[/blue] clr [blue]As Long[/blue], _
    : : [blue]ByVal[/blue] hPalette [blue]As Long[/blue], _
    : : ret [blue]As Long[/blue]) _
    : : [blue]As Long
    : :
    : : Function[/blue] GetR([blue]ByVal[/blue] clr [blue]As Long[/blue]) [blue]As Byte[/blue]
    : : [blue]Call[/blue] OleTranslateColor(clr, 0, clr)
    : : GetR = clr [blue]And[/blue] &HFF&
    : : [blue]End Function
    : :
    : : Function[/blue] GetG([blue]ByVal[/blue] clr [blue]As Long[/blue]) [blue]As Byte[/blue]
    : : [blue]Call[/blue] OleTranslateColor(clr, 0, clr)
    : : GetG = (clr &H100&) [blue]And[/blue] &HFF&
    : : [blue]End Function
    : :
    : : Function[/blue] GetB([blue]ByVal[/blue] clr [blue]As Long[/blue]) [blue]As Byte[/blue]
    : : [blue]Call[/blue] OleTranslateColor(clr, 0, clr)
    : : GetB = (clr &H10000) [blue]And[/blue] &HFF&
    : : [blue]End Function[/blue]
    : : [/code]
    : : So you just call one of the three functions giving it the color you want; it will return you the desired color component.
    : :
    : : Note this will handle [b]any[/b] color, including system colors and palette-relative colors.
    : : ________________
    : : [size=1][b][grey]Cold[/grey][blue]Shine[/blue][/b][/size]
    : :
    :
    : Why the complicated way with API's?
    :
    : Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    :
    : Dim color As Long
    :
    : color= Picture1.Point(X, Y)
    : Label1(0).Caption = color And &HFF&
    : Label1(1).Caption = color &H100& And &HFF&
    : Label1(2).Caption = color &H10000 And &HFF&
    : End Sub
    :
    :
    : ------------------------------------------
    : Only stupidity of mankind and the universe
    : are infinite, but i'm not sure concerning
    : the universe. A. Einstein
    :
    :
    I have a similar problem, but I use Matrox Inspector, which is based on Basic. This program generates some code when you open an image. What I've noticed is that in the functions that are posted here, the images are put into the functions as long, while in Matrox Inspector it is read and saved as a String. If I try something like this ( R_roi being the image ) I get an error message saying it doesn't match..
    [code]
    R_roi.GetR()
    [/code]
    Is this then an Inspector problem, or is there a way to convert the String image to a Long image ?
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories