0

Здравствуйте, недавно я задал вопрос. Но ответа на него так ине последовало, решил пойти по другому пути, сделать скриншот окна и от туда вырезать капчу и запихать ее в Bitmap.

Вопрос: как сделать скриншот только того окна которое я задам?

1

3 ответа 3

0

скриншот произвольной области экрана:

private void button1_Click(object sender, EventArgs e)
{
    // 100, 100 размер копируемой области
    Bitmap screen = new Bitmap(100, 100);
    using (Graphics g = Graphics.FromImage(screen))
    {
        // 5, 5 - координаты левого верхнего угла копируемой области
        g.CopyFromScreen(5, 5, 00, 0, screen.Size);
        pictureBox1.Image = screen;
    }
}
0
0

Уже нашел ответ, спасибо

Rectangle bounds = this.Bounds;
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
    using (Graphics g = Graphics.FromImage(bitmap))
    {
       g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
    }
    bitmap.Save("test.jpg", ImageFormat.Jpeg);
}

Вот функция для вырезки

public Bitmap Cut(Bitmap bmp, int x1, int y1, int x2, int y2)
{
    var img = bmp;
    int width = x2 - x1 + 1;
    int height = y2 - y1 + 1;
    var result = new Bitmap(width, height);
    for (int i = x1; i <= x2; i++)
        for (int j = y1; j <= y2; j++)
            result.SetPixel(i - x1, j - y1, img.GetPixel(i, j));
    return result;
}

Вот пример использования

Bitmap capthaImg = Cut(bitmap, 32, 279, 162, 329);
2
  • 1
    И чем вам это поможет в контексте того вопроса? Как вы определите положение капчи чтобы вырезать ее? 28 авг 2017 в 11:36
  • Метод Cut просто ужасен. Использовать черепашьи GetPixel и SetPixel для вырезки определенной области изображения... Кто тот жестокий человек, что Вас на это надоумил? 11 янв 2019 в 11:59
0

Делает скриншот неактивного окна по заголовку (даже, если оно свернутое, судя по описанию) :

void Example()
{
    IntPtr hwnd = FindWindow(null, "Example.txt - Notepad2");
    CaptureWindow(hwnd);
}

[DllImport("User32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);

[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr handle, ref Rectangle rect);

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

public void CaptureWindow(IntPtr handle)
{
    // Get the size of the window to capture
    Rectangle rect = new Rectangle();
    GetWindowRect(handle, ref rect);

    // GetWindowRect returns Top/Left and Bottom/Right, so fix it
    rect.Width = rect.Width - rect.X;
    rect.Height = rect.Height - rect.Y;

    // Create a bitmap to draw the capture into
    using (Bitmap bitmap = new Bitmap(rect.Width, rect.Height))
    {
        // Use PrintWindow to draw the window into our bitmap
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            IntPtr hdc = g.GetHdc();
            if (!PrintWindow(handle, hdc, 0))
            {
                int error = Marshal.GetLastWin32Error();
                var exception = new System.ComponentModel.Win32Exception(error);
                Debug.WriteLine("ERROR: " + error + ": " + exception.Message);
                // TODO: Throw the exception?
            }
            g.ReleaseHdc(hdc);
        }

        // Save it as a .png just to demo this
        bitmap.Save("Example.png");
    }
}

Код взят отсюда: https://stackoverflow.com/questions/38572021/screenshot-non-active-external-application

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.