How to print background image in webBrowser control

≯℡__Kan透↙ 提交于 2019-12-24 06:29:23

问题


I want to print the background-images through my web browser control.

    body {
        background-image: url("data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%20%3D%20%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20version%20%3D%20%271.1%27%20height%20%3D%20%27100px%27%20width%20%3D%20%27100px%27%3E%3Ctext%20transform%20%3D%20%27translate(20%2C%20100)%20rotate(-45)%27%20fill%20%3D%20%27rgb(245%2C45%2C45)%27%20font-size%20%3D%20%2720%27%3E%20watermark%20%3C%2Ftext%3E%3C%2Fsvg%3E");
    }

string styledDocTest = StyleStatementDoc(statement,true);
string HtmlDocTest = EvalStatement(styledDocTest, statement.StatementLanguage);
webBrowserTest = new System.Windows.Forms.WebBrowser();
webBrowserTest.SetHtmlContent(HtmlDocTest , $@"{new Uri(Application.StartupPath)}/");

 webBrowserTest.Print();

How to Keep background images during printing.


回答1:


There is a Print Background Colors and Images setting in Page Setup dialog which is shared between Web Browser Control and Internet Explorer.

Page setup settings for Microsoft Internet Explorer are stored in the following registry key:

HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup

Print Background Colors and Images value is stored in Print_Background key which can be yes or no. You can change the setting using code, but just keep in mind, these values are system-wide settings and will affect all instances of the WebBrowser control and Internet Explorer for the current user:

using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
    @"Software\Microsoft\Internet Explorer\PageSetup", true))
{
    key.SetValue("Print_Background", "yes", Microsoft.Win32.RegistryValueKind.String);
    key.Close();
}

Here is the test html that I used:

<html>
  <head>
    <style>
     body { background-image: url("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"); } 
    </style>
  </head>
  <body>
  </body>
</html>


来源:https://stackoverflow.com/questions/58959503/how-to-print-background-image-in-webbrowser-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!