C# capture image from DirectX and OpenGL gives black screen

Viewed 149

I am looking for a solution how to make a screen screenshot, just like Fraps does. I have already tried 10 methods in C # and none of them can make a screen. If I press print screen and paste it into pain, I can only see the whole picture, I can't see the selected game window. It's all black. I looked for 10 hours yesterday but I couldn't find a solution. Everything on the internet has different methods in C ++ etc. Is there anyone who has had a similar problem and solved it in C #?

I used methods such as:

           #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;
        }

or :

 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;

    }

But both gives only black screen. I tested 8 more methods and the same. If anyone can help with that i can pay.

0 Answers
Related