0

I have tried this code

    public System.Drawing.Bitmap CaptureWebPage(string URL)
    {
        // create a hidden web browser, which will navigate to the page
        System.Windows.Forms.WebBrowser web = new System.Windows.Forms.WebBrowser();
        // we don't want scrollbars on our image
        web.ScrollBarsEnabled = false;
        // don't let any errors shine through
        web.ScriptErrorsSuppressed = true;
        // let's load up that page!
        web.Navigate(URL);

        // wait until the page is fully loaded
        while (web.ReadyState != WebBrowserReadyState.Complete)
            System.Windows.Forms.Application.DoEvents();
        System.Threading.Thread.Sleep(1500); // allow time for page scripts to update
        // the appearance of the page

        // set the size of our web browser to be the same size as the page
        int width = web.Document.Body.ScrollRectangle.Width;
        int height = web.Document.Body.ScrollRectangle.Height;
        web.Width = width;
        web.Height = height;
        // a bitmap that we will draw to
        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height);
        // draw the web browser to the bitmap
        web.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, width, height));

        return bmp; // return the bitmap for processing
    }
    protected void btnConvert_Click(object sender, EventArgs e)
    {
        Bitmap bitmap = new Bitmap(CaptureWebPage(txtUrl.Text));
        Response.ContentType = "image/jpeg";
        bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
        bitmap.Dispose();
        bitmap.Dispose();
        Response.End();
    }

I am able to capture google.com,but not able to capture images of our web site since it contains form authentication so it redirects me to login page and login page is captured.Please help to pass through authentication.Can we use sessions,cookies or anything with the WebBrowser to valid it Or something.Please suggest.

1 Answers1

0

Try on the intended page on your website :

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        System.IO.StringWriter sw = new System.IO.StringWriter(sb);
        Page.RenderControl(new HtmlTextWriter(sw));

and then use the HTML in the string builder to render the page like the example below (Reference : Convert HTML string to image) :

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

    class Program
    {
        static void Main(string[] args)
        {
            var source =  @"
            <!DOCTYPE html>
            <html>
                <body>
                    <p>An image from W3Schools:</p>
                    <img 
                        src=""http://www.w3schools.com/images/w3schools_green.jpg"" 
                        alt=""W3Schools.com"" 
                        width=""104"" 
                        height=""142"">
                </body>
            </html>";
            StartBrowser(source);
            Console.ReadLine();
        }

        private static void StartBrowser(string source)
        {
            var th = new Thread(() =>
            {
                var webBrowser = new WebBrowser();
                webBrowser.ScrollBarsEnabled = false;
                webBrowser.DocumentCompleted +=
                    webBrowser_DocumentCompleted;
                webBrowser.DocumentText = source;
                Application.Run();
            });
            th.SetApartmentState(ApartmentState.STA);
            th.Start();
        }

        static void 
            webBrowser_DocumentCompleted(
            object sender, 
            WebBrowserDocumentCompletedEventArgs e)
        {
            var webBrowser = (WebBrowser)sender;
            using (Bitmap bitmap = 
                new Bitmap(
                    webBrowser.Width, 
                    webBrowser.Height))
            {
                webBrowser
                    .DrawToBitmap(
                    bitmap, 
                    new System.Drawing
                        .Rectangle(0, 0, bitmap.Width, bitmap.Height));
                bitmap.Save(@"filename.jpg", 
                    System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }
    }
Community
  • 1
  • 1
sm_
  • 2,572
  • 2
  • 17
  • 34