0

I'm trying to render a picture of a webpage within an application. Unfortunately I don't have a Windows Form available to me so solutions such as WebKit.net aren't avaliable as they require the control on a form to then extract an image.

I therefore started looking at wrappers for Berkelium, and only came across berkelium-sharp. It seems to have no documentation to make things difficult, but looking at the code I can't see a way to create a Bitmap (or similar) from the page that is rendered underneath. Does anyone know if it's possible?

I should add that I have to use .NET 2.0 before anyone starts offering alternatives that won't work (although I'm open to those that do!).

Ian
  • 30,720
  • 20
  • 100
  • 179
  • Are you looking for something like - `CopyFromScreen` function ? – Angshuman Agarwal Jun 19 '12 at 08:45
  • @AngshumanAgarwal: Kind of, although the actual item I want to copy from shouldn't be visible and I need more functionality that the WebBrowser control can provide. I've actually contacted the author of berkelium-sharp and I think have a solution that I'm going to post up soon. – Ian Jun 19 '12 at 10:59

1 Answers1

2

It's a little quirky to use initially, but after emailing the author I managed to come up with the following in berkelium-sharp. Posting here in case it's useful for anyone else.

    static void Main(string[] args)
    {                        
        BerkeliumSharp.Init(Path.GetTempPath());
        using (Context context = Context.Create())
        {
            BerkeliumSharp.Update();
            using (Window window = new Window(context))
            {
                string url = "http://www.google.com";
                window.Resize(500, 500);
                HandlePaint(window, @"m:\berkelium.png");
                window.NavigateTo(url);

                // Give the page some time to update
                DateTime now = DateTime.Now;
                while(DateTime.Now.Subtract(now).TotalSeconds < 1)
                    BerkeliumSharp.Update();                   
            }
        }
    }

This then uses the HandlePaint method as included in the source code for the automated tests. I should also add that in the tests there is a handy technique to wait until the page has loaded to remove the hardcoded 1 second delay.

Ian
  • 30,720
  • 20
  • 100
  • 179
  • Could you elaborte more on your last paragraph? I searched the Berkelium source for HandlePaint method as well as reading the test code but I couldn't find it. I'm interested in a reliable way that tells me if a website has finished loading. – marc40000 Jan 03 '13 at 02:47
  • @marc40000: Are you looking at the Berkelium# source code? http://code.google.com/p/berkelium-sharp/ . I ended up not using this by the way, because the author told me that Berkelium was not very stable in its initialization/teardown procedure so it was causing my application to crash. This test class has the HandlePaint method - http://code.google.com/p/berkelium-sharp/source/browse/trunk/AutomatedTests/BasicTests.cs – Ian Jan 03 '13 at 09:05