2

I have created html code and then save this html page as an image . The html controls which I have created is showing properly in the image with all images and background color. It is woking fine on localhost.

but I am trying to creating html code to image on the server. the image is creating but it's not showing anything like bgcolor, images, etc.

only blank image is showing.

Code :

Using Ajax calling function from client side I am sending the html content to the serverside

Server Side Method

[System.Web.Services.WebMethod()]
       public static void GenerateTemplateImage(string html_Content, string TemplateName)
        {
            var t = new Thread(MakeScreenshot);
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }


        public static void MakeScreenshot()
        {
            Bitmap bitmap;
            string html = string.Empty;

            string Title = string.Empty;
            string Meta = string.Empty;
            string Style = string.Empty;
            string ScriptBefore = string.Empty;
            string ScriptAfter = string.Empty;
            string Scripthead = string.Empty;

            html="<div><div id='s_p_box-1' style='background-color: rgb(24, 0, 238); width: 109px; height: 75px;>Welcome </div>' <br/> <img id='template1' class='template' style='border:1px solid green; height:142px;width:116px' src='http://ace.demos.classicinformatics.com/Advertiser-Admin/Campaign/UserTemplate/template1.jpg'></div>";

            WebBrowser wb = new WebBrowser();



            wb.Navigate("about:blank");

            if (wb.Document != null)
            {
                wb.Document.Write(html);
            }

            wb.DocumentText = html;

            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;

            // Set the size of the WebBrowser control
            // Take Screenshot of the web pages full width
          //  wb.Width = wb.Document.Body.ScrollRectangle.Width;
            wb.Width = 1024;
            // Take Screenshot of the web pages full height
          //  wb.Height = wb.Document.Body.ScrollRectangle.Height;
            //wb.Height = 786;
            wb.ScrollBarsEnabled = true;

            if (wb.Height <= 0)
            {
                wb.Height = 1024;
            }
            //if (wb.Width <= 400)
            //{
            //    wb.Width = 700;
            //}

            // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
            //Bitmap bitmap = new Bitmap(wb.Width, wb.Height);

            //using (bitmap = new Bitmap(wb.Width, wb.Height))
            using (bitmap = new Bitmap(wb.Width, wb.Height))
            {

                //wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
                wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
                //string imgPath = HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["ImgPath"].ToString());
                //string imgPath="C:\\Projects\\aec\\Ace-A-Metric\\Advertiser-Admin\\Campaign\\UserTemplate\\";
                string imgPath = URlPath + "test123" + ".bmp";
                //bitmap.Save(@"D:\" + txtTempName.Text + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                bitmap.Save(imgPath, System.Drawing.Imaging.ImageFormat.Bmp);
                //string imgpath = Path.Combine(HttpContext.Current.Server.MapPath("~") + "Advertiser-Admin\\Campaign\\UserTemplate\\" + txtTempName.Text +".bmp");
                //bitmap.Save(imgpath, System.Drawing.Imaging.ImageFormat.Bmp);
            }
            wb.Dispose();
            GC.Collect();
        }
2
  • I think the WebBrowser class only works if it's hosted within a Windows Session that has access to a desktop. If you want to do this task server-side then you'll want to run it within a desktop session (perhaps under a VM if you're uncomfortable with being logged-in to the physical server). Commented Oct 25, 2012 at 6:35
  • I had this problem some time ago and I found out that static content was not installed in server maneger ! I installed static content and after that everything showed up . So this is the first posibility . Installing static content tells IIS to pass static contents such as css , images & ... to web requests . I hope It helps you , with best wishes . Commented Oct 25, 2012 at 8:02

1 Answer 1

1

Do not use the WebBrowser control, it is shipped with a lot of constraints due its COM legacy, and the very poor object model.

One of the possible solution is to use Awesomium.Net

Espacially, this article explain the process : Capturing Web-Pages With C# (.NET)

The major difference, is that Awesomium and its .Net wrapper is written with no dependency to the host (actually from the Chromium source code). Then the library is actually standalone and let you consider a lots of more scenarios.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.