所有示例都展示了如何使用PDFsharp添加文本水印。但是,如果我需要添加图像水印怎么办?
我是这样做的:
if (!String.IsNullOrEmpty(_logo) && System.IO.File.Exists(_logo))
{
XImage logo = XImage.FromFile(_logo);
XRect rectLogo = new XRect(0, 0, page.Width, page.Height);
rectLogo.Scale(0.5, 0.5);
gfx.DrawImage(logo, rectLogo);
}
出现图像,但不显示水印。
编辑:
我终于手动放置了水印,但是,还有另一件事需要解决。如何更改XImage的不透明度?我可以通过使用图像处理器来做到这一点,但这不是主意。这是一款将使用普通用户的软件,我无法告诉他在将其分配为水印之前先对其进行修改。有什么解决办法吗?
这是当前的代码:
if (!String.IsNullOrEmpty(_logo) && System.IO.File.Exists(_logo))
{
XImage logo = XImage.FromFile(_logo);
double width = logo.PointWidth;
double height = logo.PointHeight;
double ratio = width / height;
if (width > page.Width.Point * 0.5)
{
width = page.Width.Point * 0.5;
height = width / ratio;
}
else if (height > page.Height.Point * 0.5)
{
height = page.Height.Point * 0.5;
width = height * ratio;
}
double offsetX = (page.Width.Point - width) / 2;
double offsetY = (page.Height.Point - height) / 2;
XRect rectLogo = new XRect(offsetX, offsetY, width, height);
gfx.DrawImage(logo, rectLogo);
}
像往常一样,计算机会执行您告诉他的操作,而不是您要他执行的操作。
如果要使图像位于其他内容之下,请首先进行绘制。
如果要具有半透明图像,请使用透明图像或使用透明图像绘制。在这种情况下,您可以将图像放置在页面的所有其他内容之上。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句