C # захват изображения из DirectX и OpenGL дает черный экран - PullRequest
1 голос
/ 25 октября 2019

Я ищу решение, как сделать скриншот экрана, как это делает Fraps. Я уже попробовал 10 методов в C #, и ни один из них не может сделать экран. Если я нажимаю на экран печати и вставляю его в боль, я вижу только целую картинку, я не вижу выбранное окно игры. Это все черное. Я вчера искал 10 часов, но не смог найти решение. У всего в Интернете разные методы в C ++ и т. Д. Есть ли кто-нибудь, кто сталкивался с подобной проблемой и решил ее в C #?

Я использовал такие методы, как:

           #region WinApi
    [DllImport("User32.dll")]
    static extern int SetForegroundWindow(IntPtr point);

    [DllImport("kernel32.dll")]
    public static extern bool ReadProcessMemory(IntPtr hProcess, int lpBaseAddress, byte[] buffer, int size,
        int lpNumberOfBytesRead);

    [DllImport("kernel32.dll")]
    public static extern bool WriteProcessMemory(IntPtr hProcess, int lpBaseAddress, byte[] buffer, int size,
        int lpNumberOfBytesWritten);

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

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern bool SetWindowText(IntPtr hwnd, String lpString);

    [DllImport("user32.dll", SetLastError = true)]
    static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy);

    [DllImport("user32.dll", SetLastError = true)]
    internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

    [DllImport("user32.dll")]
    static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

    [DllImport("User32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

    [DllImport("gdi32.dll")]
    static extern IntPtr CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);

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

    [DllImport("user32.dll")]
    public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);

    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll", SetLastError = true)]
    static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int Left;        // x position of upper-left corner
        public int Top;         // y position of upper-left corner
        public int Right;       // x position of lower-right corner
        public int Bottom;      // y position of lower-right corner
    }


    #endregionn

public Bitmap GetScreenshot_hwnd()
        {
            IntPtr hwnd = FindWindow(null,"MyGame");//handle here

            RECT rc;
            GetWindowRect(hwnd, out rc);
            int width = rc.Right - rc.Left;
            int height = rc.Bottom - rc.Top;
            Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            Graphics gfxBmp = Graphics.FromImage(bmp);
            IntPtr hdcBitmap = gfxBmp.GetHdc();

            PrintWindow(hwnd, hdcBitmap, 0);

            gfxBmp.ReleaseHdc(hdcBitmap);
            gfxBmp.Dispose();

            Bitmap source = bmp;

            pictureBox1.Image = source;
            return bmp;
        }

или:

 public Bitmap zrob_screen_calego_ekranu_settings()
    {



        // Shot size = screen size
        Size shotSize = Screen.PrimaryScreen.WorkingArea.Size;

        // the upper left point in the screen to start shot
        // 0,0 to get the shot from upper left point
        int x = Convert.ToInt32(0);
        int y = Convert.ToInt32(0);
        Point upperScreenPoint = new Point(x, y);

        // the upper left point in the image to put the shot
        Point upperDestinationPoint = new Point(0, 0);

        int q = Convert.ToInt32(shotSize.Width) - x;
        int w = Convert.ToInt32(shotSize.Height) - y;
        // create image to get the shot in it
        Bitmap shot;
        shot = new Bitmap(q, w, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

        // new Graphics instance 
        Graphics graphics = Graphics.FromImage(shot);

        // get the shot by Graphics class 
        graphics.CopyFromScreen(upperScreenPoint, upperDestinationPoint, shotSize);
        return shot;

    }

Но оба дают только черный экран. Я проверил еще 8 методов и столько же. Если кто-то может помочь с этим, я могу заплатить.

...