为什么我无法加载此图片?

时间:2014-04-11 10:31:13

标签: c# image system.drawing

我在疯狂尝试用C#

检索jpg图像

这是我的班级:

namespace PdfReport
{
    // Classe base che rappresenta un PDF:
    public class iTextDocumentBase : MyManagerCSharp.ManagerDB
    {
        // Oggetto MemoryStream che contiene il PDF:
        protected System.IO.MemoryStream _risultatoMemoryStream = new System.IO.MemoryStream();

        protected iTextSharp.text.Document _document;   // Generico documento di iText valido per qualsiasi formato di output
        protected iTextSharp.text.Chapter _capitolo;    // Contenitore per un insieme di Section opportunamente sequenziate
        protected iTextSharp.text.Section _section;     // Parte del Document costituita da un insieme di Paragraph opportunamente disposti

        protected System.Drawing.Image _backgroundImg = System.Drawing.Image.(Server.MapPath("~/Path/Relative/To/Root.jpg"));

        ..........................................................
        ..........................................................
        ..........................................................
}

正如你所看到的,我试图通过这一行获得图像:

protected System.Drawing.Image _backgroundImg = System.Drawing.Image.(Server.MapPath("~/Path/Relative/To/Root.jpg"));

问题是我收到以下错误消息:

Error 78  The name 'Server' does not exist in the current context C:\Develop\EarlyWarning\public\Implementazione\Ver2\PdfReport\iTextDocumentBase.cs  43  79  PdfReport
Error 77  Identifier expected C:\Develop\EarlyWarning\public\Implementazione\Ver2\PdfReport\iTextDocumentBase.cs  43  78  PdfReport

为什么呢?我该怎么做才能解决?

1 个答案:

答案 0 :(得分:0)

此问题似乎与图片无关。首先,这不是有效的C#语法:

System.Drawing.Image.(Server.MapPath("~/Path/Relative/To/Root.jpg"));

你不能只在(之后加.。你需要在那里给出一个方法名称(在这种情况下可能是FromFile)。

至于“名称'服务器'在当前上下文中不存在”错误,这种情况正在发生,因为您要么缺少using类的程序集引用或Server语句。 Server.MapPath()是一种适用于ASP.NET项目的方法。你在从事ASP.NET项目吗?