.net 将Div标记的内容转换为图像格式

.net 将Div标记的内容转换为图像格式,.net,c#-4.0,.net,C# 4.0,我是asp.net的软件实习生 这是我的div标签 <div id="contentsDiv"> <b>Write some thing</b> <br/><br> <img alt="Koala" src="Images/Koala.jpg" width="400" height="400" /> <br< /><br> <i>Some th

我是asp.net的软件实习生

这是我的div标签

<div id="contentsDiv">
    <b>Write some thing</b>
    <br/><br>
    <img alt="Koala" src="Images/Koala.jpg" width="400" height="400" />
    <br< /><br>
    <i>Some thing means any thing </i>
</div>



<asp:Button ID="Export" runat="server" Text="Export" onclick="Export_Click" />

写点东西



有些东西意味着任何东西

现在,单击“导出”按钮,我想将标记的内容转换为图像,并将该图像保存在特定文件夹中。

将html内容转换为图像并不容易。。但我认为你能做到

 class Program
    {
         [STAThread]
        static void Main(string[] args)
        {

            var bmp = MakeScreenshot(@"<div><h1>Hello World</h1></div>");

            bmp.Save(@"c:\pdf\test.jpg");
        }


        public static Bitmap MakeScreenshot(string html)
        {
            // Load the webpage into a WebBrowser control
            WebBrowser wb = new WebBrowser();
            wb.Navigate("about:blank");
            if (wb.Document != null)
            {

                wb.Document.Write(html);

            }

            wb.DocumentText = html;

            wb.ScrollBarsEnabled = true;
            wb.ScriptErrorsSuppressed = true;
            //      wb.Navigate(this.Uri);
            while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }


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

            if (wb.Height <= 0)
            {
                wb.Height = 768;
            }

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

            return bitmap;
        }
    }
类程序
{
[状态线程]
静态void Main(字符串[]参数)
{
var bmp=MakeScreenshot(@“Hello World”);
bmp.Save(@“c:\pdf\test.jpg”);
}
公共静态位图生成屏幕截图(字符串html)
{
//将网页加载到WebBrowser控件中
WebBrowser wb=新的WebBrowser();
wb.导航(“关于:空白”);
如果(wb.Document!=null)
{
wb.Document.Write(html);
}
wb.DocumentText=html;
wb.ScrollBarsEnabled=true;
wb.ScriptErrorsSuppressed=true;
//导航(this.Uri);
而(wb.ReadyState!=webbrowserereadystate.Complete){Application.DoEvents();}
//设置WebBrowser控件的大小
//全幅拍摄网页截图
wb.Width=wb.Document.Body.ScrollRectangle.Width;
//拍摄网页全高截图
wb.Height=wb.Document.Body.ScrollRectangle.Height;

如果(wb.Height请给我们一些关于你的webapp和你想要实现的目标的更多细节。你可能是指客户端导出,但我不能100%确定

如果是这种情况,您可以将此div绘制到画布元素中,然后使用
htmlcanvaseelement
对象的
toDataURL
允许用户通过浏览器“closed”div内容进行保存

var c = document.getElementById('the_canvas_element_id');
var t = c.getContext('2d');
当用户单击“导出”时,请执行以下操作:

window.open('', document.getElementById('the_canvas_element_id').toDataURL());
和中的更多信息


这对于软件培训生来说是一项相当困难的任务:)。

它显示了一些错误,请告诉我必须在哪个文件上编写此代码,即.fileName.aspx.cs或fileName.cs