3

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.

5 Answers 5

8

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

2
  • This is what I was trying to say.
    – Mr. Will
    Mar 30, 2011 at 15:39
  • It works !! but it still give me the path ("C://folder/....."). How can I retrieve the end of my URL (/images/test.jpg)? Mar 30, 2011 at 16:03
0

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";
0

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.

0

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>
-1

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.

3
  • It doesn't display any Image. It finds an image for the winform application but not for ASP. Mar 30, 2011 at 15:34
  • I updated my answer. You will need to create an images directory in your web application and store your images there, or write some code that can stream the image from the C:\images folder via an HTTP URL.
    – Mr. Will
    Mar 30, 2011 at 15:35
  • I did it. I have a folder /images in my server. So when i put the URL viewPhoto.ImageURL = /images/myphoto.jpg It works but not anymore for my Winform application. The function Image.FromFile doesn't find any picture in /images/myphoto.jpg Mar 30, 2011 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.