locked
Convert HTML string URL to Image using asp.net C# RRS feed

  • Question

  • User-471420332 posted

    I have one online test website, and I want to capture a screenshot at defined time intervals, automatically, using ASP.NET C# Web Forms. I need other related sample code.

    I had tried one way. Now I am getting a URL, using capture the HTML response from an HTTP request. I want to store the HTML Image in a database table. I'm not getting any ideas regarding this.

    Below is my C# code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Net;
    using System.Text;
    
    
    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
        getimage();
    }
    
    public void getimage()
     {
       WebClient myClient = new WebClient();
        string myPageHTML = null;
        byte[] requestHTML;
        // Gets the url of the page
        string currentPageUrl = Request.Url.ToString();
    
        UTF8Encoding utf8 = new UTF8Encoding();
    
           // by setting currentPageUrl to mypage.aspx it will fetch the source (html) 
        // of the mypage.aspx and put it in the myPageHTML variable. 
    
        currentPageUrl = "http://localhost:49161/login.aspx";
    
        requestHTML = myClient.DownloadData(currentPageUrl);
    
        myPageHTML = utf8.GetString(requestHTML);
    
        Response.Write(myPageHTML);
      }
    }
    Thursday, September 22, 2016 1:41 PM

Answers

  • User347430248 posted

    Hi Mazhar Khan,

    you had mentioned that you want to capture a snapshot.

    but from your code it looks like you are getting sting of the webpage.

    then you had said you want to save it as image in database.

    so here I am not understanding clearly you want to save the string that you get by your code in database.

    or actually you want to take a snapshot of a web page as an image and store that image in the database.

    Here I assume that you want to take a image snapshot of the webpage. like generally people want a proof of transaction or that kind of requirement.

    so here I think that taking a string of the webpage is not suitable because anyone can make changes in it.

    but if have image then it has some less changes to modify it.

    so for that you can do the code something like below.

    protected void Capture(object sender, EventArgs e)
    
    {
    
        string url = txtUrl.Text.Trim();
    
        Thread thread = new Thread(delegate()
    
        {
    
            using (WebBrowser browser = new WebBrowser())
    
            {
    
                browser.ScrollBarsEnabled = false;
    
                browser.AllowNavigation = true;
    
                browser.Navigate(url);
    
                browser.Width = 1024;
    
                browser.Height = 768;
    
                browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
    
                while (browser.ReadyState != WebBrowserReadyState.Complete)
    
                {
    
                    System.Windows.Forms.Application.DoEvents();
    
                }
    
            }
    
        });
    
        thread.SetApartmentState(ApartmentState.STA);
    
        thread.Start();
    
        thread.Join();
    
    }
    
     
    
    private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    
    {
    
        WebBrowser browser = sender as WebBrowser;
    
        using (Bitmap bitmap = new Bitmap(browser.Width, browser.Height))
    
        {
    
            browser.DrawToBitmap(bitmap, new Rectangle(0, 0, browser.Width, browser.Height));
    
            using (MemoryStream stream = new MemoryStream())
    
            {
    
                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
    
                byte[] bytes = stream.ToArray();
    
                imgScreenShot.Visible = true;
    
                imgScreenShot.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes);
    
            }
    
        }
    
    }

    References.

    Capture Screenshot (Snapshot) Image of Website (Web Page) in ASP.Net using C# and VB.Net 

    Capture web page as image using ASP.NET

    C# .NET code sample for snap shot

    Regards

    Deepak

                                                              

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 23, 2016 2:43 AM