Screenshot method generates black images

前端 未结 4 768
借酒劲吻你
借酒劲吻你 2020-12-07 04:41

After failing to use the control.drawtobitmap in c#, my second option was to take screenshots of the desktop and crop out the desired sections. My hiccup shows up once i sw

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-07 05:06

    Finally this code seems to work even when i have switched users.

    Code to take screen shot of any unsaved Notepad process ("Untitled - Notepad")

        private void Form1_Load(object sender, EventArgs e)
        {
            //loop for debugging
            int c = 0;
            while(true)
            {
                c++;
                System.Drawing.Bitmap image = CaptureWindow(FindWindow(null, "Untitled - Notepad"));
                image.Save(".\\picture"+c.ToString()+".jpg");
                Thread.Sleep(5000);
            }
    
        }
    
        [DllImport("user32.dll")]
        public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, uint nFlags);
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowDC(IntPtr hWnd);
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
        public System.Drawing.Bitmap CaptureWindow(IntPtr hWnd)
        {
            System.Drawing.Rectangle rctForm = System.Drawing.Rectangle.Empty;
            using (System.Drawing.Graphics grfx = System.Drawing.Graphics.FromHdc(GetWindowDC(hWnd)))
            {
                rctForm = System.Drawing.Rectangle.Round(grfx.VisibleClipBounds);
            }
            System.Drawing.Bitmap pImage = new System.Drawing.Bitmap(rctForm.Width, rctForm.Height);
            System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(pImage);
            IntPtr hDC = graphics.GetHdc();
    
            try
            {
                PrintWindow(hWnd, hDC, (uint)0);
            }
            finally
            {
                graphics.ReleaseHdc(hDC);
            }
            return pImage;
        }
    

    Note that the window may be hidden but it must still be maximized on the user account to get a complete screen shot.

提交回复
热议问题