예제 #1
0
파일: Eyes.cs 프로젝트: ketra/fishingbot
        public void GetBobber()
        {
            Thread.Sleep(2000);
            Bitmap template = new Bitmap(16, 16, PixelFormat.Format24bppRgb);

            try
            {
                Bitmap bmp = Screenshot();
                using (FileStream stream = new FileStream(Properties.Settings.Default.BobberIcon, FileMode.Open, FileAccess.Read))
                {
                    template = (Bitmap)System.Drawing.Image.FromStream(stream);
                }

                const Int32 divisor = 8;
                Bitmap      source  = new ResizeNearestNeighbor(bmp.Width / divisor, bmp.Height / divisor).Apply(bmp);
                Bitmap      test    = new ResizeNearestNeighbor(template.Width / divisor, template.Height / divisor).Apply(template);
                // create template matching algorithm's instance
                // (set similarity threshold to 92.1%)

                float match = Properties.Settings.Default.match;
                //ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.901f);
                ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(match);

                // find all matchings with specified above similarity

                TemplateMatch[] matchings = tm.ProcessImage(source, test);

                // highlight found matchings
                if (matchings.Length > 0)
                {
                    BitmapData data = source.LockBits(
                        new Rectangle(0, 0, source.Width, source.Height),
                        ImageLockMode.ReadWrite, source.PixelFormat);
                    BitmapData dataorg = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
                    foreach (TemplateMatch m in matchings)
                    {
                        int       placex = (int)(((m.Rectangle.X * divisor) * 1.02f) / dpi);
                        int       placey = (int)(((m.Rectangle.Y * divisor) * 1.02f) / dpi);
                        Rectangle rec    = new Rectangle(m.Rectangle.X * divisor, m.Rectangle.Y * divisor, m.Rectangle.Width * divisor, m.Rectangle.Height * divisor);
                        if (MoveMouseAndCheckCursor(placex, placey))
                        {
                            break;
                        }
                        Drawing.Rectangle(dataorg, rec, Color.Red);

                        //MessageBox.Show(m.Rectangle.Location.ToString());
                        // do something else with matching
                    }
                    source.UnlockBits(data);
                    bmp.UnlockBits(dataorg);
                    //bmp.Save(@"D:\temp\Bobbershot.jpg", ImageFormat.Jpeg);
                }
                else
                {
                    throw new Exception("No Bobber Found");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                template.Dispose();
            }
        }