Asp.net 用c创建Html代码的图像#

Asp.net 用c创建Html代码的图像#,asp.net,Asp.net,我已经创建了html代码,然后将此html页面另存为图像。我创建的html控件与所有图像和背景色一起正确显示在图像中。在本地主机上运行良好 但是我正在尝试创建html代码来在服务器上生成图像。图像正在创建,但它没有显示任何类似于背景色、图像等的内容 只显示空白图像 代码: 使用来自客户端的Ajax调用函数,我将html内容发送到服务器端 服务器端方法 [System.Web.Services.WebMethod()] public static void GenerateTempl

我已经创建了html代码,然后将此html页面另存为图像。我创建的
html
控件与所有图像和背景色一起正确显示在图像中。在本地主机上运行良好

但是我正在尝试创建html代码来在服务器上生成图像。图像正在创建,但它没有显示任何类似于背景色、图像等的内容

只显示空白图像

代码:

使用来自客户端的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();
        }
[System.Web.Services.WebMethod()]
公共静态void GenerateTemplateImage(字符串html_内容,字符串TemplateName)
{
var t=新线程(生成屏幕截图);
t、 SetApartmentState(ApartmentState.STA);
t、 Start();
}
公共静态void生成屏幕截图()
{
位图;
string html=string.Empty;
string Title=string.Empty;
string Meta=string.Empty;
字符串样式=string.Empty;
string ScriptBefore=string.Empty;
string ScriptAfter=string.Empty;
string Scripthead=string.Empty;

html=“不要使用WebBrowser控件,由于其COM遗留问题和非常糟糕的对象模型,它附带了很多约束

一个可能的解决办法是使用

特别是,本文解释了这一过程:


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

我认为 WebBubue/Cuff>类只在托管有可访问桌面的Windows会话中工作。如果您想在服务器端执行此任务,则希望在桌面会话中运行它。(如果您对登录到物理服务器感到不舒服,可能是在VM下)。不久前我遇到了这个问题,我发现服务器管理器中没有安装
静态内容
。我安装了静态内容,之后所有内容都显示出来。所以这是第一个可能性。安装静态内容告诉IIS将静态内容(如css、图像等)传递给web请求。我希望这对您有所帮助,wi致以最良好的祝愿。