Hack Forums
[C#] capture second screen desktop (windows 10) c# - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] capture second screen desktop (windows 10) c# (/showthread.php?tid=6146983)



capture second screen desktop (windows 10) c# - hacker dz - Mar 29, 2021

hello everyone
when i made two desktops on windows 10 and try to capture screen . i succeed to capture the first desktop but the second desktop captured screen became blank?
how to capture second desktop screen?


RE: capture second screen desktop (windows 10) c# - RTKN - Mar 29, 2021

(Mar 29, 2021 04:09 PM)hacker dz Wrote:  hello everyone
when i made two desktops on windows 10 and try to capture screen . i succeed to capture the first desktop but the second desktop captured screen became blank?
how to capture second desktop screen?

what screen capture app are you using , and i think its in the wrong section


RE: capture second screen desktop (windows 10) c# - gigajew - Apr 20, 2021

(Mar 29, 2021 04:09 PM)hacker dz Wrote:  hello everyone
when i made two desktops on windows 10 and try to capture screen . i succeed to capture the first desktop but the second desktop captured screen became blank?
how to capture second desktop screen?

You may have to use some sort of SetDesktop API maybe?


RE: capture second screen desktop (windows 10) c# - Scapula™ - Apr 22, 2021

Not sure i understand your request maybe this what you search.

Code
    public static class ScreenHelper
    {

        [DllImport("gdi32.dll", CharSet = CharSet.Unicode)]
        internal static extern IntPtr CreateDC(string lpszDriver, string lpszDevice, string lpszOutput, IntPtr lpInitData);

        [DllImport("gdi32.dll", EntryPoint = "BitBlt", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool BitBlt([In] IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, [In] IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
       
        [DllImport("gdi32.dll")]
        internal static extern bool DeleteDC([In] IntPtr hdc);

        private const int SRCCOPY = 0x00CC0020;

        public static Bitmap CaptureScreen(int screenNumber)
        {
            Rectangle bounds = GetBounds(screenNumber);
            Bitmap screen = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppPArgb);

            using (Graphics g = Graphics.FromImage(screen))
            {
                IntPtr destDeviceContext = g.GetHdc();
                IntPtr srcDeviceContext = CreateDC("DISPLAY", null, null, IntPtr.Zero);

                BitBlt(destDeviceContext, 0, 0, bounds.Width, bounds.Height, srcDeviceContext, bounds.X,
                    bounds.Y, SRCCOPY);

                DeleteDC(srcDeviceContext);
                g.ReleaseHdc(destDeviceContext);
            }

            return screen;
        }

        public static Rectangle GetBounds(int screenNumber)
        {
            return Screen.AllScreens[screenNumber].Bounds;
        }
    }