PDFsharp Ximage 不包含包含 1 个参数的构造函数

PDFsharp Ximage does not contain a constructor that contains 1 argument

我正在尝试编写一个 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);
    }
}

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

class 中的最后一行是隐式运算符,而不是构造函数。这将隐式地将 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

您能做的最好的事情是:跳过创建 Image 并直接从流创建 XImage

XImage class 有一些静态成员。有 XImage.FromGdiImage 需要一个 Image (这将是你问题的答案)并且有 XImage.FromStream 避免首先创建 Image

当您的项目引用 PDFsharp DLL(例如来自 NuGet)时,Visual Studio 将向您显示无法更改的反编译元数据。您可以使用 PDFsharp 源代码,添加对 .csproj 文件的引用并根据需要进行更改。
Image 作为参数的构造函数不是 public。无需将其设置为 public,因为您可以使用 XImage.FromGdiImage 来访问它。请注意,只有 PDFsharp 的 GDI 构建具有此方法。使用 WPF 构建时,可以使用 XImage.FromStream.