I'd like to have transparent watermark background image in my html document and print it. To do so, I created the following background image using svc data uri:
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");
}
To print a background image using WebBrowser control I tried this post: How to print background image and styles in WebBrowser control. But it looks like it doesn't print my data-uri svc image because of the IE limitations.
How can I print watermark background Image in WebBrowser control?
Here is the alternative solution that I've tried.
I tried to print HTML converted to image silently in webbrowser, but it prints empty page. To do so I used HTMLRenderer is a nuget package. refereed to this answer.
public MemoryStream ConvertHtmlToImage(string html)
{
Bitmap m_Bitmap = new Bitmap("image");
HtmlRender.RenderGdiPlus(Graphics.FromImage(m_Bitmap),
html);
MemoryStream memoryStream = new MemoryStream();
m_Bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
return memoryStream;
}
MemoryStream HtmlImage = ConvertHtmlToImage(processedDocTest);
webBrowserTest = new System.Windows.Forms.WebBrowser();
webBrowserTest.DocumentStream = HtmlImage; //Assign the image as a stream
private void btn_print_test_Click(object sender, EventArgs e)
{
if (Common.TestPrinter != null && !string.IsNullOrEmpty(Common.TestPrinter))
{
SetupPage();
SetDefaultPrinter(Common.TestPrinter);
}
PrinterSettings settings = new PrinterSettings();
defaultPrinter = settings.PrinterName;
if (Common.TestPrinter == defaultPrinter)
{
PrintTest();
}
else
{
ResetSetupPage();
Environment.Exit(1);
}
}