c# - PDFsharp - 以透明度绘制的图像

标签 c# pdf pdfsharp

我目前正在开发 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/

相关文章:

C#.NET : How to check if we're running on battery?

php - 在 Php 中将 Fpdf 中的文本换行

python - 从 subprocess.check_call() 获取输出时出现问题

debugging - 你如何调试PDF文件?

c# - 在 C# 中将 PDF 导出为 JPG

c# - 左缩进的 MigraDoc 表边框问题

c# - iTextSharp 是否处理 PDF 压缩?

c# - 声明前不能使用局部变量DateTime吗?

c# - System.ValueType 是否继承自 System.Object?

c# - 在 C# 应用程序 + ip :port proxy 中使用 fiddler