简体   繁体   中英

How to draw a round image with PDFsharp

I am using PDFsharp to make a PDF file, and have succeeded to put an image on my page.

        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);

This gets the binary image data and the DrawImage will draw this image retrieved from the stream.

The thing is, I want to make the image round just as if I used img-circle class on HTML. Is there a function for this with PDFsharp? If not, how can I do this?

EDIT:

    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();


    }

You can use images with transparency so that only the round part in the centre is visible.

Or use a hack: draw the image first, then draw a white mask over the image, leaving just the round part in the centre showing.

You can set a circle as a clip path and then draw the image. You have to save the graphic state (XGraphics.Save) before setting the clip path and then restore it (XGraphics.Restore) after you drew all the objects that needed to be clipped.

EDIT: I'm not familiar with PDFSharp API but the code would look something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM