How to print css applied background images with WebBrowser control
-
09-06-2019 - |
Question
I am using the webbrowser control in winforms and discovered now that background images which I apply with css are not included in the printouts.
Is there a way to make the webbrowser print the background of the displayed document too?
Edit: Since I wanted to do this programatically, I opted for this solution:
using Microsoft.Win32;
...
RegistryKey regKey = Registry.CurrentUser
.OpenSubKey("Software")
.OpenSubKey("Microsoft")
.OpenSubKey("Internet Explorer")
.OpenSubKey("Main");
//Get the current setting so that we can revert it after printjob
var defaultValue = regKey.GetValue("Print_Background");
regKey.SetValue("Print_Background", "yes");
//Do the printing
//Revert the registry key to the original value
regKey.SetValue("Print_Background", defaultValue);
Another way to handle this might be to just read the value, and notify the user to adjust this himself before printing. I have to agree that tweaking with the registry like this is not a good practice, so I am open for any suggestions.
Thanks for all your feedback
Solution
If you're going to go and change an important system setting, make sure to first read the current setting and restore it when you are done.
I consider this very bad practice in the first place, but if you must do it then be kind.
Registry.LocalMachine
Also, try changing LocalUser
instead of LocalMachine
- that way if your app crashes (and it will), then you'll only confounded the user, not everyone who uses the machine.
OTHER TIPS
Another registry key would be : HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup\Print_Background HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer\PageSetup\Print_Background
The corresponding HKCU key for this setting is: HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\Print_Background
By default, the browser does not print background images at all.
In Firefox
* File > Page Setup > Check Off "Print Background"
* File > Print Preview
In IE
* Tools > Internet Options > Advanced > Printing
* Check Off "Print Background Images and Colors"
In Opera
* File > Print Options > Check Off "Print Page Background"
* File > Print Preview (You may have to scroll down/up to see it refresh)
var sh = new ActiveXObject("WScript.Shell");
key = "HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\Main\\Print_Background";
var defaultValue = sh.RegRead(key);
sh.RegWrite(key,"yes","REG_SZ");
document.frames['detailFrame'].focus();
document.frames['detailFrame'].print();
sh.RegWrite(key,defaultValue,"REG_SZ");
return false;