Link to home
Start Free TrialLog in
Avatar of Salah a a a Al Jasem
Salah a a a Al JasemFlag for Kuwait

asked on

asp.net Placing an image on an image control Does it have to be a URL

asp.net Placing an image on an image control Does it have to be a URL

The following is working fine
Image1.ImageUrl = "~/data/" & ImageFileName  & ".gif"

The following is NOT working
Image1.ImageUrl = "C:\MySite\data\" & ImageFileName  & ".gif"

Is there a way to make asp image control takes a normal path e.g. (d:\temp\mypicture.gif)
Avatar of Big Monty
Big Monty
Flag of United States of America image

try using Server.MapPath, as you must use a relative path:

Image1.ImageUrl = "Server.MapPath("C:\MySite\data\" & ImageFileName  & ".gif" )
Avatar of Salah a a a Al Jasem

ASKER

I did that and it is giving error (.........is a physical path, but a virtual path was expected)

also documents says ( you cannot specify a path outside of the Web application)
ASKER CERTIFIED SOLUTION
Avatar of Big Monty
Big Monty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
check the documentation of Image.ImageUrl Property
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.image.imageurl(v=vs.110).aspx

the value of ImageUrl has to be accessible online, so in order to show the image correctly it got to be a valid path when browsing through internet.

"C:\MySite\data\" is referred to the server's path but not exactly exist in the machine/device that currently browsing the page.

"~/data/" is good enough to refer to the folder "data" in the root directory of the site.
I have created a virtual directory C:\MyVir

<asp:Image ID="Image1" runat="server" Width="896px" ImageUrl="~/MyVir/MyImg.gif" />
<asp:Image ID="Image1" runat="server" Width="896px" ImageUrl="/MyVir/MyImg.gif" />
<asp:Image ID="Image1" runat="server" Width="896px" ImageUrl="MyVir/MyImg.gif" />

none of the above is working, then I did many readings to find out why,  many said it is about permissions and security and web.config
I have tried many as I read

This one still working fine because (Data) folder is under the site folder
<asp:Image ID="Image1" runat="server" Width="896px" ImageUrl="~/Data/MyImg.gif" />

Is there any changes should be done to the web.config file to make virtual directory work?
Is there any changes should be done to the web.config file to make virtual directory work?
how would you create the virtual directory in IIS or in web.config?

what's the purpose behind to use virtual directory rather than a common directory?
virtual directories are more useful only in the way it helps organize things. Sure, you don't *need* them in this particular instance, but it does help make configuring things easier, as well as referencing them.

Make sure your new virtual folder has the same permissions as your regular web site, chance are you need to add the IUSR account
(how would you create the virtual directory in IIS or in web.config?)
using iis manager

(what's the purpose behind to use virtual directory rather than a common directory?)
Image1.ImageUrl    will not display image if it is the physical path, and mr (Big Monty) suggested to use virtual path


Mr Big Monty
one of your answers advice to create a virtual folder, and in the last answer you said (Sure, you don't *need* them in this particular instance), can you be more clear

I am aiming to display my images in Image1.ImageUrl, physical folder will not work, you suggested virtual.
all i was saying is that you could make it work w/out virtual dirs, as you just need to set up the proper permissions. It's easier WITH a virtual directory, so just go that route,
setting the physical directory to a virtual one enables asp image control to display the image
Many thanks for Big Monty and Ryan Chong for helping

I have closed the question,

Mr Big Monty, if there is a better way of displaying the image in the image control please explain, because I am not happy with the virtual directory, it allows every body to access my images.
if you really want to "hide" the image path, you probably need to show it with base64 encoded method (but there is concern for large amount of images / image file size).

more info:
Base64 encoding images
https://varvy.com/pagespeed/base64-images.html

>>it allows every body to access my images.
this was no mentioned in your original question, i would suggest to open another question for discussions if needed.