PDFsharp Ximage不包含包含1个参数

时间:2017-07-05 23:26:13

标签: c# image pdfsharp

我正在尝试编写一个XImage函数,该函数从给定的URL读取图像,它看起来像:

    public static XImage FromURI(string uri)
    {
        HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
        webRequest.AllowWriteStreamBuffering = true;
        WebResponse webResponse = webRequest.GetResponse();
        System.Drawing.Image image = System.Drawing.Image.FromStream(webResponse.GetResponseStream());
        webResponse.Close();
        PdfSharp.Drawing.XImage ximg = new PdfSharp.Drawing.XImage(image);
        return new PdfSharp.Drawing.XImage(image);
    }

但我在行上收到错误

        return new PdfSharp.Drawing.XImage(image);

它说:

XImage does not contain a constructor that contains 1 argument.

当我查看XImage.cs文件时,似乎它有一个带有1个参数的构造函数。

namespace PdfSharp.Drawing
{
    //
    // Summary:
    //     Defines an object used to draw image files (bmp, png, jpeg, gif) and PDF forms.
    //     An abstract base class that provides functionality for the Bitmap and Metafile
    //     descended classes.
    public class XImage : IDisposable
    {
        //
        // Summary:
        //     Initializes a new instance of the PdfSharp.Drawing.XImage class.
        protected XImage();

        //
        // Summary:
        //     Gets the vertical resolution of the image.
        public virtual double VerticalResolution { get; }
        //
        // Summary:
        //     Gets the horizontal resolution of the image.
        public virtual double HorizontalResolution { get; }
        //
        // Summary:
        //     Gets the size in point of the image.
        public virtual XSize Size { get; }
        //
        // Summary:
        //     Gets the height of the image in pixels.
        public virtual int PixelHeight { get; }
        //
        // Summary:
        //     Gets the width of the image in pixels.
        public virtual int PixelWidth { get; }
        //
        // Summary:
        //     Gets the height of the image in point.
        public virtual double PointHeight { get; }
        //
        // Summary:
        //     Gets the width of the image in point.
        public virtual double PointWidth { get; }
        //
        // Summary:
        //     Gets the height of the image.
        [Obsolete("Use either PixelHeight or PointHeight. Temporarily obsolete because of rearrangements for WPF. Currently same as PixelHeight, but will become PointHeight in future releases of PDFsharp.")]
        public virtual double Height { get; }
        //
        // Summary:
        //     Gets the width of the image.
        [Obsolete("Use either PixelWidth or PointWidth. Temporarily obsolete because of rearrangements for WPF. Currently same as PixelWidth, but will become PointWidth in future releases of PDFsharp.")]
        public virtual double Width { get; }
        //
        // Summary:
        //     Gets or sets a flag indicating whether image interpolation is to be performed.
        public virtual bool Interpolate { get; set; }
        //
        // Summary:
        //     Gets the format of the image.
        public XImageFormat Format { get; }

        //
        // Summary:
        //     Tests if a file exist. Supports PDF files with page number suffix.
        //
        // Parameters:
        //   path:
        //     The path to a BMP, PNG, GIF, JPEG, TIFF, or PDF file.
        public static bool ExistsFile(string path);
        //
        // Summary:
        //     Creates an image from the specified file.
        //
        // Parameters:
        //   path:
        //     The path to a BMP, PNG, GIF, JPEG, TIFF, or PDF file.
        public static XImage FromFile(string path);
        //
        // Summary:
        //     Conversion from Image to XImage.
        public static XImage FromGdiPlusImage(Image image);
        //
        // Summary:
        //     Under construction
        public void Dispose();
        //
        // Summary:
        //     Disposes underlying GDI+ object.
        protected virtual void Dispose(bool disposing);

        //
        // Summary:
        //     Implicit conversion from Image to XImage.
        public static implicit operator XImage(Image image);
    }
}

即使我尝试修复此文件,也无法修复,因为它有[来自元数据]标签。

2 个答案:

答案 0 :(得分:0)

您可以做的最好:跳过创建Image并直接从流中创建XImage

XImage类有一些静态成员。有XImage.FromGdiImageImage的故事(这将是您问题的答案),XImage.FromStream可以避免首先创建Image

当您的项目引用PDFsharp DLL(例如来自NuGet)时,Visual Studio将显示无法更改的反编译元数据。您可以使用PDFsharp源代码,添加对.csproj文件的引用并进行所需的更改 以Image为参数的构造函数不公开。无需公开,因为您可以使用XImage.FromGdiImage来访问它。

答案 1 :(得分:-1)

类中的最后一行是隐式运算符,而不是构造函数。这会将Image转换为XImage,即无数据丢失。你不能像你一样从Image创建XImage。你需要这样做:

 XImage xImage = new PdfSharp.Drawing.XImage();
 xImage = image;
 return xImage;

您可以阅读implice https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/implicit