简体   繁体   中英

How to load an image from the server in a web application in C#

I actually have a C# winform application which load images from my computer C://images/... with the Image object and the function Fromfile.

Image.FromFile(Path);

but in my web application (ASP)

<asp:Image ID="viewPhoto" runat="server" Width="550px" Height="400px"/>

I use the attribute ImageURL.

viewPhoto.ImageURL = Path

But the problem is that it doesn't find the correct path because with this way. The path will be http://localhost:3656/C://images....

I would like to load an image directly from my server to have the correct path for both of my applications.(web ASP and winform)

Image.FromFile(/images/myimage.jpg)

This actually doesn't work because the program doesn't find any photo in this path.

First of all i think that images you are trying to show are not in web application folder / virtual directory. Move images folder to your web application folder and then use:

Page.ResolveClientUrl("images/test.jpg");

or for server side:

Server.MapPath("images/test.jpg");

If you dont want to move images to your web folder then your only choice is to write HttpHandler which will read images from C:\\images folder and transmit it to the client. This will also require some specific permissions for your web app IIS user to access some folder outside the web app scope.

You can see the sample of HttpHandler here: Thumbnailer HTTP Handler

I found the quickest way to resolve this was to assign the image path as a css attribute. The only downside of this is that if you are looking to display a large amount of images in a repeater you would not want to use this, but if you have one or two images you can use this.

<style type="text/css">
#errorImage{
background: url(/path/path/images/image.jpg);
background-size: contain;
height: 100px;
width: 100px;
}
</style>


<div id="errorImage"><div>

ASP.Net image simply renders the ImageURL to the client browser, this will then try to load that image from the location specified, this typically needs to be a resource available via your website.

Try moving your images inside your web root then you can access them like :

viewPhoto.ImageUrl = "~/images/yourimage.jpg";

As im sure you know your <asp:Image> element is actually creating an html <img> element that will instruct the browser to request the image from the browser.

Image.FromFile will give you an image object on the server side but it won't be much use for your clients browser.

As @HABJAN said, you need to map the path from your local file to a relative web URI and to do that you can use Server.MapPath. However your images will need to be inside your asp.net project folder which is asccessible by the web server in order for it to serve the file.

If you really want to share the paths in some sort of shared constant assembly I suggest you make your windows application work with relative paths and duplicate the images folder structure for both the windows app and the web app.

Using the path you are using now (http://localhost:3656/C://images....), the application will look for a /C://images... folder in your app directory on the web server. I believe you should create an images directory in your application folder and server the images from there so that you could reference the images using a relative path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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