首页 文章

PDFsharp - 用透明度绘制的图像

提问于
浏览
2

我目前正在研究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);

我有什么理由不能在没有透明度的情况下绘制图像吗?我现在没做的只是简单的事吗?

谢谢 .

2 回答

  • 1

    我通过在使用暗色图层绘制背景之前保存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);
    }
    

    这只是一种帮助方法,可以将背景图像绘制到正确的比例并使其居中 .

  • 1

    我确认我在评论中写的内容:刷子的透明度设置也应用于图像是一个错误 .

    随着本周早些时候发布的1.50.3915-beta2版本,这个错误得到了解决 .

    对于早期版本,使用 Save()Restore() 是一种合适的解决方法,但是使用最新版本不再需要此解决方法 .

相关问题