Results 1 to 10 of 10

Thread: Get pixel color by coordinates (PNG)

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2024
    Posts
    4

    Question Get pixel color by coordinates (PNG)

    Dear VB developers,

    I used to think that it was impossible to get the pixel color by coordinates when we talk about PNG file, because Microsoft reported that this format is not supported by VBScript. But then I found out about WIA library, which is available on many computers, and even successfully rotated some PNG image. Now it's time to reach out to you.

    Name:  ac5d9fa149d2cb41a47aa9f428c0e748.png
Views: 516
Size:  231 Bytes

    How to make getPixelColor.vbs, which runs from the command line, picks image's filename and two coordinates as input (for example, image.png 350 15) and then outputs the color as RGB values (for example, 255 0 0)?

  2. #2
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,563

    Re: Get pixel color by coordinates (PNG)


  3. #3
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,233

    Wink Re: Get pixel color by coordinates (PNG)

    This code should do the trick. It loads a PNG file and then displays the color value at coordinates (XPos,YPos):

    Code:
    Dim XPos As Long, YPos as Long
    
        With New WIA.ImageFile
            .LoadFile "filename.png"
            Debug.Print Hex$(.ARGBData(XPos + YPos * .Width))
        End With

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2024
    Posts
    4

    Re: Get pixel color by coordinates (PNG)

    fafalone,
    what a relief to know there are VB ways to solve this case!
    It remains to understand how to apply them more wisely.

    VanGoghGaming,
    thank you for a quick reply, but your code throws an error, alas.
    Let me clarify VB's version is 5.8.9600.16428 on Windows 7 x64.

    Name:  2024-0108-2150 getPixelColor fail 01.png
Views: 462
Size:  3.6 KB

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Get pixel color by coordinates (PNG)

    You posted in the wrong forum. You want the incorrectly-named "ASP, VB Script" forum.

    Yes it should be "ASP, VBScript" but that's water over the dam at this late date.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2024
    Posts
    4

    Re: Get pixel color by coordinates (PNG)

    It seems that our dear administrators have moved this topic
    to the proper forum section and now it is time to say *bump*
    to attract attention of the developers. Folks, help!

  7. #7
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,047

    Re: Get pixel color by coordinates (PNG)

    Here is VanGoghGaming's code for VBScript:

    Code:
    XPos = WScript.Arguments.Named("x") + 0
    YPos = WScript.Arguments.Named("y") + 0
    
    With CreateObject("WIA.ImageFile")
        .LoadFile WScript.Arguments.Named("f")
        WScript.echo Right("00000000" & Hex(.ARGBData(XPos + YPos * .Width)), 8)
    End With
    Use it like this:

    c:> cscript /nologo aaa.vbs /f:aaa.png /x:0 /y:42

    . . . where /f is the PNG filename while /x and /y are the coordinates of the pixel.

    cheers,
    </wqw>

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2024
    Posts
    4

    Re: Get pixel color by coordinates (PNG)

    wqweto
    It works, indeed. God bless you for your help.

  9. #9

    Re: Get pixel color by coordinates (PNG)

    How do you plan to implement the suggested VBScript code for obtaining pixel color by coordinates in a PNG file from the command line, considering the version constraints you mentioned?

  10. #10
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,376

    Re: Get pixel color by coordinates (PNG)

    Note that Microsoft is planning to deprecate VBScript at some point:

    https://www.vbforums.com/showthread....ipt+deprecated

    https://www.vbforums.com/showthread....ipt+deprecated

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