Results 1 to 3 of 3

Thread: rgb value?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Posts
    65
    If I put a bmp into a picturebox or image control
    is there a way of getting the rgb value under a
    mouse click inside the control at run time?

  2. #2
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892

    Cool PictureBox is kind, Image is evil...

    Use the PictureBox, it's much less evil, the Image is only for decorations... If you don't want a border, well, that's what the BorderStyle property is there for.

    This code goes in the form where the PictureBox (Picture1 in this example) is.
    Code:
    Option Explicit
    
    Private Declare Function GetPixel Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long) As Long
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
    
    Private Type POINTAPI
            X As Long
            Y As Long
    End Type
    
    Sub GetRGB(ByVal lColor As Long, ByRef lRed As Long, ByRef lGreen As Long, ByRef lBlue As Long)
        Dim sColor As String
        
        ' RGB Hex format is BbGgRr, not RrGgBb, because this is a little-endian system
        ' Let's not get into this, ok? It just is like that!
        sColor = Right("000000" & Hex(lColor), 6)
        lRed = CLng("&H" & Right(sColor, 2)) ' Gets this: bbggRR
        lGreen = CLng("&H" & Mid(sColor, 2, 2)) ' Gets this: bbGGrr
        lBlue = CLng("&H" & Left(sColor, 2)) ' Gets this: BBggrr
    End Sub
    
    Private Sub Picture1_Click()
        Dim ptCursorPos As POINTAPI, lColor As Long, lRed As Long, lGreen As Long, lBlue As Long
        
        Call GetCursorPos(ptCursorPos) ' Get mouse cursor location
        Call ScreenToClient(Picture1.hWnd, ptCursorPos) ' Translate location on screen to location in Picture1
        lColor = GetPixel(Picture1.hDC, ptCursorPos.X, ptCursorPos.Y) ' Get color in that location
        Call GetRGB(lColor, lRed, lGreen, lBlue) ' Translate color to Red, Green and Blue
        
        Caption = "R = " & lRed & ", G = " & lGreen & ", B = " & lBlue
    End Sub

  3. #3
    Addicted Member
    Join Date
    Feb 2000
    Posts
    224
    If you want to get the pixel colors only within the picturebox there is a simple solution.

    Use the Picture1.Point(X,Y) ...

    '-----------------------------------------------
    Dim Curx As Single
    Dim Cury As Single

    Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Curx = X
    Cury = Y
    End Sub

    Private Sub Picture1_Click()
    MsgBox Picture1.Point(Curx,Cury)
    End Sub
    '------------------------------------------------

    This will display the long value of the color of the pixel.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width