如何用PDFsharp绘制圆形图像
问题描述:
我使用PDFsharp制作PDF文件,并成功将图像放在我的页面上。如何用PDFsharp绘制圆形图像
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类一样。用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();
}
答
您可以将圆圈设置为剪辑路径,然后绘制图像。您必须在设置剪辑路径之前保存图形状态(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();
答
您可以使用图像的透明度,以便只有在中央圆形部分是可见的。
或者使用黑客:首先绘制图像,然后在图像上绘制白色蒙版,只留下中心的圆形部分。
我不太确定剪辑路径的作用。你能帮我一个示例代码吗? – Dukakus17
你可以看看我的编辑?我按照网站上的例子做了类似的事情。但图像保持不变。你能纠正我的错误吗? – Dukakus17
@ user7677413看到我的编辑,您在绘制图像(或任何其他想要剪裁的对象)后恢复图形。 – iPDFdev