将图像加载到asp.net应用程序中

时间:2016-02-12 08:50:18

标签: c# asp.net image iis directory

我遇到了一个我无法处理的障碍,我已经完成了很多与之相关的话题。我是ASP.NET的初学者,我想将图片加载到.aspx页面,但不知怎的,它永远不会渲染。我使用VS 2015,在项目的属性中,在选项卡Web上,选择了IIS Express。我知道浏览器无法从本地磁盘读取,但我无法使用虚拟目录进行操作。还有什么我也无法创建一个新的(我在管理员帐户下运行VS)。

根据此主题中的其他帖子,在applicationhost.config中,我尝试修改与虚拟路径相关的物理路径:

         <sites>
            <site name="WebSite1" id="1" serverAutoStart="true">
                <application path="/">
                    <virtualDirectory path="/" physicalPath="C:\Temp\images" />
                </application>
                <bindings>
                    <binding protocol="http" bindingInformation=":8080:localhost" />
                </bindings>
            </site>
            <siteDefaults>
                <logFile logFormat="W3C" directory="%IIS_USER_HOME%\Logs" />
                <traceFailedRequestsLogging directory="%IIS_USER_HOME%\TraceLogFiles" enabled="true" maxLogFileSizeKB="1024" />
            </siteDefaults>
            <applicationDefaults applicationPool="Clr4IntegratedAppPool" />
            <virtualDirectoryDefaults allowSubDirConfig="true" />
        </sites>

在代码背后,我尝试了很多东西,比如:

try
            {
                //bitmap i Graphics
                Bitmap bmp = new Bitmap(200, 200);
                {...}
                string url = @"C:\Temp\images\test.gif";
                string url1 = "~/test.gif";
                bmp.Save(url,ImageFormat.Png);
                bmp.Dispose();
                g.Dispose();
                imgTheImage.Visible = true;
                imgTheImage.ImageUrl = url1;
                lblErros.Text = "";
            }

然而,它并没有奏效。我的意思是图像没有被渲染。任何人都可以帮我实现吗?如何指向与物理路径对应的虚拟目录,以便加载文件?该文件确实存在,因为我在运行时使用GDI +创建它并在C:\Temp\images中显示。我还没有真正熟悉这个IIS,所以任何帮助都会受到赞赏。

如果我忘了写任何重要的事情,感谢和抱歉!:)

修改 当我将url变量更改为项目文件夹中的@"C:\Users\pablo\Documents\Visual Studio 2015\Projects\GDI_Asp\GDI_Asp\test.gif"时,这是有效的。因此,似乎我没有改变我的虚拟目录的物理路径,但如果用手做出的更改不起作用,我就不知道如何做到这一点。

0 个答案:

没有答案