Saturday, January 6, 2018

How to create website thumbnail from URL in C# .net

Step 1 : Add reference in your web application System.Windows.Forms

Step 2 : Create class WebSiteThumbnail

Step 3 : Add Below code for generate thumbainl

private string url = null;
        private Bitmap bmp = null;
        public Bitmap Image
        {
            get
            {
                return bmp;
            }
        }

        private ManualResetEvent mre = new ManualResetEvent(false);
        private int timeout = 5;
        private int thumbWidth;
        private int thumbHeight;
        private int width;
        private int height;
        private string absolutePath;

        public static Bitmap GetSiteThumbnail(string url, int width, int height, int thumbWidth, int thumbHeight)
        {
            WebSiteThumbnail thumb = new WebSiteThumbnail(url, width, height, thumbWidth, thumbHeight);
            Bitmap b = thumb.GetScreenShot();
            if (b == null)
                b = (Bitmap)System.Drawing.Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("PAB.WebControls.Notavailable.jpg"));
            return b;
        }

        public static Bitmap GetSiteThumbnail(string url, int width, int height, int thumbWidth, int thumbHeight, string absolutePath)
        {
            WebSiteThumbnail thumb = new WebSiteThumbnail(url, width, height, thumbWidth, thumbHeight, absolutePath);
            Bitmap b = thumb.GetScreenShot();
            if (b == null)
                b = (Bitmap)System.Drawing.Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("PAB.WebControls.Notavailable.jpg"));
            return b;
        }

        public WebSiteThumbnail(string url, int width, int height, int thumbWidth, int thumbHeight)
        {
            this.url = url;
            this.width = width;
            this.height = height;
            this.thumbHeight = thumbHeight;
            this.thumbWidth = thumbWidth;
        }

        public WebSiteThumbnail(string url, int width, int height, int thumbWidth, int thumbHeight, string absolutePath)
        {
            this.url = url;
            this.width = width;
            this.height = height;
            this.thumbHeight = thumbHeight;
            this.thumbWidth = thumbWidth;
            this.absolutePath = absolutePath;
        }

        public Bitmap GetScreenShot()
        {
            string fileName = url.Replace("http://", "") + ".jpg";
            fileName = System.Web.HttpUtility.UrlEncode(fileName);
            if (absolutePath != null && File.Exists(absolutePath + fileName))
            {
                bmp = (Bitmap)System.Drawing.Image.FromFile(absolutePath + fileName);
            }
            else
            {
                Thread t = new Thread(new ThreadStart(_GetScreenShot));
                t.SetApartmentState(ApartmentState.STA);
                t.Start();
                mre.WaitOne();
                t.Abort();
            }
            return bmp;
        }

        private void _GetScreenShot()
        {
            WebBrowser webBrowser = new WebBrowser();
            webBrowser.ScrollBarsEnabled = false;
            DateTime time = DateTime.Now;
            webBrowser.Navigate(url);
            webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
            while (true)
            {
                Thread.Sleep(0);
                TimeSpan elapsedTime = DateTime.Now - time;
                if (elapsedTime.Seconds >= timeout)
                {
                    mre.Set();
                }
                Application.DoEvents();
            }
        }

        private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser webBrowser = (WebBrowser)sender;
            webBrowser.ClientSize = new Size(this.width, this.height);
            webBrowser.ScrollBarsEnabled = false;
            bmp = new Bitmap(webBrowser.Bounds.Width, webBrowser.Bounds.Height);
            webBrowser.BringToFront();
            webBrowser.DrawToBitmap(bmp, webBrowser.Bounds);
            Image img = bmp.GetThumbnailImage(thumbWidth, thumbHeight, null, IntPtr.Zero);
            string fileName = url.Replace("http://", "") + ".jpg";
            fileName = System.Web.HttpUtility.UrlEncode(fileName);
            if (absolutePath != null && !File.Exists(absolutePath + fileName))
            {
                img.Save(absolutePath + fileName);
            }
            bmp = (Bitmap)img;
            webBrowser.Dispose();
            if (mre != null)
                mre.Set();
        }

        public void Dispose()
        {
            if (bmp != null) this.bmp.Dispose();
        }

Step 4 : How to use class method in code

string Path = HttpContext.Server.MapPath("YourThumbnailSavePath");

Bitmap b = WebSiteThumbnail.GetSiteThumbnail("http://developer2001.blogspot.com/", 1024, 1024, 1024, 768, Path);

Write above line for generate website thumbnail

Labels : ASP.Net, How to create website thumbnail from URL in C# .net, Thumbnail, Website

No comments :

Post a Comment