2

我创建了 html 代码,然后将此 html 页面保存为图像。html我创建的控件在带有所有图像和背景颜色的图像中正确显示。它在 localhost 上运行良好。

但我正在尝试创建 html 代码以在服务器上成像。图像正在创建,但未显示 bgcolor、图像等任何内容。

只显示空白图像。

代码 :

从客户端使用 Ajax 调用函数我将 html 内容发送到服务器端

服务器端方法

[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();
        }
4

1 回答 1

1

不要使用 WebBrowser 控件,由于它的 COM 遗留问题和非常糟糕的对象模型,它带有很多约束。

一种可能的解决方案是使用Awesomium.Net

特别是,这篇文章解释了这个过程:Capturing Web-Pages With C# (.NET)

主要区别在于 Awesomium 及其 .Net 包装器的编写不依赖于主机(实际上来自 Chromium 源代码)。然后这个库实际上是独立的,让你考虑更多的场景。

于 2013-07-09T12:27:20.653 回答