Скриншот программы Winapi

Master4eg

Client
Регистрация
06.04.2016
Сообщения
421
Благодарностей
122
Баллы
43
Всем привет, подскажите, как с помощью Winapi сделать скриншот определенной программы с сохранением файла в папку?
 

avtostopshik

Client
Регистрация
09.09.2016
Сообщения
757
Благодарностей
135
Баллы
43

specialist

Client
Регистрация
28.12.2018
Сообщения
732
Благодарностей
343
Баллы
63
Неужели никто не знает?
В любом поисковике, winapi скриншот окна c#, неужели трудно?

Пример, скриншот окна по заголовку:
Код:
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");
    }
}
 
  • Спасибо
Реакции: Dorian_Gray и avtostopshik

avtostopshik

Client
Регистрация
09.09.2016
Сообщения
757
Благодарностей
135
Баллы
43
В любом поисковике, winapi скриншот окна c#, неужели трудно?

Пример, скриншот окна по заголовку:
Код:
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");
    }
}
Ага, отлично, спасибо! Осталось это под C# код в зеннопостере адаптировать
 

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)