PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Cutting off an image off at the bottom of XRect
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3737
Page 1 of 1

Author:  mbeg [ Wed Mar 07, 2018 5:09 pm ]
Post subject:  Cutting off an image off at the bottom of XRect

Using PDFsharp NuGet v1.32.3057.0 on a windows form application.

I'm trying to figure out how I can define an XRect and tile an XImage all the way to bottom. If the image goes past the bottom of the rectangle, I want it to cut the image off at the bottom of the rectangle.

So right now, I have the tiling portion done. I can't figure out how to use the image transformations to cut the image off.

Tiling portion -
Code:
XRect graphicRect = newXRect(topLeftPt, bottomRightPt);

double imageY;
double width = scale * image.PointWidth;
double height = scale * image.PointHeight;

for (imageY = 0; imageY < graphicRect.Height - height; imageY += height)
{
    gfx.DrawImage(image, graphicRect.X, graphicRect.Y + imageY, width, height);
}


After this, I have the line
Code:
gfx.DrawImage(image, graphicRect.X, graphicRect.Y + imageY, width, graphicRect.Height - imageY);


This skews the image dimensions to fit the remaining space. What I'd like to do is just cut the rest of the image off after the bottom of the rectangle has been reached. Does anyone know how to accomplish this?

Author:  Thomas Hoevel [ Wed Mar 07, 2018 5:56 pm ]
Post subject:  Re: Cutting off an image off at the bottom of XRect

Hi!

Here's what I would try: Draw the full image, but use a path to clip it.

See also:
http://www.pdfsharp.net/wiki/Graphics-s ... gh_path_16

Author:  mbeg [ Wed Mar 07, 2018 6:16 pm ]
Post subject:  Re: Cutting off an image off at the bottom of XRect

Thomas Hoevel wrote:
Hi!

Here's what I would try: Draw the full image, but use a path to clip it.

See also:
http://www.pdfsharp.net/wiki/Graphics-s ... gh_path_16


Thanks for the suggestion Thomas. I have it functioning properly now, below is my solution for any people who struggle with this in the future.

Code:
            XRect graphicRect = new XRect(topLeftGraphicPt, bottomRightGraphicPt);
            double imageY;
            double scale = 21 / image.PointWidth;
            double width = scale * image.PointWidth;
            double height = scale * image.PointHeight;
                   
            for (imageY = 0; imageY < graphicRect.Height - height; imageY += height)
            {
                  gfx.DrawImage(image, graphicRect.X, graphicRect.Y + imageY, width, height);
            }
            DrawGraphic(gfx, image, graphicRect, imageY, width, height);


Where the DrawGraphic method is -

Code:
        private static void DrawGraphic(XGraphics gfx, XImage image, XRect rect, double imageY, double width, double height)
        {
            gfx.Save();
            XGraphicsPath clipPath = new XGraphicsPath();
            clipPath.AddRectangle(rect);
            gfx.IntersectClip(clipPath);
            gfx.DrawImage(image, rect.X, rect.Y + imageY, width, height);
            gfx.Restore();
        }

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/