Results 1 to 24 of 24

Thread: API get pixel

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Posts
    492

    API get pixel

    I have question, is it windows is provide some API can easy and quick to get (bmp/picture) that Pixel?

    I want to get that pixel around 200-300 bmp file(s), any advice please!
    Attached Images Attached Images  

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: API get pixel

    GetPixel retrieves the pixel from the bitmap image's (hdc) X and Y position specified in the API and returns it's color

    Public Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Posts
    492

    Re: API get pixel

    I found MSDN to get more about gdi32/getpixel
    http://msdn.microsoft.com/en-us/libr...amp;cs-lang=vb

    but I not undetstand how to start, any simple sample tell me please!

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: API get pixel

    Private Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long

    Dim c As Long

    c = GetPixel(Picture1.hdc, 0, 0)


    c will contain the long color value of the upper left corner (X = 0, Y = 0) of Picture1.Picture

    This is for VB6 and earlier. If you are using VB.NET you are in the wrong Forum


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: API get pixel

    Judging from the screenshot, I think rpool wants to retrieve each bitmap file's image dimensions. If so, then the GetDIBits function might help achieve that.

    Quote Originally Posted by MSDN
    If lpvBits is NULL and the bit count member of BITMAPINFO is initialized to zero, GetDIBits fills in a BITMAPINFOHEADER structure or BITMAPCOREHEADER without the color table. This technique can be used to query bitmap attributes.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Posts
    492

    Re: API get pixel

    I am sorry, not clearly my question,
    yes, I need get that bitmap.picture.dimensions (result: need use "pixel" value)
    but where to start ?
    please!


    EDIT: Width / 26.45 and Height/26.45
    but not understand why use 26.45?
    Code:
    myPic_W = myPic.Width / 26.45 'changed to pixel
    myPic_H = myPic.Height / 26.45 'changed to pixel
    Code:
    Option Explicit
    
    Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
    
    Private Sub Command1_Click()
    
    Dim myPic As StdPicture
    Dim myPic_W As Long
    Dim myPic_H As Long
    Form1.Picture1.ScaleMode = 3 'pixel
    Form1.Picture1.AutoRedraw = True
    Form1.Picture1.AutoSize = True
    
    Set myPic = LoadPicture("R:\st.bmp") 
    Picture1.Picture = myPic
    myPic_W = myPic.Width
    myPic_H = myPic.Height
    
    Debug.Print Time, myPic_W, myPic_H
    
    Dim c As Long
    c = GetPixel(Picture1.hdc, 0, 0)
    Debug.Print c
    
    Set myPic = Nothing
    End Sub
    Attached Images Attached Images  
    Last edited by rpool; Jun 15th, 2013 at 07:21 AM.

  7. #7
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: API get pixel

    Quote Originally Posted by rpool View Post
    EDIT: Width / 26.45 and Height/26.45
    but not understand why use 26.45?
    As you can see, you don't understand what that magic number means. Therefore, you should avoid magic numbers whenever possible. Use constants instead!

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        Dim myPic_W As Long
        Dim myPic_H As Long
    
        With Form1.Picture1
            .ScaleMode = vbPixels
            .AutoRedraw = True
            .AutoSize = True
    
            Set .Picture = LoadPicture("R:\st.bmp")
            myPic_W = ScaleX(.Picture.Width, vbHimetric, vbPixels)
            myPic_H = ScaleY(.Picture.Height, vbHimetric, vbPixels)
    
            Debug.Print Time, myPic_W & "x" & myPic_H
        End With
    End Sub
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  8. #8
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: API get pixel

    Why don't you just use Picture1.Width and Picture1.Height only? Why the other stuff?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  9. #9
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: API get pixel

    Quote Originally Posted by jmsrickland View Post
    Why don't you just use Picture1.Width and Picture1.Height only? Why the other stuff?
    Because Picture1.Width and Picture1.Height returns the size of the PictureBox control itself while the dimensions of the PictureBox's Picture property are obtained through the similarly named Width and Height properties of the StdPicture class. The Picture property's dimensions are usually independent from the size of the PictureBox, but in this case, since the AutoSize property was set to True, both dimensions of the PictureBox and its Picture property coincide with each other. However, I decided not to rely on this fact, but instead I used the ScaleX/ScaleY methods of the Form to convert the true dimensions of the Bitmap Picture from HiMetric to Pixels.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  10. #10
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: API get pixel

    Then use ScaleWidth and ScaleHeight.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  11. #11
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: API get pixel

    Quote Originally Posted by jmsrickland View Post
    Then use ScaleWidth and ScaleHeight.
    Well, they don't always correspond with the dimensions of the Picture property, so they're also unreliable. A PictureBox can be bigger or smaller than the Picture assigned to it, thus its Width/Height or ScaleWidth/ScaleHeight won't always agree with the dimensions of its Picture property.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  12. #12
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: API get pixel

    Interesting. Never ran into that problem in all the years I have used pictureboxes in VB programming. I'll take your work for it however


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  13. #13
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: API get pixel

    Bonnie

    Well, they don't always correspond with the dimensions of the Picture property, so they're also unreliable.
    True dat ..

    BTW, how did you get the keywords to go blue in your post #7.
    It's brilliant.

    Spoo

  14. #14
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: API get pixel

    There is a Text Color drop-down button on the editor's toolbar.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  15. #15
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: API get pixel

    Quote Originally Posted by Spoo View Post
    Bonnie



    True dat ..

    BTW, how did you get the keywords to go blue in your post #7.
    It's brilliant.

    Spoo
    Spoo, How did you get "keywords to go blue" in your post?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  16. #16
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: API get pixel

    Here is another way of doing it, very easy.
    You will need 'Microsoft Window Image Acquisition Library v2.0' installed on the system (which I'm sure you do).

    Code:
    Private Function GetImageDimension(FileName As String) As String
    Dim ImageFile As Object
    
        Set ImageFile = CreateObject("WIA.ImageFile")
        
        With ImageFile
            .LoadFile (FileName)
             GetImageDimension = .Width & " x " & .Height
        End With
        
    End Function
    
    Private Sub Command1_Click()
        MsgBox GetImageDimension("C:\1.bmp")
    End Sub
    You could do much more with WIA 2.0, ask dilettante

  17. #17
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: API get pixel

    Just to add, Bonnie West's example works very good, but is a bit slow because it loads picture into picturebox.
    Bonnie West is a great teacher and knows a lot more than I do, but testing Bonnie's example with image folder (image list) it was very slow!

    Using the WIA 2.0 looking for 10 images' dimension takes about under 1 second .
    Using picturebox (Bonnie's example) took about 4 seconds!

    Depending what you need it for, I would go with WIA 2.0.

    If you only need the one dimension and that's it, then you could go with Bonnie's example.

  18. #18
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: API get pixel

    There is another approach: ask Windows Explorer (a.k.a. Shell32).

    This can be quick or a littler slower depending on whether it already has the info cached from a previous viewing. But at worst it might still be much quicker than completely loading each image file and decoding it far enough to extract the dimensions the way previous suggestions do.

    Another approach to consider anyway. Here I request an image folder to list, then pull out the files (not traversing subfolders though you could do that too). Then I extract a few properties and extended properties and dump them into a flexgrid.

    Name:  sshot.png
Views: 4614
Size:  20.1 KB
    Attached Files Attached Files
    Last edited by dilettante; Jun 15th, 2013 at 07:57 PM. Reason: attachment updated

  19. #19
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: API get pixel

    Quote Originally Posted by jmsrickland View Post
    Spoo, How did you get "keywords to go blue" in your post?
    JMS

    Well, funny enough, I used Text Color drop-down button on the editor's toolbar

    Bonnie

    Well then, kudos for taking the time to highlight each keyword.

    Spoo

  20. #20
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: API get pixel

    You know what Spoo, I always wondered that too, about the blue text in Bonnie's post.

  21. #21
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: API get pixel

    Regarding my post #18 above:

    Note that results might vary from version to version of Windows. Sometimes the extended properties have different "friendly names" and then you'd want to fetch them by FMTID and PID (combined as a SCID), which you'd need to look up.

    ExtendedProperty Method

    sPropName argument:

    Required. String value that specifies the property. There are two ways to specify a property. The first is to assign the property's name, such as "Author" or "Date", to sPropName. However, each property is a member of a COM property set and can also be identified by specifying its format ID (FMTID) and property ID (PID). An FMTID is a GUID that identifies the property set, and a PID is an integer that identifies a particular property within the property set. Specifying a property by its FMTID/PID values is usually more efficient than using its name. To use a property's FMTID/PID values with ExtendedProperty, they must be combined into an SCID. An SCID is a string that contains the FMTID/PID values in the form "FMTID,PID", where the FMTID is the string form of the property set's GUID. For example, the SCID of the summary information property set's author property is "{F29F85E0-4FF9-1068-AB91-08002B27B3D9},4". For a list of FMTIDs and PIDs that are currently supported by the Shell, see SHCOLUMNID.
    Ok, went back and got the info to make SCIDs for the extended properties I've used here. Updating attachment above.

  22. #22
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: API get pixel

    Your attachment worked for me dilettate (just to let you know).
    It would be nice if you did fix that issue for other versions of windows, I am using Winows 7 Home Premium (x64).

  23. #23
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: API get pixel

    Got it.

    Sure was a pain figuring out where to dig it out from though. The info isn't where they say it is in that quote above.

    Even so, it requires Shell 5.00 or later so it probably won't work at all prior to Windows Me and 2000.
    Last edited by dilettante; Jun 15th, 2013 at 08:05 PM.

  24. #24
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: API get pixel

    Quote Originally Posted by Max187Boucher View Post
    Just to add, Bonnie West's example works very good, but is a bit slow because it loads picture into picturebox.
    ... but testing Bonnie's example with image folder (image list) it was very slow!
    Actually, I just modified the OP's code a bit, but you are right, of course! Accessing the Width and Height property of the Picture property of the PictureBox control will definitely be slower than the other methods presented here.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

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