FileNotFoundException为什么要尝试打开图像

时间:2012-05-17 01:32:30

标签: c# image filenotfoundexception

我的项目文件夹中有一张图片: 让我们说: ~/App_Themes/Default/images/SomeImage.png

我想将此图像加载到System.Drawing.Image中,我该怎么做?

如果我尝试使用Image类的FromFile方法:

Image img = Image.FromFile("~/App_Themes/Default/images/SomeImage.png", true);

我得到FileNotFoundException

我已经阅读了一些建议将图像存储到服务器中,但这不是一个选项。有没有办法将其加载到图像中?

1 个答案:

答案 0 :(得分:0)

您似乎使用相对路径而不是文件路径来定位图像。试试这个:

var path = @"~/App_Themes/Default/images/SomeImage.png";

using (Image img = Image.FromFile(Server.MapPath(path)))
{
   do some stuff
}