Example #1
0
        public static Bitmap TakeClientPicture(IntPtr handle = default(IntPtr))
        {
            if (handle == default(IntPtr))
            {
                handle = GetForegroundWindow();
            }
            Rectangle rect = WindowWrapper.GetClientArea(handle);

            return(WindowWrapper.ScreenCapture(rect));
        }
Example #2
0
        public static Bitmap TakeClientPicture(IntPtr handle, Rectangle area)
        {
            Rectangle rect = ClientToScreenRect(handle, area);
            Bitmap    bmp  = WindowWrapper.ScreenCapture(rect);

            if (bmp == null)
            {
                return(null);
            }

            return(bmp);
        }
        private void ProcessGlobalKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.Control && e.Shift && e.Alt)
            {
                switch (e.KeyCode)
                {
                // Shortcuts for changing bot settings.
                case Keys.F6:
                    this.chkEnableConveienceKeys.IsChecked ^= true;
                    break;

                // Shortcuts for setting the bounds of the region.
                case Keys.I:
                {
                    System.Drawing.Point pt = System.Windows.Forms.Cursor.Position;
                    SetTop(pt.Y);
                }
                break;

                case Keys.J:
                {
                    System.Drawing.Point pt = System.Windows.Forms.Cursor.Position;
                    SetLeft(pt.X);
                }
                break;

                case Keys.K:
                {
                    System.Drawing.Point pt = System.Windows.Forms.Cursor.Position;
                    SetRight(pt.X);
                }
                break;

                case Keys.M:
                {
                    System.Drawing.Point pt = System.Windows.Forms.Cursor.Position;
                    SetBottom(pt.Y);
                }
                break;

                // Shortcuts for defininig the top-left and bottom-right points of the region.
                case Keys.U:
                {
                    System.Drawing.Point pt = System.Windows.Forms.Cursor.Position;
                    SetLeft(pt.X);
                    SetTop(pt.Y);
                }
                break;

                case Keys.Oemcomma:
                {
                    System.Drawing.Point pt = System.Windows.Forms.Cursor.Position;
                    SetRight(pt.X);
                    SetBottom(pt.Y);
                }
                break;

                // Debugging shortcuts
                case Keys.L:
                {
                    System.Drawing.Point     pt   = System.Windows.Forms.Cursor.Position;
                    System.Drawing.Rectangle rect = new System.Drawing.Rectangle(pt.X, pt.Y, 1, 1);
                    using (System.Drawing.Bitmap bmp = WindowWrapper.ScreenCapture(rect))
                    {
                        using (Bitmap24 bmp24 = Bitmap24.FromImage(bmp))
                        {
                            bmp24.Lock();
                            int[]  px  = bmp24.GetPixel(0, 0);
                            double lum = Bitmap24.CalculateLuminance(px);
                            Console.WriteLine("({0}, {1}) -> ({1}) -> {2}", pt.X, pt.Y, string.Join(", ", px), lum);
                        }
                    }
                }
                break;

                case Keys.O:
                {
                    System.Drawing.Rectangle bounds = new System.Drawing.Rectangle(
                        _left,
                        _top,
                        _right - _left + 1,
                        _bottom - _top + 1
                        );
                    using (System.Drawing.Bitmap bmp = WindowWrapper.ScreenCapture(bounds))
                    {
                        bmp.Save(@"tmp.bmp");
                    }
                }
                break;
                }
            }
        }
        private void btnShrinkToFit_Click(object sender, RoutedEventArgs e)
        {
            // Create a Rectangle defining the region.
            System.Drawing.Rectangle bounds = new System.Drawing.Rectangle(
                _left - 1,
                _top - 1,
                _right - _left + 3,
                _bottom - _top + 3
                );

            // Get the width and height of the region.
            int width  = bounds.Width;
            int height = bounds.Height;

            // Quit if the width and the height are too small.
            if (width < 20 || height < 20)
            {
                MessageBox.Show("The defined region is too small.  Please update the region and try again.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            // Get a screenshot of the defined region to process.
            using (System.Drawing.Bitmap bmp = WindowWrapper.ScreenCapture(bounds))
            {
                using (Bitmap24 bmp24 = Bitmap24.FromImage(bmp))
                {
                    // Lock the bits in the Bitmap24 to directly access the raw data.
                    bmp24.Lock();

                    // Compute how much to shift the left coordinate by.
                    int leftShift = -1;
                    int leftDark  = 0;
                    for (int x = 0; x < width; ++x)
                    {
                        System.Drawing.Rectangle rect = new System.Drawing.Rectangle(x, (height / 2) - CHECK_AROUND, 1, CHECK_SIZE);
                        int currDark = CountDark(bmp24, rect);
                        if (leftDark == CHECK_SIZE && currDark == 0)
                        {
                            leftShift = x - 1;
                            break;
                        }
                        leftDark = currDark;
                    }

                    // Compute how much to shift the right coordinate by.
                    int rightShift = -1;
                    int rightDark  = 0;
                    for (int x = width - 1; x >= 0; --x)
                    {
                        System.Drawing.Rectangle rect = new System.Drawing.Rectangle(x, (height / 2) - CHECK_AROUND, 1, CHECK_SIZE);
                        int currDark = CountDark(bmp24, rect);
                        if (rightDark == CHECK_SIZE && currDark == 0)
                        {
                            rightShift = width - x - 2;
                            break;
                        }
                        rightDark = currDark;
                    }

                    // Compute how much to shift the top coordinate by.
                    int topShift = -1;
                    int topDark  = 0;
                    for (int y = 0; y < height; ++y)
                    {
                        System.Drawing.Rectangle rect = new System.Drawing.Rectangle((width / 2) - CHECK_AROUND, y, CHECK_SIZE, 1);
                        int currDark = CountDark(bmp24, rect);
                        if (topDark == CHECK_SIZE && currDark == 0)
                        {
                            topShift = y - 1;
                            break;
                        }
                        topDark = currDark;
                    }

                    // Compute how much to shift the bottom coordinate by.
                    int bottomShift = -1;
                    int bottomDark  = 0;
                    for (int y = height - 1; y >= 0; --y)
                    {
                        System.Drawing.Rectangle rect = new System.Drawing.Rectangle((width / 2) - CHECK_AROUND, y, CHECK_SIZE, 1);
                        int currDark = CountDark(bmp24, rect);
                        if (bottomDark == CHECK_SIZE && currDark == 0)
                        {
                            bottomShift = height - y - 2;
                            break;
                        }
                        bottomDark = currDark;
                    }

                    // Define a list containing the ones that were not found.
                    List <string> notFound = new List <string>();

                    // Check if a valid left shift was found.
                    if (leftShift > 0)
                    {
                        SetLeft(_left + leftShift);
                    }
                    else if (leftShift < 0)
                    {
                        notFound.Add("Left");
                    }

                    // Check if a valid right shift was found.
                    if (rightShift > 0)
                    {
                        SetRight(_right - rightShift);
                    }
                    else if (rightShift < 0)
                    {
                        notFound.Add("Right");
                    }

                    // Check if a valid top shift was found.
                    if (topShift > 0)
                    {
                        SetTop(_top + topShift);
                    }
                    else if (topShift < 0)
                    {
                        notFound.Add("Top");
                    }

                    // Check if a valid bottom shift was found.
                    if (bottomShift > 0)
                    {
                        SetBottom(_bottom - bottomShift);
                    }
                    else if (bottomShift < 0)
                    {
                        notFound.Add("Bottom");
                    }

                    if (notFound.Count > 0)
                    {
                        string errorMessage = "Unable to auto-detect the bounds.  Please make sure that the defined region completely contains the game window, and that the game is displaying a bright image, such as the main screen.\n\nThe following dimensions were not found:\n\n" + string.Join("\n", notFound) + "\n\n";
                        MessageBox.Show(errorMessage, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
            }
        }