Problem in displaying image in the web page - c#

Trying to display an image in the webpage tried like below.
Image1.ImageUrl = "C:\\Users\\naresh\\documents\\visual studio 2010\\Projects\\Vacancy\\Vacancy\\Files\\3\\710d7a38-5a4d-44fc-883f-5996150ba507.jpg";
Image2.ImageUrl = "C:\\Users\\naresh\\Desktop\\710d7a38-5a4d-44fc-883f-5996150ba507.jpg";
Second one Worked but first one didn't. I think the problem is with the spaces in the path. How can I resolve this problem?

Why do you not use a relative path to the location of your site? The path will be shorter and when you will port the web site on production server the image will still work (if the image is port to the server)

May be with the Vacancy which you have given two times. Check it, if its the mistake.

You can try to replace the spaces with %20.
But I wonder if that's the problem because the image is located at your local disk. Is this webpage only running on your local machine? Does the image exist?

Put quoted quotation marks into the string, e.g.:
Image1.ImageUrl = "\"C:\\Users\\naresh\\documents\\visual studio 2010\\Projects\\Vacancy\\Vacancy\\Files\\3\\710d7a38-5a4d-44fc-883f-5996150ba507.jpg\"";

Try giving the virtual directory path (create a virtual directory for the image folder. If it is in the root itself use the http://systemName/virtualDirectoryName/ImageFolder/image1.jpg) instead of physical path as follows:
Image1.ImageUrl=http://systemName/virtualDirectoryName/ImageFolder/image1.jpg
Hope this helps..

You are doing anyway very bad things in there. You should use Server.MapPath to resolve dynamically an IIS relative path into the file system path and you should not, never ever, load an image from the user's folders.
Put the image inside a subfolder in your web application, like for example create a folder called images at the same level where your aspx pages are.

Related

How to display Image from outside project directory

I have an Image Path which is of D: drive and I want to display that Image on my Image Control of asp.net.So, how to provide file path to Image.ImageUrl?
"D:/Folder/001_001.jpg"
I tried with server.MapPath() method but doesn't work.
If you're deploying in IIS, you could create a virtual directory Images that would point towards D:/Folder, then you could set your ImageUrl to ~/Images/001_001.jpg.jpg.
An alternative solution could be to create a symlink between the two folders. You would run something like the following in cmd mklink /D ""D:/Folder" "[YouProjectPath]/Images", then set your ImageUrl to ~/Images/001_001.jpg.jpg. I would stay away from this though as it would only work on a machine where the project lives in the exact same path as yours.
You can make local path as following "file:///D:/Folder/001_001.jpg".
However, it's strongly not recommended to use this practice even if your site is planned to stay on your local computer only and will never been deployed to real Internet. You should copy / upload the file to your site's subdirectory 'Images' and then use the relative path '~/Images/001_001.jpg'.

How to Check URL Path Of Images

Suppose I have an Image located at “C:\A\mah” Or “E:\A\mah” my question is can I check the URL Path of such an image? If yes then how? Which will lots help me to put right URL path in my Asp.Net Web Application.
If those images are not within your project folder, you may need to create a virtual folder (say myImages) and then access images.
#Url.Content("~/myImages/imgName.ext")
Otherwise you could use
#Url.Content("~/path_relative_to_main_folder/imgName.ext")
Never use exact locations. It'll give you a headache. Instead, put a folder in your site's directory somewhere and then reference it with relative paths. For example:
<img src="#Url.Content("~/Images/ABC.png")" />
This means it is looking for the Images folder in your project's root directory.

Get relative path from OpenFileDialog in C#

I am a beginner at programing and I´m writing my second Prog.
I have a Question about how to get a relative path to my application Startup path.
The Program reads an .xml-file that has a path of a .jpg stored in it. It creates a Picturebox for every path and loads the respective image.
The problem I have is, that I have the images in my Dropbox to be able to use the Program on any PC that has Dropbox. When I use the OpenFileDialog on my main PC and save the Path of the .jpg to the xml it won´t work on my laptop because the Dropbox folder is on another drive as on my main PC.
Does anyone have an idea, how to get around this Problem?
To solve your issue, This will get the current location of your application
Directory.GetCurrentDirectory()
You can do a simple replace of the path.
Example :
String JPG_Path_Relative = openFileDialog1.FileName.Replace(Directory.GetCurrentDirectory(),"")
When the dropbox folder is in the default location (User-folder) you can use this to get your path:
string userFolderPath= Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
This will give C:\Users\USERNAME (drive may be different).
Then simply add the rest of the path to your image folder.
string imageFolderPath = userFolderPath + #"\Dropbox\Imagefolder";

c# picturebox point to filesystem

i have a picture box in my win forms app. the question is, how can i make the image load from a directory within that application,
for example, i have a program and it lies under Debug directory. Now i have an image located under Debug/source/images/logo.png. I dont want to make use of resources file (embedded within the assembly)
How can i point it towards that path, instead of getting the image/picture from the local resources file.
Try this
String.Format(#"{0}\myDesiredPath\MyFile.MyExtension",System.Reflection.Assembly.GetExecutingAssembly().Location);
There are a number of ways to do this, you can use the ImageLocation property or the Load method to specify the path just take a look at the documentation here

full path of a folder in c#

i need to get the full folder path in a windows project using c#.I tried with path.getFulPath(filename).bt it returns the application path+filename.how can i get the actual path like "D:\eclipse_files\ads_data"?
A relative path such as myfile.txt is always resolved in relation to the current working directory.
In your case the current working directory seems to be D:\eclipse_files\ads_data so your relative file path gets resolved to D:\eclipse_files\ads_data\myfile.txt when you call Path.GetFullPath.
To solve the problem, either make sure that you start with an absolute path from the beginning, or, that your working directory is set correctly.
You can get/set the working directory using the Directory.GetCurrentDirectory and Directory.SetCurrentDirectory methods.
Your question is not very clear, but I think you're looking for this:
string path = Path.GetDirectoryName(filename);
If I have understood correctly, you have a filename, for example 'doc.txt', and you want to have a method to return the full path of this file regardless of where the application runs from?
If this is what you ask it is not possible. Have you considered that there might be several files called 'doc.txt' on your harddrives?
The best you can hope to do it to search all harddrives, and return a list of all files found with the same name, but that will just be ridicously slow.

Categories