This is a very similar to Auto-its PixelSearch functions (since that's what i intended to make it)
Basically it creates a bitmap of a region on the screen, converts the image to a 2d array and loops through the array searching for the inputed color.
NOTE: to compile this in a VC# 2008 you will need to allow unsafe code (HOW: Project->Properties->Build->Allow unsafe code)
Code:
public static Point PixelSearch(Rectangle rect, int PixelColor, int Shade_Variation)
{
Color Pixel_Color = Color.FromArgb(PixelColor);
Point Pixel_Coords = new Point(-1, -1);
Bitmap RegionIn_Bitmap = CaptureScreenRegion(rect);
BitmapData RegionIn_BitmapData = RegionIn_Bitmap.LockBits(new Rectangle(0, 0, RegionIn_Bitmap.Width, RegionIn_Bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int[] Formatted_Color = new int[3] { Pixel_Color.B, Pixel_Color.G, Pixel_Color.R }; //bgr
unsafe
{
for (int y = 0; y < RegionIn_BitmapData.Height; y++)
{
byte* row = (byte*)RegionIn_BitmapData.Scan0 + (y * RegionIn_BitmapData.Stride);
for (int x = 0; x < RegionIn_BitmapData.Width; x++)
{
if (row[x * 3] >= (Formatted_Color[0] - Shade_Variation) & row[x * 3] <= (Formatted_Color[0] + Shade_Variation)) //blue
{
if (row[(x * 3) + 1] >= (Formatted_Color[1] - Shade_Variation) & row[(x * 3) + 1] <= (Formatted_Color[1] + Shade_Variation)) //green
{
if (row[(x * 3) + 2] >= (Formatted_Color[2] - Shade_Variation) & row[(x * 3) + 2] <= (Formatted_Color[2] + Shade_Variation)) //red
{
Pixel_Coords = new Point(x + rect.X, y + rect.Y);
goto end;
}
}
}
}
}
}
end:
return Pixel_Coords;
}
private static Bitmap CaptureScreenRegion(Rectangle rect)
{
Bitmap BMP = new Bitmap(rect.Width, rect.Height, PixelFormat.Format24bppRgb);
Graphics GFX = System.Drawing.Graphics.FromImage(BMP);
GFX.CopyFromScreen(rect.X, rect.Y, 0, 0, rect.Size, CopyPixelOperation.SourceCopy);
return BMP;
}
btw: performance wise this is about 3-4 ms slower than Autoits (23ms vs 26ms), so if you REALLY need that much of a difference then you might get improvement if you just call the function from autoitx.dll
big credit to dude at which was very helpful, and some other dude with capture screen region function
I added an overloaded method signature to allow rgb to be passed in as seperate values. It's easier to find r g b with photoshop.
public static Point PixelSearch(Rectangle rect, int r, int b, int g, int Shade_Variation) {
Color Pixel_Color = Color.FromArgb(r, g, b);
However... I can't seem to find the pixel color with the code. I checked the color code by taking a screenshot and importing that into photoshop. from there i got the rgb values right out of photoshop and plugged them in to the search. The snippet IS pulling the data off the screen, and to verify it's in the right section i was saving the bmp to a file. It's the right section, but it wont find the color.
in my last attempt i also added a sliderbar to allow some flux in the shade variation. values allowed in the slider are 0-50 which sets the shade_variation... still fails.
Bitmap bmp = null;
bmp = new Bitmap(xresolution, yresolution);
Graphics graphic = Graphics.FromImage(bmp);
graphic.CopyFromScreen(new Point(placement.rcNormalPosition.X, placement.rcNormalPosition.Y),
new Point(0, 0), new Size(ixresolution, yresolution));
Color color = new Color();
color = bmp.GetPixel(x, y);
color.R; // red color
color.B; // blue color
color.G; // green color
You are using the CopyFromScreen function.
Be carfull with it, from my lasts work with it I know it has a handle-resource-leak.
In my case, I could use the funktion a few 10.000 times and then windows crashed.
( Don't know if they have fixed it in the meantime.)
This funktion should work better:
Code:
public static Bitmap GetBitmap(int x, int y, int dx, int dy)
{
Bitmap screenCopy = new Bitmap(dx, dy);
using (Graphics gdest = Graphics.FromImage(screenCopy))
using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero))
{
IntPtr hSrcDC = gsrc.GetHdc();
IntPtr hDC = gdest.GetHdc();
int retval = BitBlt(hDC, 0, 0, dx, dy, hSrcDC, x, y, (int)CopyPixelOperation.SourceCopy);
gdest.ReleaseHdc();
gsrc.ReleaseHdc();
}
return screenCopy;
}
Problem mit Pixel search Autoit 06/18/2010 - AutoIt - 14 Replies Wie der Titel schon sagt habe ich ein Problem mit der funktion Pixel search
Mein Problem ist das die Maus immer über die Rausgesuchte Pixel Position geht
zb O ist der ausgewählte punkt es kommt aber eher sowas raus als ob ich Ö striche ausgewählt hab
Also aimt immer etwas über der angegebennen position der Code :
und dann hätte ich noch die frage ob man mehrere Pixel Searches in einem Script laufen lassen kann
Zb: Wenn die Lebensanzeige sinkt oder Fast lehr ist das eine Function...
Pixel Search Click 04/11/2010 - AutoIt - 4 Replies Hi,
I am trying to open up Internet Explorer using Pixel Search,
But i keep getting Error.
Here is my script :
It finds the color then double clicks on it,but it cannot find the color,i hope some one can help me, +k for any one who can
pixelsearch: wie finde ich ein pixel????? 07/04/2009 - AutoIt - 6 Replies ich habe einen bot erstellt der mit pixelsearch gehen soll.....
aber ich weiß nicht wie ich die coordinaten für die pixelserachfunktion finde
in der sufu hab ich nichts gefunden
hoffentlich könnt ihr mir helfen
Aoc Bot with Pixel search or how 07/20/2008 - General Gaming Releases - 0 Replies Hi there
sorry for my poor english
I tryed a aoc bot , but i got several problems
the enemys are fare away so i need to walk to them and then hit.
i cannot get the next target cause first the bot need to look for 50m to see is there a enemy
Pixel search 02/23/2008 - Guild Wars - 18 Replies hallihallo
bin immoment dabei einen meiner eigenen bots zu optimieren und wollte ne Pixelsearchfunktion einbauen.
Leider weiß ich nicht genau wie man das macht vorallem mit den farben.
mein ziel:
3 verschiedene punkte im gw fenster(mit: x1;y1;;x2;y2;;x3;y3) sollen geprüft werden ob sie in GRÜN sind.
falls x1;y1 grün ist -> (dollerzeichen)folge = 1
falls x2;y2 grün ist -> (dollerzeichen)folge = 2
falls x3;y3 grün ist -> (dollerzeichen)folge = 3