Beispiel #1
0
        /// <summary>
        /// Write a page to an image stream in the specified format.
        /// </summary>
        /// <param name="pageNumber">The number of the page to render (starting at 0).</param>
        /// <param name="zoom">The scale at which the page will be rendered. This will determine the size in pixel of the image.</param>
        /// <param name="pixelFormat">The format of the pixel data.</param>
        /// <param name="outputStream">The stream to which the image data will be written.</param>
        /// <param name="fileType">The output format of the image.</param>
        /// <param name="includeAnnotations">If this is <see langword="true" />, annotations (e.g. signatures) are included in the display list that is generated. Otherwise, only the page contents are included.</param>
        public void WriteImage(int pageNumber, double zoom, PixelFormats pixelFormat, Stream outputStream, RasterOutputFileTypes fileType, bool includeAnnotations = true)
        {
            Rectangle region = this.Pages[pageNumber].Bounds;

            WriteImage(pageNumber, region, zoom, pixelFormat, outputStream, fileType, includeAnnotations);
        }
Beispiel #2
0
        /// <summary>
        /// Save a page to an image file in the specified format.
        /// </summary>
        /// <param name="pageNumber">The number of the page to render (starting at 0).</param>
        /// <param name="zoom">The scale at which the page will be rendered. This will determine the size in pixel of the image.</param>
        /// <param name="pixelFormat">The format of the pixel data.</param>
        /// <param name="fileName">The path to the output file.</param>
        /// <param name="fileType">The output format of the file.</param>
        /// <param name="includeAnnotations">If this is <see langword="true" />, annotations (e.g. signatures) are included in the display list that is generated. Otherwise, only the page contents are included.</param>
        public void SaveImage(int pageNumber, double zoom, PixelFormats pixelFormat, string fileName, RasterOutputFileTypes fileType, bool includeAnnotations = true)
        {
            Rectangle region = this.Pages[pageNumber].Bounds;

            SaveImage(pageNumber, region, zoom, pixelFormat, fileName, fileType, includeAnnotations);
        }
Beispiel #3
0
        /// <summary>
        /// Write (part of) a page to an image stream in the specified format.
        /// </summary>
        /// <param name="pageNumber">The number of the page to render (starting at 0).</param>
        /// <param name="region">The region of the page to render in page units.</param>
        /// <param name="zoom">The scale at which the page will be rendered. This will determine the size in pixel of the image.</param>
        /// <param name="pixelFormat">The format of the pixel data.</param>
        /// <param name="outputStream">The stream to which the image data will be written.</param>
        /// <param name="fileType">The output format of the image.</param>
        /// <param name="includeAnnotations">If this is <see langword="true" />, annotations (e.g. signatures) are included in the display list that is generated. Otherwise, only the page contents are included.</param>
        public void WriteImage(int pageNumber, Rectangle region, double zoom, PixelFormats pixelFormat, Stream outputStream, RasterOutputFileTypes fileType, bool includeAnnotations = true)
        {
            if (pixelFormat == PixelFormats.RGBA && fileType == RasterOutputFileTypes.PNM)
            {
                throw new ArgumentException("Cannot save an image with alpha channel in PNM format!", nameof(fileType));
            }

            if (DisplayLists[pageNumber] == null)
            {
                DisplayLists[pageNumber] = new MuPDFDisplayList(this.OwnerContext, this.Pages[pageNumber], includeAnnotations);
            }

            if (zoom < 0.000001 | zoom * region.Width <= 0.001 || zoom * region.Height <= 0.001)
            {
                throw new ArgumentOutOfRangeException(nameof(zoom), zoom, "The zoom factor is too small!");
            }

            if (this.ImageXRes != 72 || this.ImageYRes != 72)
            {
                zoom  *= Math.Sqrt(this.ImageXRes * this.ImageYRes) / 72;
                region = new Rectangle(region.X0 * 72 / this.ImageXRes, region.Y0 * 72 / this.ImageYRes, region.X1 * 72 / this.ImageXRes, region.Y1 * 72 / this.ImageYRes);
            }

            float fzoom = (float)zoom;

            IntPtr outputBuffer     = IntPtr.Zero;
            IntPtr outputData       = IntPtr.Zero;
            ulong  outputDataLength = 0;

            ExitCodes result = (ExitCodes)NativeMethods.WriteImage(OwnerContext.NativeContext, DisplayLists[pageNumber].NativeDisplayList, region.X0, region.Y0, region.X1, region.Y1, fzoom, (int)pixelFormat, (int)fileType, ref outputBuffer, ref outputData, ref outputDataLength);

            switch (result)
            {
            case ExitCodes.EXIT_SUCCESS:
                break;

            case ExitCodes.ERR_CANNOT_RENDER:
                throw new MuPDFException("Cannot render page", result);

            case ExitCodes.ERR_CANNOT_CREATE_CONTEXT:
                throw new MuPDFException("Cannot create the output buffer", result);

            default:
                throw new MuPDFException("Unknown error", result);
            }

            byte[] buffer = new byte[1024];

            while (outputDataLength > 0)
            {
                int bytesToCopy = (int)Math.Min(buffer.Length, (long)outputDataLength);
                Marshal.Copy(outputData, buffer, 0, bytesToCopy);
                outputData = IntPtr.Add(outputData, bytesToCopy);
                outputStream.Write(buffer, 0, bytesToCopy);
                outputDataLength -= (ulong)bytesToCopy;
            }

            NativeMethods.DisposeBuffer(OwnerContext.NativeContext, outputBuffer);
        }
Beispiel #4
0
        /// <summary>
        /// Save (part of) a page to an image file in the specified format.
        /// </summary>
        /// <param name="pageNumber">The number of the page to render (starting at 0).</param>
        /// <param name="region">The region of the page to render in page units.</param>
        /// <param name="zoom">The scale at which the page will be rendered. This will determine the size in pixel of the image.</param>
        /// <param name="pixelFormat">The format of the pixel data.</param>
        /// <param name="fileName">The path to the output file.</param>
        /// <param name="fileType">The output format of the file.</param>
        /// <param name="includeAnnotations">If this is <see langword="true" />, annotations (e.g. signatures) are included in the display list that is generated. Otherwise, only the page contents are included.</param>
        public void SaveImage(int pageNumber, Rectangle region, double zoom, PixelFormats pixelFormat, string fileName, RasterOutputFileTypes fileType, bool includeAnnotations = true)
        {
            if (pixelFormat == PixelFormats.RGBA && fileType == RasterOutputFileTypes.PNM)
            {
                throw new ArgumentException("Cannot save an image with alpha channel in PNM format!", nameof(fileType));
            }

            if (DisplayLists[pageNumber] == null)
            {
                DisplayLists[pageNumber] = new MuPDFDisplayList(this.OwnerContext, this.Pages[pageNumber], includeAnnotations);
            }

            if (zoom < 0.000001 | zoom * region.Width <= 0.001 || zoom * region.Height <= 0.001)
            {
                throw new ArgumentOutOfRangeException(nameof(zoom), zoom, "The zoom factor is too small!");
            }

            if (this.ImageXRes != 72 || this.ImageYRes != 72)
            {
                zoom  *= Math.Sqrt(this.ImageXRes * this.ImageYRes) / 72;
                region = new Rectangle(region.X0 * 72 / this.ImageXRes, region.Y0 * 72 / this.ImageYRes, region.X1 * 72 / this.ImageXRes, region.Y1 * 72 / this.ImageYRes);
            }

            float fzoom = (float)zoom;

            ExitCodes result = (ExitCodes)NativeMethods.SaveImage(OwnerContext.NativeContext, DisplayLists[pageNumber].NativeDisplayList, region.X0, region.Y0, region.X1, region.Y1, fzoom, (int)pixelFormat, fileName, (int)fileType);

            switch (result)
            {
            case ExitCodes.EXIT_SUCCESS:
                break;

            case ExitCodes.ERR_CANNOT_RENDER:
                throw new MuPDFException("Cannot render page", result);

            case ExitCodes.ERR_CANNOT_SAVE:
                throw new MuPDFException("Cannot save to the output file", result);

            default:
                throw new MuPDFException("Unknown error", result);
            }
        }