Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > .NET Languages
You last visited: Today at 12:16

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



C# PixelSearch (search screen for pixel)

Discussion on C# PixelSearch (search screen for pixel) within the .NET Languages forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Mar 2009
Posts: 6
Received Thanks: 5
C# PixelSearch (search screen for pixel)

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
ProtoBit is offline  
Thanks
5 Users
Old 08/31/2009, 06:29   #2
 
elite*gold: 0
Join Date: Aug 2009
Posts: 1
Received Thanks: 0
Yo sup. This was a great find to me, and I'd love to use it, but I'm getting around 32 errors.

What namespaces do I need to include?


Thank you!!

Nevermind, got it.
NargalaX is offline  
Old 10/14/2009, 02:15   #3
 
elite*gold: 0
Join Date: Oct 2009
Posts: 1
Received Thanks: 0
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.

Any idea what i'm missing?
Otagtubt is offline  
Old 12/26/2010, 23:32   #4
 
elite*gold: 0
Join Date: Jun 2008
Posts: 36
Received Thanks: 2
Did I need a funcion for Bitmap RegionIn_Bitmap = CaptureScreenRegion(rect); ?
Cant find CaptureScreenRegion();
jonasX is offline  
Old 12/31/2010, 17:36   #5
 
elite*gold: 0
Join Date: Mar 2008
Posts: 204
Received Thanks: 92
Code:
            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
cooletobi is offline  
Thanks
1 User
Old 12/31/2010, 19:14   #6
 
Gertos's Avatar
 
elite*gold: 0
Join Date: Mar 2009
Posts: 404
Received Thanks: 120
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;
        }
Gertos is offline  
Old 04/13/2019, 14:48   #7
 
elite*gold: 0
Join Date: Feb 2015
Posts: 95
Received Thanks: 58
Thanks, but for pixelsearch function in autoitx.dll, there are 2 parameters called shade and step. Any idea of what they are?
InfamousZeus is offline  
Reply


Similar Threads Similar Threads
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



All times are GMT +1. The time now is 12:16.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.