Results 1 to 7 of 7

Thread: Getting pixel colors with DCs

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    Getting pixel colors with DCs

    Hello guys,
    that's my code:
    Code:
    dim DCSprite as long
    dim BMPSprite as long
    
    Public Function SetPicture(hDC As Long, lWidth As Long, lHeight As Long) As Boolean
        DCSprite = CreateCompatibleDC(0)
        BMPSprite = CreateCompatibleBitmap(hDC, lWidth, lHeight)
        If (DCSprite < 1) Or (BMPSprite < 1) Then
            SetPicture = False
            Exit Function
        End If
    
        SelectObject DCSprite, BMPSprite
        BitBlt DCSprite, 0, 0, lWidth, lHeight, hDC, 0, 0, vbSrcCopy
        
        SetPicture = True
    
    End Function
    All works fine, if I bitblt to form1 I can get my image, but if I try to get the color of pixels with GetPixel it returns always -1...

    Any suggestions??!?
    Thx,
    Xmas

  2. #2
    Addicted Member
    Join Date
    Oct 2002
    Location
    Somewhere out in space
    Posts
    151
    on wich dc do you use the getpixel? I've tried the following with your code:
    VB Code:
    1. Public Function SetPicture(hdc As Long, lWidth As Long, lHeight As Long) As Boolean
    2.  
    3.     DCSprite = CreateCompatibleDC(0)
    4.     BMPSprite = CreateCompatibleBitmap(hdc, lWidth, lHeight)
    5.    
    6.     If (DCSprite < 1) Or (BMPSprite < 1) Then
    7.        
    8.         SetPicture = False
    9.         Exit Function
    10.        
    11.     End If
    12.    
    13.     SelectObject DCSprite, BMPSprite
    14.     BitBlt DCSprite, 0, 0, lWidth, lHeight, hdc, 0, 0, vbSrcCopy
    15.     SetPicture = True
    16.     MsgBox GetPixel(DCSprite, 2, 0)
    17.     BitBlt Picture2.hdc, 0, 0, lWidth, lHeight, DCSprite, 0, 0, vbSrcCopy
    18.     Picture2.Refresh
    19.     MsgBox GetPixel(Picture2.hdc, 2, 0)
    20.    
    21. End Function

    and the getpixel work both times.

    can you post a bigger part of code or your project, maybe i'll have a better chance to see what you are doing wrong.

    - Valkan
    'You keep creatures in cages and release them to fight? That's sick!'

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    Oh well, Tonight I was "arrabbiato" so I changed all my code and I decided to do it with picture1.point, and setpixel... I used picture1.point (that is the slowest) because getpixel always returns -1!!!!
    I used my previous code,for a picturebox, for a form... Nothing... Picture1.autoredraw and form1.autoredraw were true they were visible, all seemed to be correct but gepixel returned -1. I can't understand why... Before i post I tried to see if getpixel worked, stopping my program and in the writing in the immediate window:
    Code:
    print getpixel(form1.hdc,2,0)  <---- Sure here I'm in boundaries...
    returned -1... Something wrong in my computer yesterday night ??!?
    I looked at this forum for other posts and I found that people had my problem and they resolved it, They had not set autoredraw property to true, but in my project they were right!!!

    Does getpixel works with picture1.visible=false? in my computer no. It doesn't work neither with picture1.visible=true...

    I'm unhappy...

    Thx Xmas.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    I started a new project, put on form1 two pictureboxes, one command button and the below code:

    Code:
    Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, _
     ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, _
     ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, _
     ByVal dwRop As Long) As Long
    Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, _
     ByVal hObject As Long) As Long
    Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
    Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, _ 
    ByVal nWidth As Long, ByVal nHeight As Long) As Long
    Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, _ 
    ByVal x As String, ByVal y As Long) As Long
    
    
    Public Function SetPicture(hdc As Long, lWidth As Long, lHeight As Long) As Boolean
        Dim dcsprite As Long
        Dim bmpsprite As Long
        
        dcsprite = CreateCompatibleDC(0)
        bmpsprite = CreateCompatibleBitmap(hdc, lWidth, lHeight)
        
        If (dcsprite < 1) Or (bmpsprite < 1) Then
            
            SetPicture = False
            Exit Function
            
        End If
        
        SelectObject dcsprite, bmpsprite
        BitBlt dcsprite, 0, 0, lWidth, lHeight, hdc, 0, 0, vbSrcCopy
        SetPicture = True
        MsgBox GetPixel(dcsprite, 2, 0)
        BitBlt Picture2.hdc, 0, 0, lWidth, lHeight, dcsprite, 0, 0, vbSrcCopy
        Picture2.Refresh
        MsgBox GetPixel(Picture2.hdc, 2, 0)
    
    End Function
    
    Private Sub Command1_Click()
        Call SetPicture(Picture1.hdc, Picture1.ScaleWidth, Picture1.ScaleHeight)
    End Sub
    I think this is what you've done.
    Two Msgboxes appear, both of them give me -1.

    Scalemode properties of form, picture1 and picture2 are set to Pixel. They have also autoredraw property set to true.

    Xmas.

  5. #5
    Addicted Member
    Join Date
    Oct 2002
    Location
    Somewhere out in space
    Posts
    151

    Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, _
    ByVal x As String, ByVal y As Long) As Long


    should be: Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, _
    ByVal x As long, ByVal y As Long) As Long

    now it will work

    - valkan
    'You keep creatures in cages and release them to fight? That's sick!'

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    uhm...














    AAAAAAAAAAAAAAAAAAAAHHHHHHHHHHHHHHH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!



    Thx Valkan, I copied that declaration, but I didn't looked at it... I was sure of it...

  7. #7
    Addicted Member
    Join Date
    Oct 2002
    Location
    Somewhere out in space
    Posts
    151
    nah, don't worry, stuff like this happens to everyone once in a while (and that of course include me)... you've got an error, look closely at all the more complicated part of your code because you are sure that the error is got to be there... only to find out after what can be a long while that the error was caused by the simplest of mistake. Welcome to the club budy
    'You keep creatures in cages and release them to fight? That's sick!'

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