Solved

Getting a pixel's colour

Posted on 2000-02-14
15
249 Views
Last Modified: 2010-05-02
How can I get a pixel's colour in a picture? I have tried to use: picture1.point(x,y), but it seems only to work when my form has the focus.
Is there a way to store the image in a variable and then call a point-like function to get the colour, e.g. using an API-Call?
0
Comment
Question by:Beginner2104
  • 6
  • 5
  • 2
  • +1
15 Comments
 
LVL 9

Expert Comment

by:Ruchi
ID: 2520078
Try this one...

Private Sub Form_Load()
Picture1.MousePointer = vbCrosshair
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Caption = Hex$(Picture1.Point(X, Y))
End Sub
0
 

Author Comment

by:Beginner2104
ID: 2520143
That works... but I can not let the user do it. It must be the program it self and when I try to do that it does not really work using the PictureBox.
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2520182
Can you explain me more about your problem?
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2520827
The problem is that if the form that contains the PictureBox is minimized or covered by another window, the Point method returns -1.  This is also true for the GetPixel() API.
0
 

Author Comment

by:Beginner2104
ID: 2521407
Adjusted points to 70
0
 

Author Comment

by:Beginner2104
ID: 2521408
More about my problem:
1. I capture the screen and places the image in a PictureBox or a variable (If posible)
2. I want to identify certain pixels, see if they are black or white.
It has to be a fast method too.
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 2521868
Erick:

Is this also true for an off-screen DC? (if not, surely this would be a solution?)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Accepted Solution

by:
nutwiss earned 70 total points
ID: 2521877
nope... :)


Use a Picturebox with

autoredraw = true
visible = false

to hold the image - point(x,y) should now work
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 2521893
As far as speed goes, try this:


Results:

Point:    5.0 seconds
GetPixel: 1.8 seconds


'#####################################
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 t As Double
Dim c As Long
Dim x As Long
t = Timer
For x = 1 To 100000
    c = Picture1.Point(10, 10)
Next
MsgBox Timer - t
End Sub

Private Sub Command2_Click()
Dim t As Double
Dim c As Long
Dim mydc As Long
Dim x As Long
mydc = Picture1.hdc
t = Timer
For x = 1 To 100000
    c = GetPixel(mydc, 10, 10)
Next
MsgBox Timer - t
End Sub
'##################################
0
 

Author Comment

by:Beginner2104
ID: 2522127
Point works using autoredraw=true,visible=false but I just get -1 from Getpixel(). Is there a way to get it working?
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 2522861
in the code above, have you checked the value of 'c' in the command2_click sub?

I get the same as the point() result ((=/=-1) ....

I'm using NT4 SP3, but this shouldn't make a difference (I don't think - I haven't got my API reference with me today)
0
 

Author Comment

by:Beginner2104
ID: 2524386
I have checked the values... e.g. when I get 0 (black) from Point, I get 14... from GetPixel. Picture1.autoredraw=true Picture1.visible=false
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2524484
Make sure that you are referencing the same point.  GetPixel() uses pixel coordinates, Point uses the scalemode of the PictureBox.  Set the Scalemode of the picturebox to 3, pixels, and try again.
0
 

Author Comment

by:Beginner2104
ID: 2524530
Thank you both nutwiss and Erick37!! I forgot the last one about scalemode. Now it all works fine!! I'll make a question with some points for your last comment Erick37... what about making it posible to share the points among several answers?
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 2526354
Beginner: If you're doing anything graphical, it should be a matter of course that you set scalemode to 3 - it makes life so much easier...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB6 code to programmatically convert pdf to excel 21 66
VBA error replacing data 6 36
Updates not working for MS Windows 7 12 152
MsgBox 2 47
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now