Click here to Skip to main content
16,016,789 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a source code for generating the snapshot of a webpage. The code is containing the WebBrowser class which is under the namespace System.Windows.Forms;

When i debug the code, it shows System.Windows.Forms as error in ASP.NET webpage.
What to do?

Here is the code what i found on web.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;
namespace WebSiteSnapshotThumbnails
{
    public class WebSiteSnapshot
    {
        #region Private Properties
        private String Url { get; set; }
        private Int32 BrowserWidth { get; set; }
        private Int32 BrowserHeight { get; set; }
        private Bitmap Bitmap { get; set; }
        #endregion

        #region Constructors
        
        public WebSiteSnapshot(string url, int browserWidth, int browserHeight)
        {
            this.Url = url;
            this.BrowserWidth = browserWidth;
            this.BrowserHeight = browserHeight;
        } 
        #endregion
        #region Public Methods
        
        public Bitmap GenerateWebSiteImage()
        {
            Thread m_thread = new Thread(new ThreadStart(_GenerateWebSiteImage));
            m_thread.SetApartmentState(ApartmentState.STA);
            m_thread.Start();
            m_thread.Join();
            return Bitmap;
        } 
        #endregion
        #region Private Methods
        private void _GenerateWebSiteImage()
        {
            WebBrowser m_WebBrowser = new WebBrowser();
            m_WebBrowser.ScrollBarsEnabled = false;
            m_WebBrowser.Navigate(Url);
            m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
            while (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete)
                Application.DoEvents();
            m_WebBrowser.Dispose();
        }
        private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser m_WebBrowser = (WebBrowser)sender;
            m_WebBrowser.ClientSize = new Size(this.BrowserWidth, this.BrowserHeight);
            m_WebBrowser.ScrollBarsEnabled = false;
            Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height);
            m_WebBrowser.BringToFront();
            m_WebBrowser.DrawToBitmap(Bitmap, m_WebBrowser.Bounds);
        } 
        #endregion
    }
}
Posted
Updated 27-Jul-11 0:01am
v3

First, you have to add a reference to the appropriate assembly.

Second, doing this for a control is fairly pointless because you can't actually use any of the controls in the Windows.Forms assembly on a web page.
 
Share this answer
 
Leave alone making it work, you are missing the basics about windows application and web application. I would rather suggest you to first understand how a web application works.
 
Share this answer
 
Nothing in System.Windows is intended to be used in a Web application.
System.Windows.Forms are client controls. However, web applications run on the server side, and have their own UI elements in System.Web.UI.
 
Share this answer
 
v2
I think best solution is to use your code by the help of silverlight
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900