在线游戏中无法捕获“打印窗口”

时间:2018-11-16 19:54:25

标签: c# dllimport user32

我正在尝试创建一个简单的自动访问程序。 用该代码捕获仅保存灰色图片。 我该怎么办? 正常情况下会捕获不是游戏的其他程序。 是因为安全程序吗? 而且我尝试在该游戏中键入键盘,但根本无法正常工作。

  
      
  • SendKeys.SendWait(“ W”)
  •   
  • SendKeys.Send(“ W”)
  •   
  • InputSimulator
  •   
  • SendInput
  •   
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Drawing.Imaging;

namespace WindowsFormsApp2
{

public partial class Form1 : Form
{
        [System.Runtime.InteropServices.DllImport("User32", EntryPoint = "FindWindow")]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    String AppPlayerName = "LOST ARK (64-bit) v.1.0.1.3";

    [DllImport("user32.dll", SetLastError = true)]

    [return: MarshalAs(UnmanagedType.Bool)]

    static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);



    [DllImport("user32.dll", SetLastError = true)]

    static extern int GetWindowRgn(IntPtr hWnd, IntPtr hRgn);



    [DllImport("gdi32.dll")]

    static extern IntPtr CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);


    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        IntPtr findwindow = FindWindow(null, AppPlayerName);
        if (findwindow != IntPtr.Zero)
        {
            Debug.WriteLine("Found.");
            Debug.WriteLine(findwindow.ToString());
            PrintWindow(findwindow);
        }
        else
        {
            Debug.WriteLine("Not Found");
        }
    }

    public static void PrintWindow(IntPtr hwnd)

    {

        Rectangle rc = Rectangle.Empty;

        Graphics gfxWin = Graphics.FromHwnd(hwnd);

        rc = Rectangle.Round(gfxWin.VisibleClipBounds);

        Bitmap bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb);

        Graphics gfxBmp = Graphics.FromImage(bmp);

        IntPtr hdcBitmap = gfxBmp.GetHdc();

        bool succeeded = PrintWindow(hwnd, hdcBitmap, 1);

        gfxBmp.ReleaseHdc(hdcBitmap);

        if (!succeeded)

        {

            gfxBmp.FillRectangle(

                new SolidBrush(Color.Gray),

                new Rectangle(Point.Empty, bmp.Size));

        }

        IntPtr hRgn = CreateRectRgn(0, 0, 0, 0);

        GetWindowRgn(hwnd, hRgn);

        Region region = Region.FromHrgn(hRgn);

        if (!region.IsEmpty(gfxBmp))

        {

            gfxBmp.ExcludeClip(region);

            gfxBmp.Clear(Color.Transparent);

        }

        gfxBmp.Dispose();

        bmp.Save(Application.StartupPath + "1.bmp", System.Drawing.Imaging.ImageFormat.Bmp);

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
}

}

1 个答案:

答案 0 :(得分:1)

游戏不使用GDI来显示内容。 您的程序将创建一个GDI对象(图形)并从中复制内容。但是游戏和3d编辑器都使用DirectX,OpenGL或任何其他直接访问视频的方式。他们绕过了GDI级别。

在过去,它被称为剧院模式。

当游戏使用DirectX时,请使用本文:

https://social.msdn.microsoft.com/Forums/vstudio/ru-RU/c582109f-6b6f-4d5a-9b47-720145d21411/capture-screenshots-of-fullscreen-directx-games-in-c?forum=Offtopic