|
-
May 3rd, 2013, 12:21 PM
#1
Thread Starter
New Member
How to use Getpixel
I have a program where i use getpixel to see what color a specific point is, and if it's the correct color a messagebox will pop up. For some reason, when i watch the program, every time it checks, the getpixel name (eg. ff008000) changes about every time even when the point doesn't change. Why does it do this? Do i need to make the background like a full blue or red with no other colors in it to work?
-
May 3rd, 2013, 01:11 PM
#2
Re: How to use Getpixel
Theoretically it shouldn't, which means that something else is causing that pixel color to change. Show us how you're using the GetPixel, I have a feeling it has to do with the way you're checking the coordinates or perhaps it could lie the bitmap itself. But when you say 'every time it checks' are you talking about setting up a breakpoint or are you displaying it in a MessageBox? If its the latter, than switch to the former that away you can get all the details when you call GetPixel.
-
May 3rd, 2013, 01:24 PM
#3
Re: How to use Getpixel
If its the latter, than switch to the former
Errm ... won't that change the display and therefore in all likelihood the very pixel that you're supposedly tracking?
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 3rd, 2013, 01:30 PM
#4
Re: How to use Getpixel
 Originally Posted by dunfiddlin
Errm ... won't that change the display and therefore in all likelihood the very pixel that you're supposedly tracking?
Switching to a breakpoint? No. Well atleast it shouldn't, I didn't test it, but it shouldn't.
-
May 3rd, 2013, 03:04 PM
#5
Thread Starter
New Member
Re: How to use Getpixel
This all inside a timertick. Sorry about the format of this all... Idk how to put it in the right thing or whatever
Dim bmp As New Bitmap("U:/land.png")
Dim g As Graphics = Graphics.FromImage(bmp)
g.CopyFromScreen(MousePosition, Point.Empty, bmp.Size)
Dim pixelcolor As Color = bmp.GetPixel(x, y)
If pixelcolor.Name = "ff008000" Then
Me.Timer1.Stop()
MessageBox.Show("Whatevers")
Exit For
End If
-
May 3rd, 2013, 03:14 PM
#6
Re: How to use Getpixel
Get rid of the MessageBox and use a breakpoint for that. You want the breakpoint probably at about the If statement. That will let you see all the relevant values.
There are a couple things taht seem unlikely to be right about that, and one thing that is less than ideal, but won't impact things too badly. The first issue is the MousePosition. That may work, depending on what you are doing, but it also might be in the wrong coordinates. You'd want to see what coordinates you get in there and whether that looks right. That may be hard to do unless you try this somewhere in a corner where one axis will be really small. The upper left corner would be ideal, as you would be able to see whether it is sufficiently close to 0,0.
The really weird thing is that you are taking x,y from the bmp. What is the x,y? Where did those come from, and why do you deal with the mouse position if you are seeking a hardcoded x,y in a bitmap. It doesn't seem likely that the image has to be the same if it is a copy of some random place on the screen.
Additionally, checking the color name isn't so great. GetPixel gives you a color that has an ARGB value. Compare those, or turn it into a UInt and compare that rather than the slower string comparison.
My usual boring signature: Nothing
 
-
May 3rd, 2013, 03:16 PM
#7
Re: How to use Getpixel
I'd like to mention that the pixelcolor.Name will never equal ff008000. Here is a list of all of the images and their names, so your timer would never stop. You should also not be declaring a new instance of a bitmap every time the timer ticks, instead declare a bitmap variable at the form level set the bitmap outside of the timer's tick event(like when you call Timer.Start). But have you set up a breakpoint to see if the details of the bmp.GetPixel change?
Edit - you're also looking for the code tags. To use them put your code in tags like this:
{code}
'Code Here
{/code}
Just replace the curly brackets with square brackets so the code comes out formatted:
-
May 3rd, 2013, 03:19 PM
#8
Thread Starter
New Member
Re: How to use Getpixel
The X and Y is actually the x and y coordinates of an animated ball. I'm using it to see if it touches the walls in a maze, and i made the background in paint. You move the ball with W,A,S, and D.
Also- dday- But what doesn't make sense then is why it does pop up. And i have watched it with a breakpoint and such and the name has been ff008000. And other random numbers and letters
-
May 3rd, 2013, 03:21 PM
#9
Re: How to use Getpixel
 Originally Posted by savage110
The X and Y is actually the x and y coordinates of an animated ball. I'm using it to see if it touches the walls in a maze, and i made the background in paint. You move the ball with W,A,S, and D.
If you're trying to check the collision between two objects, don't use .GetPixel. Instead use .IntersectsWith. If you take a look at my game 'bouncy ball' in my signature it resembles brick breaker. In that game I show how to deal with collision detection.
-
May 7th, 2013, 09:51 AM
#10
Thread Starter
New Member
Re: How to use Getpixel
 Originally Posted by dday9
If you're trying to check the collision between two objects, don't use .GetPixel. Instead use .IntersectsWith. If you take a look at my game 'bouncy ball' in my signature it resembles brick breaker. In that game I show how to deal with collision detection.
ok I think it's all working now. Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|