Hi smile,
Please refer below sample.
Namespaces
using System.Drawing;
using System.Drawing.Imaging;
Code
private void Form1_Load(object sender, EventArgs e)
{
var dataURL = "";
using (var image = WatermarkImage("@ASPSnippets", new Font("Arial", 16, FontStyle.Bold), Color.Blue, new Size(200, 200)))
{
dataURL = GetDataURL(image);
var html = $@"
<html>
<head>
<style>body {{background-image: url(""{dataURL}""); background-repeat: no-repeat;}}</style>
</head><body>
</body>
</html>";
webBrowser1.DocumentText = html;
}
}
public Image WatermarkImage(string text, Font font, Color color, Size size)
{
var bm = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb);
using (var g = Graphics.FromImage(bm))
{
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
g.RotateTransform(0);
var format = new StringFormat();
format.Alignment = StringAlignment.Center;
using (var brush = new SolidBrush(color))
g.DrawString(text, font, brush, new Rectangle(Point.Empty, size), format);
}
return bm;
}
public static string GetDataURL(Image image)
{
var bytes = (byte[])new ImageConverter().ConvertTo(image, typeof(byte[]));
return $"data:image/png;base64,{Convert.ToBase64String(bytes)}";
}
Screenshot