HTML源代码在asp.net中转换为图像

时间:2017-03-29 04:57:22

标签: asp.net c#-4.0

我有html来源,我想转换为asp.net中的图片。 我的html源来自数据库,例如。

<table border="0" cellpadding="0" cellspacing="0" width="658" style="border-collapse: collapse;width:494pt"> <colgroup><col width="146" style="mso-width-source:userset;mso-width-alt:5339;width:110pt" /> <col width="64" span="8" style="width:48pt" /> </colgroup>
    <tbody>
        <tr height="undefined" style="height:15.0pt">  
            <td height="20" width="146" style="height:15.0pt;width:110pt"></td>  
            <td width="64" style="width:48pt"></td>  
            <td width="64" style="width:48pt"></td>  
            <td class="xl65" align="right" width="64" style="width:48pt">Jfhn-20</td>  
            <td class="xl65" align="right" width="64" style="width:48pt">Jfhan-40</td>  
            <td class="xl65" align="right" width="64" style="width:48pt">Jaghn-80</td>  
            <td class="xl66" width="64" style="width:48pt">1/160</td>  
            <td class="xl66" width="64" style="width:48pt">fgh1/320</td>  
            <td class="xl66" width="64" style="width:48pt">1/640</td> </tr> 
        <tr height="undefined" style="height:15.0pt">  
            <td height="20" class="xl66" style="height:15.0pt">S.Typhi-O:</td>  
            <td class="xl66"></td>  
            <td></td>  
            <td class="xl66">#</td>  
            <td class="xl66">#</td>  
            <td class="xl66">#</td>  
            <td class="xl66">#</td>  
            <td class="xl66">#</td>  
            <td class="xl66">#</td> </tr> 
        <tr height="undefined" style="height:15.0pt">  
            <td height="20" class="xl66" style="height:15.0pt">S.Typhi-H:</td>  
            <td class="xl66"></td>  
            <td></td>  
            <td class="xl66">#</td>  
            <td class="xl66">#</td>  
            <td class="xl66">#</td>  
            <td class="xl66">#</td>  
            <td class="xl66">#</td>  
            <td class="xl66">#</td> </tr> 
        <tr height="undefined" style="height:15.0pt">  
            <td height="20" class="xl66" style="height:15.0pt">S.Paratyphi-AH:</td>  
            <td class="xl66">#</td>  
            <td class="xl66">#</td>  
            <td class="xl66">#</td>  
            <td class="xl66">#</td>  
            <td class="xl66">#</td>  
            <td class="xl66">#</td>  
            <td></td>  
            <td></td> </tr> 
        <tr height="undefined" style="height:15.0pt">  
            <td height="20" class="xl66" style="height:15.0pt">S.Paratyphi-BH:</td>  
            <td class="xl66">#</td>  
            <td class="xl66">#</td>  
            <td class="xl66">#</td>  
            <td class="xl66">#</td>  
            <td class="xl66">#</td>  
            <td class="xl66">#</td>  
            <td></td>  
            <td></td> 
        </tr>
    </tbody>
</table>

我想转换成图片。 我正在使用代码

private static void StartBrowser(string source)
{
    var th = new Thread(() =>
    {
        var webBrowser = new WebBrowser();
        webBrowser.ScrollBarsEnabled = false;
        webBrowser.DocumentCompleted += webBrowser_DocumentCompleted;
        webBrowser.DocumentText = source;
        Application.Run();
    });
    th.SetApartmentState(ApartmentState.STA);
    th.Start();
}

static void webBrowser_DocumentCompleted(
    object sender,
    WebBrowserDocumentCompletedEventArgs e)
{
    var webBrowser = (WebBrowser)sender;
    using (Bitmap bitmap =
        new Bitmap(webBrowser.Width, webBrowser.Height))
    {
        webBrowser.DrawToBitmap(
            bitmap,
            new System.Drawing
                .Rectangle(0, 0, bitmap.Width, bitmap.Height));
        bitmap.Save(@"c:\\filename.jpg",
            System.Drawing.Imaging.ImageFormat.Jpeg);
    }
}

但它显示如下错误:

  

“错误HRESULT E_FAIL已从调用COM组件返回”

在以下行

webBrowser.DocumentText = source;

建议我如何将html源代码转换为asp.net中的图像

0 个答案:

没有答案