CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Threaded View

  1. #1
    Join Date
    Apr 2011

    Snapshot of any window in foreground? Similar to Task Switcher

    Title is self explainatory.
    Here is the code:
            public Bitmap CaptureWindow(IntPtr hWnd)
                Rectangle rctForm = Rectangle.Empty;
                IntPtr test = GetWindowDC(hWnd);
                Graphics grfx = Graphics.FromHdc(test);
                using (grfx)
                    RectangleF test2 = grfx.VisibleClipBounds;
                    rctForm = Rectangle.Round(grfx.VisibleClipBounds);
                Bitmap pImage = new Bitmap(rctForm.Width, rctForm.Height);
                Graphics graphics = Graphics.FromImage(pImage);
                IntPtr hDC = graphics.GetHdc();
                //paint control onto graphics using provided options        
                    PrintWindow(hWnd, hDC, (uint)0);
                return pImage;
    Now... It works for most of the windows but not all. Example: http://snpr.cm/ozRClN.jpg
    Non working: http://snpr.cm/HU2QuR.jpg. Its a Delphi program but i guess it doesnt matter. Same happens to few others.

    The function is called in on Load event of my Form containing PictureBox
     pictureBox1.Image = CaptureWindow(okienko);
    'okienko' is a handle to that window.

    I already tested out few solutions using gdi32.dll and user32.dll functions but without luck.

    1.What i have already discovered is different property of 'Graphics grfx = Graphics.FromHdc(test);'

    2.Working thumbnails has "IsVisibleClipEmpty" property set to false, while blank ones are true values.
    Screenshot: http://snpr.cm/vGytAV.jpg

    3.Function returns in this line each time it crashes:
     rctForm = Rectangle.Round(grfx.VisibleClipBounds);
    Im also curious how did they manage to do it in win7(alt-tab task switcher)? Do you have any clues mighty gurus? What could possibly set the IsVisibleClipEmpty property to true if i can get the handle to the window and see the content with my own eyes? Why is it blind for some windows and then miraculously cured the other second on?
    Last edited by qwqwqw12; October 3rd, 2011 at 08:39 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.