Results 1 to 14 of 14
  1. #1
    Lounger
    Join Date
    Apr 2001
    Location
    Johannesburg
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts

    RGB colour of a pixel

    Hi

    How do I determine the colour of a specific pixel in an image? The image could be stored on a disk or pasted in an office document. I would like to use VBA code if possible. Hope someone can help me.

    Regards

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: RGB colour of a pixel

    <post:=561,828>post 561,828</post:> mentions a free interactive tool Meazure.

    I'm not sure how one could use VBA to obtain the color of a pixel in an image that "could be stored on a disk or pasted in an office document". How would you specify the pixel?

  3. #3
    Lounger
    Join Date
    Apr 2001
    Location
    Johannesburg
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: RGB colour of a pixel

    Say I have an image 100 pixels wide and 100 pixels high. I want to know what is the colour properties of the pixel at 20,50 (if the top left pixel is 1,1). Lets say the image is stored on disk. Hope I am explaining myself better.

    Thanx

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: RGB colour of a pixel

    That would be rather complicated, since various file types (.bmp, .gif, .jpg, ...) store the image in different ways.

    A dedicated image processing library such as Victor probably provides what you need, but it is expensive (US$ 499).

  5. #5
    Lounger
    Join Date
    Apr 2001
    Location
    Johannesburg
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: RGB colour of a pixel

    Thanx Hans, I was hoping for a simple solution.
    Anyone else with ideas please reply!

    Regards

  6. #6
    Plutonium Lounger Leif's Avatar
    Join Date
    Dec 2000
    Location
    U.K.
    Posts
    14,010
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: RGB colour of a pixel

    If you are into Java... Get the color of a specific pixel

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: RGB colour of a pixel

    Do you have VB6 or VB Express?

  8. #8
    Lounger
    Join Date
    Apr 2001
    Location
    Johannesburg
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: RGB colour of a pixel

    I was hoping to do it in VBA from Excel. No VB6 or VB Express. If it is possible in VB I could ask a friend to do it for me.

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: RGB colour of a pixel

    I have attached a bare-bones example. It lets you load a picture onto a userform and it uses some Windows API functions to get the RGB value of a specified pixel. There are no checks for valid coordinates or a valid picture.

    Code in general module:

    Public Declare Function GetPixel Lib "gdi32" _
    (ByVal hDC As Long, ByVal x As Long, ByVal y As Long) As Long

    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

    Public Declare Function GetDC Lib "User32.dll" _
    (ByVal hWnd As Long) As Long

    Sub ShowForm()
    UserForm1.Show
    End Sub

    Code behind command button on userform:

    Private Sub CommandButton1_Click()
    Dim hWnd As Long
    Dim hDC As Long
    Dim lngColor As Long
    Dim strFile As String

    On Error GoTo ErrHandler

    With Application.FileDialog(msoFileDialogFilePicker)
    If .Show = True Then
    strFile = .SelectedItems(1)
    Else
    Beep
    Exit Sub
    End If
    End With
    Me.Picture = LoadPicture(strFile)
    hWnd = FindWindow("ThunderDFrame", Me.Caption)
    hDC = GetDC(hWnd)
    lngColor = GetPixel(hDC, Me.txtX, Me.txtY)
    MsgBox lngColor
    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbExclamation
    End Sub
    Attached Files Attached Files

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: RGB colour of a pixel

    Here is a slightly improved version - it repaints the form to ensure that the picture has been drawn correctly before getting the pixel color, and the color is split into R, G and B components.
    Attached Files Attached Files

  11. #11
    Uranium Lounger
    Join Date
    Mar 2001
    Location
    New Jersey
    Posts
    6,684
    Thanks
    1
    Thanked 11 Times in 11 Posts

    Re: RGB colour of a pixel

    Take a look at Match Head v1.5. I've used it for years and it is installed on my XP SP2 system now. Works just as advertised. HTH <img src=/S/smile.gif border=0 alt=smile width=15 height=15>
    <IMG SRC=http://www.wopr.com/w3tuserpics/DocWatson_sig.gif>

  12. #12
    Lounger
    Join Date
    Apr 2001
    Location
    Johannesburg
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: RGB colour of a pixel

    Hans

    Thank you for your effort your code works fine. The form can however not load a PNG file. Would it be possible to load a PNG file somehow or would I need to convert the images to JPG or BMP. If I need to convert, do you know of maybe a command line conversion utility that I can use to automate the task?

  13. #13
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: RGB colour of a pixel

    According to LoadPicture Function, the supported file types are .bmp .ico .cur .rle .wmf .emf .gif and .jpg.

    Although I've never used it, ImageMagick appears to be able to convert using a command line.

  14. #14
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: RGB colour of a pixel

    ImageMagick is a handy utility, if somewhat poorly documented. It includes a COM object to run command line conversions from a program or a VBScript. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    This is some sample code I wrote to automate ImageMagick in 2005. I was exporting PowerPoint slides and wanted GIF images with a particular color transparent, which I could not do with native PowerPoint methods. This code exports each slide to a PNG file and then converts it to GIF using ImageMagick. I think.

    <code>' ImageMagick COM object required for the following !!
    Dim objIM As New ImageMagickObject.MagickImage, msgIM As String
    Load frmStatus
    frmStatus.Show vbModeless
    For Each aSlide In ActivePresentation.Slides
    frmStatus.lblMessage = "Converting slide " & aSlide.SlideIndex & " to image..."
    frmStatus.Repaint
    If aSlide.SlideShowTransition.Hidden = msoFalse Then
    strImgPath = strPath & "" & strFileRoot & "_" & _
    Right("00" & Trim(Str(aSlide.SlideIndex)), 3) & "."
    aSlide.Export strImgPath & "png", "png", lngWidth, 0.75 * lngWidth
    msgIM = objIM.Convert("-transparent", "#00FF00", strImgPath & "png", strImgPath & "gif")
    Debug.Print strImgPath, msgIM
    End If
    Next
    Unload frmStatus
    Set frmStatus = Nothing
    Set objIM = Nothing</code>

Posting Permissions

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