简体   繁体   English

检索图像“不支持URI格式”和“ FileNotFoundException”

[英]Retrieve image “URI formats no supported” & “FileNotFoundException”

I'm just trying to retrieve an image in my project. 我只是想在我的项目中检索图像。 But I am unable to make it work on the online server. 但是我无法使其在联机服务器上工作。

This code works fine on local server : 这段代码在本地服务器上运行良好

Image image = Image.FromFile("C:\\Users\\Nico\\Desktop\\Project\\MvcApplication1\\MvcApplication1\\Images\\sf.gif");

But I can't deploy it on my server. 但是我无法在服务器上部署它。

The following code gives the exception "FileNotFoundException" . 以下代码给出了异常 "FileNotFoundException"

Image image = Image.FromFile("\\Images\\sf.gif");

And this code gives "URI formats not supported" 并且此代码给出了“不支持URI格式”

Image image = Image.FromFile("http://www.mydomain.com/Images/sf.gif");

Any help is appreciated. 任何帮助表示赞赏。

Where on the server is the file? 文件在服务器上的哪个位置?

The first one works because you're referencing an existing file. 第一个有效,因为您引用的是现有文件。 The second one doesn't work likely because it can't find that file relative to whatever the current path is. 第二个文件可能不起作用,因为它找不到相对于当前路径的文件。 You'll probably want to be more explicit with the path. 您可能希望对路径更加明确。 The third one definitely won't work because that's not a file, it's a URL. 第三个绝对不起作用,因为这不是文件,而是URL。 (HTTP isn't a file system.) (HTTP不是文件系统。)

Is the "Images" folder in the root of the application? “图像”文件夹位于应用程序的根目录中吗? Try prepending the second version with a "~" to indicate that: 尝试在第二个版本之前加上"~"以表明:

Image image = Image.FromFile("~/Images/sf.gif");

If the "Images" folder can change from something more than just the root of the application, you can also use a config setting to indicate the location of the images. 如果“图像”文件夹不仅可以更改应用程序的根目录,还可以使用配置设置来指示图像的位置。 Something as simple as this: 像这样简单的事情:

<add key="imagesRoot" value="C:\Users\Nico\Desktop\Project\MvcApplication1\MvcApplication1\Images\" />

And then you can use the Path object to build a more complete path: 然后可以使用Path对象构建更完整的路径:

Image image = Image.FromFile(Path.Combine(ConfigurationManager.AppSettings["imagesRoot"], "sf.gif"));

For just the images folder of a website that might be overkill, but it's certainly flexible. 对于仅是网站的图片文件夹而言,这可能是过大的选择,但它肯定是灵活的。 And then you can point it to any folder via the config file whenever you need to change it or deploy it to a different server. 然后,您可以在需要更改或将其部署到其他服务器时,通过配置文件将其指向任何文件夹。

You can use Server.MapPath to let the code to detect the root folder of a website/application. 您可以使用Server.MapPath来让代码检测网站/应用程序的根文件夹。

Image image = Image.FromFile(Path.Combine(Server.MapPath("/Images"), "sf.gif"));

It would be worth to check 值得检查

Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\\"), Server.MapPath("/"). Server.MapPath(“。”),Server.MapPath(“〜”),Server.MapPath(@“ \\”),Server.MapPath(“ /”)。 What is the difference? 有什么区别?

Try 尝试

Image image = Image.FromFile(Server.MapPath("\\Images\\sf.gif")); 图片image = Image.FromFile(Server.MapPath(“ \\ Images \\ sf.gif”));

<!-- language: c# -->
Image image = Image.FromFile ("Images\\sf.gif");
//OR
Image image = Image.FromFile (@"Images\sf.gif");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM