2017-07-05 115 views
0

我试图写一个XImage功能,从给定的URL读取图像,它看起来像:PDFsharp Ximage不包含包含一个构造函数1参数

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个参数的nstructor。

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); 
    } 
} 

即使我试图修复这个文件,它不能被修复,因为它有一个[来自元数据]标签。

+0

我没有注意到元数据中有1个参数的构造函数。它在哪里?很可能你需要使用FromGdiPlusImage。 –

回答

-1

该类的最后一行是隐式操作符,而不是构造函数。这将明确地将Image转换为​​,即没有任何数据丢失。您无法像完成一样从Image创建XImage。你需要做的像:

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

你可以阅读有关implicehttps://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/implicit

+0

谢谢。有没有办法从本地主机调用URL来使用FromURI函数?现在,当我测试它时,它表示访问不被允许。 – Dukakus17

+0

我没有完全明白你的问题。但是你能够从浏览器访问你的输入网址吗? –

+0

它只适用于当我在Visual Studio上运行我的网页时。该网址看起来像localhost/somethingsomething ..所以它不适用于PDF生成器。 – Dukakus17

0

你能做的最好的:跳过创建Image,直接从流创建​​。

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

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

相关问题