如何用PDFsharp绘制圆形图像

How to draw a round image with PDFsharp

我正在使用 PDFsharp 制作 PDF 文件,并已成功将图像放到我的页面上。

        byte[] imgBytes = interview.Application.CandidateImage.ImageBinary.ToArray();
        Stream stream = new MemoryStream(imgBytes);
        MemoryStream strm = new MemoryStream();
        System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
        img.Save(strm, System.Drawing.Imaging.ImageFormat.Png);

        XImage xfoto = XImage.FromGdiPlusImage(img);

        gfx.DrawImage(xfoto, 30, 130, 300, 300);

这会获取二进制图像数据,DrawImage 将绘制从流中检索到的图像。

问题是,我想让图像变圆,就像我在 HTML 上使用 img-circle class 一样。 PDFsharp 有这个功能吗?如果没有,我该怎么做?

编辑:

    void DrawClipPath(XGraphics gfx, PdfPage page)
    {


        XGraphicsPath path = new XGraphicsPath();
        path.AddEllipse((page.Width / 2) - 150, (page.Height / 2) - 120, 300, 300);
        gfx.Save();
        gfx.IntersectClip(path);

        // Draw a beam of dotted lines
        XPen pen = XPens.DarkRed.Clone();
        pen.DashStyle = XDashStyle.Dot;
        for (double r = 0; r <= 90; r += 0.5)
            gfx.DrawLine(pen, 0, 0, 250 * Math.Cos(r / 90 * Math.PI), 250 * Math.Sin(r / 90 * Math.PI));

        gfx.Restore();


    }

您可以使用具有透明度的图像,这样只有中间的圆形部分可见。

或者使用 hack:先绘制图像,然后在图像上绘制白色遮罩,只留下中间的圆形部分。

您可以设置一个圆作为裁剪路径,然后绘制图像。设置裁剪路径前必须先保存图形状态(XGraphics.Save),画完所有需要裁剪的对象后再恢复(XGraphics.Restore)

编辑:我不熟悉 PDFSharp API 但代码看起来像这样:

gfx.Save();
XGraphicsPath clipPath = new XGraphicsPath();
clipPath.AddEllipse(30, 130, 300, 300);
gfx.IntersectClip(clipPath);
gfx.DrawImage(xfoto, 30, 130, 300, 300);
gfx.Restore();