////////////////////////////////////////////////////////////////////
        // Draw image of a flower and clip it
        ////////////////////////////////////////////////////////////////////

        private void DrawFlower
        (
            PdfDocument Document,
            PdfContents Contents
        )
        {
            // define local image resources
            PdfImage Image1 = new PdfImage(Document, "flower.jpg");

            // image size will be limited to 1.4" by 1.4"
            SizeD ImageSize = Image1.ImageSize(1.4, 1.4);

            // save graphics state
            Contents.SaveGraphicsState();

            // translate coordinate origin to the center of the picture
            Contents.Translate(3.36, 5.7);

            // clipping path
            Contents.DrawOval(-ImageSize.Width / 2, -ImageSize.Height / 2, ImageSize.Width, ImageSize.Height, PaintOp.ClipPathEor);

            // draw image
            Contents.DrawImage(Image1, -ImageSize.Width / 2, -ImageSize.Height / 2, ImageSize.Width, ImageSize.Height);

            // restore graphics state
            Contents.RestoreGraphicsState();
            return;
        }
示例#2
0
        /// <summary>
        /// Add an image to the current page with a specified width
        /// </summary>
        /// <param name="path">Image file path</param>
        /// <param name="width">Target width for image in mm</param>
        /// <param name="options">ImageOptions</param>
        public void AddImage(string path, double width, ImageOptions options = null)
        {
            this.checkBuilderState();

            if (options == null)
            {
                options = new ImageOptions()
                {
                    PositionX = this.PagePositionX,
                    PositionY = this.PagePositionY
                };
            }

            // load the image from size and determine it new pixel dimensions based on relative resolutions
            var    srcBitmap            = new Bitmap(path);
            double srcBitmapAspectRatio = srcBitmap.Size.Height / (srcBitmap.Size.Width * 1.0);
            double widthInInches        = width / 25.4;
            int    widthInPixels        = Convert.ToInt32(Math.Round(widthInInches * srcBitmap.HorizontalResolution));
            int    heightInPixels       = Convert.ToInt32(Math.Round(widthInPixels * srcBitmapAspectRatio));

            // resize the source image and load into pdf
            var targetBitmap = Helpers.Image.Resize(srcBitmap, widthInPixels, heightInPixels, Color.White);

            PdfImage pdfImage = new PdfImage(this.document);

            pdfImage.Resolution   = options.Resolution;
            pdfImage.ImageQuality = options.Quality;
            pdfImage.LoadImage(targetBitmap);

            this.contents.SaveGraphicsState();

            // Height is calculated from width as per aspect ratio
            double height = width * pdfImage.HeightPix / pdfImage.WidthPix;

            SizeD newSize   = pdfImage.ImageSize(width, height);
            var   positionX = options.PositionX;
            var   positionY = options.PositionY - newSize.Height;

            this.contents.DrawImage(pdfImage, positionX, positionY, newSize.Width, newSize.Height);
            this.contents.RestoreGraphicsState();
            this.pagePosition.Y = positionY;
        }