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)
saljasAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
try using Server.MapPath, as you must use a relative path:

Image1.ImageUrl = "Server.MapPath("C:\MySite\data\" & ImageFileName  & ".gif" )
0
saljasAuthor Commented:
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)
0
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
Without seeing the code change you made, how would you expect me to offer help?

and yes, it does indeed need to be accessible to your web app. If it's not, it's quite simple to do so,  Simply set up a virtual folder (which can be located outside of your main web app, and then you can use the following code (you don't even need the call to Server.MapPath() !) to set the image:

Image1.ImageUrl = "/nameOfVirtualFolder/" & ImageFileName  & ".gif"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Ryan ChongCommented:
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.
0
saljasAuthor Commented:
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?
0
Ryan ChongCommented:
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?
0
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
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
0
saljasAuthor Commented:
(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.
0
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
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,
0
saljasAuthor Commented:
setting the physical directory to a virtual one enables asp image control to display the image
0
saljasAuthor Commented:
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.
0
Ryan ChongCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.