locked
Convert html to image using asp.net RRS feed

  • Question

  • User-1690110300 posted

    Is there an easy way to convert a html page to an image using ASP.net.  I tried this code but I get this error.  Can anyone tell me why? Or is there a better approach for this?  Thanks a lot

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Threading;
    using System.IO;
    using System.Reflection;
    
    
        public class Page2Image
        {
            private string iUrlText = string.Empty;
            private ManualResetEvent manre = new ManualResetEvent(false);
            private int timeout = 5;
            private Bitmap bmp = null;
            private string _fileName = string.Empty;
            WebBrowser webB11;
    
            public void DrawImage()
            {
                webB11 = new WebBrowser();
                webB11.ScrollBarsEnabled = false;
                DateTime time = DateTime.Now;
                webB11.DocumentText = iUrlText;
    
                webB11.Navigating += new WebBrowserNavigatingEventHandler(webB11_Navigating);
                while (true)
                {
                    Thread.Sleep(0);
                    TimeSpan elapsedTime = DateTime.Now - time;
                    if (elapsedTime.Seconds >= timeout)
                    {
                        manre.Set();
                    }
                    Application.DoEvents();
                    this.webB11_Navigating(webB11, new WebBrowserNavigatingEventArgs(webB11.Url, _fileName));
                }
            }
    
            public Bitmap GetScreenShot(string urlText, string fileName)
            {
                iUrlText = urlText;
                _fileName = fileName;
                Thread t = new Thread(new ThreadStart(DrawImage));
                t.SetApartmentState(ApartmentState.STA);
                t.Start();
                manre.WaitOne();
                t.Abort();
    
                return bmp;
            }
    
            private void webB11_Navigating(object sender, WebBrowserNavigatingEventArgs e)
            {
                WebBrowser webB1 = (WebBrowser)sender;
                webB1.ClientSize = new Size(800, 800);
                webB1.ScrollBarsEnabled = false;
                bmp = new Bitmap(webB1.Bounds.Width, webB1.Bounds.Height);
                webB1.BringToFront();
                webB1.DrawToBitmap(bmp, webB1.Bounds);
                Image img = bmp.GetThumbnailImage(800, 800, null, IntPtr.Zero);
                img.Save(_fileName);
    
            }
        }
    

    Tuesday, April 26, 2011 4:12 PM

Answers

All replies

  • User-2010311731 posted

    Could you post the error message?

    Matt

    Tuesday, April 26, 2011 5:14 PM
  • User-1690110300 posted

    When it gets to image.save(filename) it gives me this error - 

    A generic error occurred in GDI+.

    Tuesday, April 26, 2011 6:21 PM
  • User1459534743 posted

    Here is a good example on how you can fix it - http://blog.vishalon.net/index.php/bitmapsave-a-generic-error-occurred-in-gdi/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 26, 2011 9:49 PM
  • User-1498334129 posted

    filename could have some problems:

    The full path may not be right - you will have to put some Server.MapPath()

    There could be some mismatch with file extension and the image format. for eg, bmp image, and a file name with gif extension.

    Tuesday, April 26, 2011 9:55 PM
  • User-1696077569 posted

    Looks like a permission issue with asp.net or problem with filename.

    Where are you writing this file to?

    Does aspnet account has write permission to that account or Are you doing identity impersonation with a user who has write access to that folder?

    Tuesday, April 26, 2011 11:13 PM