2 Replies - 3472 Views - Last Post: 26 February 2010 - 01:59 AM Rate Topic: -----

#1 Guest_Coder*


Reputation:

Pixel Search In C# Almost Have It

Posted 25 February 2010 - 06:17 PM

okay I did take bits of this code and I almost have it working

My window form has a click button on it with of course the search for pixel code in it but I cant get it to work. Please Help

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {            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;
        }
        }
    }
}

 


Now that is all of my code and here are the errors im getting

Error 1 } expected C:\Documents and Settings\Owner\Local Settings\Application Data\Temporary Projects\WindowsFormsApplication1\Form1.cs 30 22 WindowsFormsApplication1

Error 2 Type or namespace definition, or end-of-file expected C:\Documents and Settings\Owner\Local Settings\Application Data\Temporary Projects\WindowsFormsApplication1\Form1.cs 76 1 WindowsFormsApplication1


Now how do I fix that i now im close but cant quite get that last peice to work please help thank you

Is This A Good Question/Topic? 0

Replies To: Pixel Search In C# Almost Have It

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6258
  • View blog
  • Posts: 24,026
  • Joined: 23-August 08

Re: Pixel Search In C# Almost Have It

Posted 25 February 2010 - 06:21 PM

Your braces are all over the place. Double check them all.
Was This Post Helpful? 0
  • +
  • -

#3 Adkins   User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: Pixel Search In C# Almost Have It

Posted 26 February 2010 - 01:59 AM

In a case such as this I find it helpful to label all the ending braces with a comment containing the code that starts that block. eg:
if (conditional)
{
   // code to execute...
} // if (conditional)



do that for all of them and you will be able to see who doesn't belong or who is missing a brace.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1