Hi!
(This is my first post on the forum)
I'm trying to capture the desktop of the "not showing" desktop.
I got two users, both logged in. I start the program on the first users desktop (to capture), and then the second one on the other user, which is supposed to show the desktop of the first user. And when I try to do this (using a console window and Graphics.CopyFromScreen()) I get an error message saying: Win32Exception was unhandled. The reference (handle) is bad (or something in that style). (My language isn't english so I dosen't get the exceptions in english :icon_razz:.) My code is C#.
If you want me to post any code please tell!

Hi!
I want to capture the screen of a new Desktop created using CreateDesktop(). But everytime I try I get an exception, Win32Exception, The reference (handle) is incorrect. I try to capture using Graphics g = new Graphics();
g.CopyFromScreen();

Any ideas how to overcome this??

Main code

Console.WriteLine("Creating New Desktop...");
            IntPtr desktop = CreateDesktop("LolDesktop", IntPtr.Zero, IntPtr.Zero, 0, AccessRights, IntPtr.Zero);

            Capturer capturer = new Capturer() {
                Images=Images,
                Desktop = desktop
            };
            Thread Capture = new Thread(capturer.DoWork);
            Capture.Start();

This in the main code too.

[DllImport("user32.dll")]
        private static extern IntPtr CreateDesktop(string lpszDesktop,
        IntPtr lpszDevice,
        IntPtr pDevmode,
        int dwFlags,
        int dwDesiredAccess,
        IntPtr lpsa);

        [DllImport("user32.dll")]
        public static extern bool SetThreadDesktop(IntPtr hDesktop);

        [DllImport("user32.dll", EntryPoint = "CloseDesktop", CharSet = CharSet.Unicode, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool CloseDesktop(IntPtr handle);

        public const int DESKTOP_CREATEWINDOW = 0x0002;
        public const int DESKTOP_ENUMERATE = 0x0040;
        public const int DESKTOP_WRITEOBJECTS = 0x0080;
        public const int DESKTOP_SWITCHDESKTOP = 0x0100;
        public const int DESKTOP_CREATEMENU = 0x0004;
        public const int DESKTOP_HOOKCONTROL = 0x0008;
        public const int DESKTOP_READOBJECTS = 0x0001;
        public const int DESKTOP_JOURNALRECORD = 0x0010;
        public const int DESKTOP_JOURNALPLAYBACK = 0x0020;
        public const int AccessRights =
        DESKTOP_CREATEWINDOW |
        DESKTOP_SWITCHDESKTOP;

This is the "capturer".

class Capturer
    {
        public Socket Images;
        public IntPtr Desktop;

        public void DoWork()
        {
            SetThreadDesktop(Desktop);
            while (true)
            {
                Bitmap bmp;
                Graphics gfx;
                bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                gfx = Graphics.FromImage(bmp);
                gfx.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] toSend = ms.ToArray();
                bmp.Dispose();
                ms.Close();
                Images.Send(toSend);
            }
        }

        [DllImport("user32.dll")]
        public static extern bool SetThreadDesktop(IntPtr hDesktop);
    }

Edited 6 Years Ago by Mattan360: n/a

This article has been dead for over six months. Start a new discussion instead.