Solved

to take a screenshot of a web page using an hidden window?

Posted on 2009-04-03
6
687 Views
Last Modified: 2013-12-17
In a C++ desktop application, is it possible to load a web page with the IE rendering engine in a hidden window and take a screenshot of that hidden page? if so, how?
0
Comment
Question by:lucavilla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 24060918
I needed to do something very similar (was using an embedded Mozilla browser in my app). I never found a way to accomplish what you're asking, I had to render the page to the screen and then take a screenshot of the area.

In case you end up having to go that route, I attached the code I used. In the attached code, "this" refers to the web browser control itself (we created a custom control to encapsulate this, and other, functionality).
/// <summary>
/// Returns an image of the rendered item as a Bitmap object.
/// </summary>
/// <remarks>
/// It is up to the caller of this method to dispose of the Bitmap
/// object returned when the caller is finished with it.
/// </remarks>
/// <returns>
/// A Bitmap object containing the rendering of the current item.
/// </returns>
public Bitmap GetItemAsBitmap()
{
    // NOTE: You cannot wrap the Bitmap declaration in a using 
    //       statement as that causes the Bitmap object to be 
    //       disposed before the caller receives it. It is up to
    //       the caller to dispose of the Bitmap.
 
    // Get a reference to the drawing surface of the control
    using( Graphics source = this.CreateGraphics() )
    {
        // Create a bitmap that is the same width, height, and
        // resolution as the control
        Bitmap bitmap = new Bitmap( this.Width, this.Height, source );
 
        // Create a graphics object on the blank bitmap so we can
        // draw into it
        using( Graphics target = Graphics.FromImage( bitmap ) )
        {
            // Get the screen coordinates of this control; we are looking
            // for a point relative to the top left of the control (this)
            // offset by x = 0 and y = 0 (Point.Empty)
            Point screen = this.PointToScreen( Point.Empty );
 
            // Copy from the screen into the bitmap (via the Graphics
            // object)
            target.CopyFromScreen( screen.X, screen.Y, 0, 0, this.Size );
 
            return bitmap;
        }
    }
}

Open in new window

0
 

Author Comment

by:lucavilla
ID: 24062627
Thanks CraigWagner for your very kind answer with code.

I keep this question open for a solution with the window hidden, if such a solution exists.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 500 total points
ID: 24163749
I, too have explored the issue of making a program render to an off-screen bitmap, and had at best mixed results.  One try is to send the window a WM_PRINTCLIENT message:
    WM_PRINTCLIENT Message
    http://msdn.microsoft.com/en-us/library/dd145217(VS.85).aspx
But  not all windows handle that message, and anyway, if it is not your own application (e.g., an external webbrowser) then there are serious problems in setting up the DC in that app's address space, etc.
See these EE links:
http://www.experts-exchange.com/Programming/System/Windows__Programming/MFC/Q_20780945.html
http://www.experts-exchange.com/Programming/Languages/CPP/Q_20309208.html
Your best bet would be to display the window visibly (breifly) and take the snapshot, then hide it again.
Assuming the window must remain hidden, your next best bet would be to try embedding a webbrowser control into your own app and sending the WM_PRINTCLIENT message to it.  But I'm not at all certain how successful that will be.
Outside of the box:
You might add a secondary video adapter (withh no video monitor) and have the window display on that one -- it would be hidden from the user's view but still provide a bitmap surface for rendering
0
 

Author Closing Comment

by:lucavilla
ID: 31566266
Better than nothing.  Thanks!
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question