我目前正在开发 PDF 生成器。我需要先绘制背景图像,然后绘制一个透明度为 85% 的深色图层。我可以很好地绘制它,但是当我想之后绘制两个图像时,这些图像也会获得透明度,而这不是我想要的。
XBrush brush = new XSolidBrush(XColor.FromArgb((int)(.85 * 255), 255, 255, 255));
DrawPageBackground(gfx,backgroundImage,page.Width.Value,page.Height.Value);
gfx.DrawRectangle(b,0,0,gfx.PageSize.Width,gfx.PageSize.Height );
gfx.DrawImage(otherImage,25,25);
有什么原因导致我不能在没有透明度的情况下绘制图像吗?这只是我现在没有做的简单事情吗?
谢谢。
最佳答案
我通过在绘制带有暗层的背景之前保存 XGraphicsState 成功解决了这个问题。绘制完成后,我使用 XGraphicsState 恢复并绘制没有任何透明度的图像。请看下面的一段代码。
XGraphicsState state = gfx.Save();
XBrush brush = new XSolidBrush(XColor.FromArgb((int)(.85 * 255), 255, 255, 255));
DrawPageBackground(gfx,backgroundImage,page.Width.Value,page.Height.Value);
gfx.DrawRectangle(b,0,0,gfx.PageSize.Width,gfx.PageSize.Height );
gfx.DrawImage(otherImage,25,25);
gfx.Restore(state);
DrawPageBackground方法:
private static void DrawPageBackground(XGraphics gfx, XImage image, double pageWidth, double pageHeight)
{
if (image.Size.Width > pageWidth)
gfx.DrawImage(image, CalculateDiffImageCenterToPageCenter(image,pageWidth), 0, CalculateBackgroundImageWidth(image,pageHeight), pageHeight);
else
gfx.DrawImage(image, 0, 0, CalculateBackgroundImageWidth(image, pageHeight),pageHeight);
}
这只是一个辅助方法,用于将背景图像绘制到正确的比例并将其居中。
关于c# - PDFsharp - 以透明度绘制的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31449327/