[vb.net] GetPixel kleur

Pagina: 1
Acties:
  • 104 views sinds 30-01-2008
  • Reageer

  • Rhapsody
  • Registratie: Oktober 2002
  • Laatst online: 23:15

Rhapsody

In Metal We Trust

Topicstarter
hey,

ik ben bezig met een klein proggie om de kleur van de locatie van de cursor (kleur onder de cursor dus) te achterhalen.

Nu heb ik me helemaal rot gezocht, op google, codeguru, mdsn, GoT etc, en ik heb nu dit:

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
   <StructLayout(LayoutKind.Sequential)> _
     Private Structure POINTAPI
        Dim x As Int32
        Dim y As Int32
    End Structure

    Private Declare Function GetCursor Lib "user32" () As Int32
 
   Private Declare Function GetCursorPos Lib "user32" 
Alias "GetCursorPos" (ByRef lpPoint As POINTAPI) As Int32
 
   Private Declare Function WindowFromPoint Lib "user32" 
(ByVal xPoint As Int32, ByVal yPoint As Int32) As Int32
 
   Public Declare Function GetPixel Lib "gdi32" 
Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) 
As Long

    Public Declare Function GetDC Lib "user32" 
Alias "GetDC" (ByVal hwnd As Long) As Long


        Dim clr As Long
        GetCursorPos(pt)
        Dim myWin = WindowFromPoint(pt.x, pt.y)
        Dim myDC = GetDC(myWin)
        MessageBox.Show(GetPixel(myDC, pt.x, pt.y))



nu geeft het stukje code in de messagebox altijd "9222812406911074303" terug, wat dus al niet kan kloppen.

Het 2de probleem is dat ik nergens wat vind om Long te converteren naar een system.drawing.color.

[ Voor 12% gewijzigd door Rhapsody op 08-05-2003 22:04 . Reden: layout ]

🇪🇺 pro Europa!


  • RayNbow
  • Registratie: Maart 2003
  • Laatst online: 01:20

RayNbow

Kirika <3

De X en Y coordinaten van GetPixel moeten lokale coordinaten zijn... dus de coordinaten van het window en niet het scherm. Probeer eens met Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Long, lpRect As RECT) As Long de schermcoordinaten van het window te krijgen en daarmee te rekenen.

Ipsa Scientia Potestas Est
NNID: ShinNoNoir


  • SWfreak
  • Registratie: Juni 2001
  • Niet online
Het lijkt erop dat er ergens iets mis gaat. Controleer eens of de geretourneerde waarden van alle functies wel goed zijn (ofwel dat ze niet falen).

  • Rhapsody
  • Registratie: Oktober 2002
  • Laatst online: 23:15

Rhapsody

In Metal We Trust

Topicstarter
RayNbow schreef op 09 May 2003 @ 11:53:
De X en Y coordinaten van GetPixel moeten lokale coordinaten zijn... dus de coordinaten van het window en niet het scherm. Probeer eens met Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Long, lpRect As RECT) As Long de schermcoordinaten van het window te krijgen en daarmee te rekenen.
Als ik mij niet vergis geef ik aan welk venster er gebruikt moet worden door:

code:
1
2
        Dim myWin = WindowFromPoint(pt.x, pt.y)
        Dim myDC = GetDC(myWin)

🇪🇺 pro Europa!


  • RayNbow
  • Registratie: Maart 2003
  • Laatst online: 01:20

RayNbow

Kirika <3

Ja, maar de coordinaten die jij aan GetPixel geeft zijn schermcoordinaten. GetPixel verwacht echter lokale coordinaten.

Ik heb ff een VB6 voorbeeld proggie gemaakt:
(Nodig: Form1, Timer1)

Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Option Explicit
Private Type POINTAPI
        x As Long
        y As Long
End Type

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
 
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
'Private Declare Function GetDesktopWindow Lib "user32" () As Long

Private Sub Timer1_Timer()
    Dim Ret As Long
    Dim WindowHandle As Long
    Dim WindowDC As Long
    Dim Punt As POINTAPI
    Dim Rechthoekje As RECT
    
    Dim VrolijkKleurtje As Long
    Dim Rood As Long, Groen As Long, Blauw As Long
    
    Call GetCursorPos(Punt)
    WindowHandle = WindowFromPoint(Punt.x, Punt.y)
    WindowDC = GetDC(WindowHandle)
    
    Call GetWindowRect(WindowHandle, Rechthoekje)
    
    
    VrolijkKleurtje = GetPixel(WindowDC, Punt.x - Rechthoekje.Left, Punt.y - Rechthoekje.Top)
    
    Rood = VrolijkKleurtje And &HFF
    Groen = (VrolijkKleurtje And &HFF00&) \ &H100&
    Blauw = (VrolijkKleurtje And &HFF0000) \ &H10000
    
    Me.Caption = Rood & " " & Groen & " " & Blauw
    
    DoEvents
End Sub

[ Voor 5% gewijzigd door RayNbow op 09-05-2003 13:12 ]

Ipsa Scientia Potestas Est
NNID: ShinNoNoir


  • Rhapsody
  • Registratie: Oktober 2002
  • Laatst online: 23:15

Rhapsody

In Metal We Trust

Topicstarter
hartstikke bedankt, heb hier gelukkig ook vb6.0

kun je mij nog even uitleggen waar DoEvents nu eigenlijk voor is?

en hoe kan ik nu die rood, groen en blauw converteren naar system.drawing.color?

🇪🇺 pro Europa!


  • RayNbow
  • Registratie: Maart 2003
  • Laatst online: 01:20

RayNbow

Kirika <3

DoEvents zorgt ervoor dat m'n programmaatje niet hangt in het geval dat m'n timer al m'n processorkracht gebruikt ;)

Over System.Drawing.Color...

code:
1
System.Drawing.Color.FromArgb(CType(Rood, Byte), CType(Groen, Byte), CType(Blauw, Byte))

Bronnen: http://msdn.microsoft.com...rawingcolorclasstopic.asp, http://www.vbwm.com/forums/topic.asp?TOPIC_ID=1947

EDIT: Heh, achteraf gezien kon ik DoEvents achterwege laten... DoEvents is eigenlijk alleen noodzakelijk als je ellenlange lussen hebt (in de orde van tienduizend keer doorlopen).

[ Voor 19% gewijzigd door RayNbow op 09-05-2003 13:06 ]

Ipsa Scientia Potestas Est
NNID: ShinNoNoir


  • Rhapsody
  • Registratie: Oktober 2002
  • Laatst online: 23:15

Rhapsody

In Metal We Trust

Topicstarter
ik zat te klooien met RGB() en HEX():S

Alleen krijg ik nu nog een fatal error als ik het uitprobeer in vb.net:

System.NullReferenceException: Object reference not set to an instance of an object.
at Pick_it.Form1.GetWindowRect(Int64 hwnd, Rectangle lpRect)
at Pick_it.Form1.Timer1_Tick(Object sender, EventArgs e) in C:\Documents and
Settings\Mathieu\My Documents\Visual Studio Projects\Pick-it\Form1.vb:line 121
at System.Windows.Forms.Timer.OnTick(EventArgs e)
at System.Windows.Forms.Timer.DebuggableCallback
(IntPtr hWnd, Int32 msg, IntPtr idEvent, IntPtr dwTime)

---------------------
line 121:

GetCursorPos(Punt)
WindowHandle = WindowFromPoint(Punt.x, Punt.y)
WindowDC = GetDC(WindowHandle)

GetWindowRect(WindowHandle, Rechthoekje) ' << regel 121


Kan over die "GetWindowRect" alleen maar vb6 artikelen vinden :S

[ Voor 2% gewijzigd door Rhapsody op 09-05-2003 13:14 . Reden: Typfout ]

🇪🇺 pro Europa!


  • Rhapsody
  • Registratie: Oktober 2002
  • Laatst online: 23:15

Rhapsody

In Metal We Trust

Topicstarter
ff nieuwe post maar omdat ik weer wat verder ben:

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    <StructLayout(LayoutKind.Sequential)> _
     Private Structure POINTAPI
        Dim x As Int32
        Dim y As Int32
    End Structure

    Private Structure RECT
        Dim Left As Long
        Dim Top As Long
        Dim Right As Long
        Dim Bottom As Long
    End Structure

        Dim Ret As Long
        Dim WindowHandle As Long
        Dim WindowDC As Long
        Dim Punt As POINTAPI
        Dim Rechthoekje As RECT

        Dim VrolijkKleurtje As Long
        Dim Rood As Long, Groen As Long, Blauw As Long

        GetCursorPos(Punt)
        WindowHandle = WindowFromPoint(Punt.x, Punt.y)
        WindowDC = GetDC(WindowHandle)

        GetWindowRect(WindowHandle, Rechthoekje)

        VrolijkKleurtje = GetPixel(WindowDC, 
Punt.x - Rechthoekje.Left, 
Punt.y - Rechthoekje.Top)

        Rood = VrolijkKleurtje And &HFF
        Groen = (VrolijkKleurtje And &HFF00&) \ &H100&
        Blauw = (VrolijkKleurtje And &HFF0000) \ &H10000

        Me.BackColor = System.Drawing.Color.FromArgb(
CType(Rood, Byte), CType(Groen, Byte), CType(Blauw, Byte))
        System.Windows.Forms.Application.DoEvents()


is wat ik nu heb, en ik krijg nu deze fout:

"Additional information: Object reference not set to an instance of an object."


Api's heb ik gecontroleerd, alle benodigde zijn gedeclareerd.

[ Voor 3% gewijzigd door Rhapsody op 09-05-2003 15:19 . Reden: Layout ]

🇪🇺 pro Europa!


  • RayNbow
  • Registratie: Maart 2003
  • Laatst online: 01:20

RayNbow

Kirika <3

Ik heb geen ervaring met VB.NET en z'n foutmeldingen. Enig idee welke regel de fout veroorzaakt? :)

Ipsa Scientia Potestas Est
NNID: ShinNoNoir


  • Rhapsody
  • Registratie: Oktober 2002
  • Laatst online: 23:15

Rhapsody

In Metal We Trust

Topicstarter
Ik heb het opgelost,

- heb als eerste me wezenloos gezocht naar een goede manier om de vb6.0 migration wizard weer aan de gang te krijgen. Die heb ik gevonden: http://support.microsoft....spx?scid=KB;EN-US;q320305
- daarna gewoon het vb6.0 project geconverteerd naar een vb.net project en dat verliep zonder problemen.

Dus hartstikke bedankt voor je hulp/code _/-\o_

[ Voor 2% gewijzigd door Rhapsody op 09-05-2003 17:23 . Reden: smilie bestond niet :S ]

🇪🇺 pro Europa!


  • RayNbow
  • Registratie: Maart 2003
  • Laatst online: 01:20

RayNbow

Kirika <3

No problemo :)

Ipsa Scientia Potestas Est
NNID: ShinNoNoir

Pagina: 1