problem loading image at runtime - c#

For some strange reason my picture is not loading at runtime:
string path = Server.MapPath("./abc.jpeg");
Response.Write("the path is:");
Response.Write(path);
img_ProfilePic.ImageUrl = path;
As you see from above code, I have verified that the path is correct.
Also the image is only 20 KB and is JPEG.
My environment is VS 2008 C#
Thanks

Server.MapPath returns the physical (file system) path.
Image.ImageUrl requires a virtual path (or relative/absolute URL). You should use it like this for example:
img_ProfilePic.ImageUrl = "~/images/abc.jpeg";
img_ProfilePic.ImageUrl = "../abc.jpeg";
img_ProfilePic.ImageUrl = "http://www.host.com/abc.jpeg";
More on web project paths (check the Server Controls section which is specific to your problem):
http://msdn.microsoft.com/en-us/library/ms178116.aspx

Right click on the "broken image" icon, and copy and paste the path into your browser. Do you get the image, a "broken image" or a 404?
Are you testing locally?

Replace string path = Server.MapPath("./abc.jpeg"); with string path = Server.MapPath("~/abc.jpeg");

Related

Saving images relative to content directory where my site is running in IIS

I want to save images uploaded from a asp mvc page to the content/img/ folder which is relative to where my site is running in IIS. But I don't want to hard code the location on my file system to a Absolute path as this could change easily. This is the code I'm using to save them.
public static string SaveCompanyLogoImage(HttpPostedFileBase file)
{
var newFileName = GetNewCompanyLogoFileName(file.FileName);
file.FileName = newFileName;
file.SaveAs(//relative-location-here);
return Path.GetFileNameWithoutExtension(last);
}
Hope this explains my issue!
Thanks for any assistance
Use Server.MapPath("~/MyImagesFolder")
http://msdn.microsoft.com/es-es/library/system.web.httpserverutility.mappath.aspx

image control unable to display image

my code is:
string filename = FileUploader.PostedFile.FileName.Substring(fuImage.PostedFile.FileName.LastIndexOf("\\") + 1);
if(fuImage.HasFile)
{
FileUploader.SaveAs(Server.MapPath("Modules/NewUserProfile/UserPic/" + filename));
imgUser.ImageUrl = Server.MapPath("Modules/NewUserProfile/UserPic/" + filename);
}
imgUser is id of asp:Image Control.Image is uploaded in desire folder but its not display image in image control.What i am doing wrong here? Is there any postback issue.Thanks.
Use relative path for ImageUrl property.
imgUser.ImageUrl = "Modules/NewUserProfile/UserPic/" + filename;
OR use root operator ~ if Modules folder is located at root of web-app.
imgUser.ImageUrl = "~/Modules/NewUserProfile/UserPic/" + filename;
To run this code please understand the following things:
Server.MapPath() is used for getting physical Path like: D:/Img/Upload/..
so it is good idea to get path for saving image.
But in the case when you are getting the image for binding it to image control then you must have to use virtual path instead of Physical path.
Virtual path like: localhost/demo/upload/myimage.jpg.
I usually debug problems like this by first doing a view-source with your browser, and making sure the URL in the HTML source is what you expect. Then try copying the url that shows up in your view-source and pasting it into the address bar of your browser, and see if it shows up.
I think you should wait for the image to completely upload and then set the Image path to the image.
Also after rendering into the Browser use FireBug in Mozilla or Chrome Inspect Elemnt to check whether the Image has been rendered properly by looking at the image. If it broken then your image path is wrong. Try to give relative path and check.
newpic.ImageUrl = Page.ResolveURL("~/Pictures")+filename;
Server.MapPath returns physical drive location which is not useful when you are assigning to the imageurl.

How can I load an image from a specific directory in C#?

I would like to load an image from a directory "../MyAppFolder/Logos".
My code:
Bitmap bmp = new Bitmap(#"/Logos/bitmap.bmp");
pictureBox1.Image = bmp;
This code doesn't work. When I use (#"/Bitmapx.bmp") it works, but when I want to load an image from a deeper directory I get an error message.
What am I doing wrong?
The leading slash targets the current drive root. Use the realative path...
Bitmap bmp = new Bitmap(#"Logos/bitmap.bmp");
..Or one of the many Path. methods to resolve the full path that you want.
and yes I know my example above targets the current working path... that would be why I added the above comment. And for the pointless downvoter you might like to learn that current versions of Windows don't care which slash you use.
Try this:
string myLogo = System.IO.Path.Combine(Application.StartupPath, #"Logos\bitmap.bmp");
Bitmap bmp = new Bitmap(myLogo);
pictureBox1.Image = bmp;
And make sure your Logos folder is in your application root folder.
The first character of your path is / which makes this path relative to the root level of the drive on which the current working directory lives. But you probably want a relative path so just remove the initial /.
What's more, relative paths are relative to the working directory. But the working directory is not necessarily the application directory. For example, if you navigate in a file dialog that can change your working directory.
If I were you I would probably pre-pend the path with the app directory and make it a fully-specified absolute path, exactly as HABJAN suggests.

Access folders in root directory

Hi I am developing an asp.net web application. I have to access one of the image in images folder in root directory. I am using following code in my code behind file.
string imageDirectory = HttpContext.Current.Server.MapPath("~/images/");
string imageUrl = imageDirectory + "/img1.bmp";
This works fine in my local machine. My question is does this code work when I move my application to production ?
It should as long as you have an application root/virtual directory for your site.
Also, you can combine these two lines into:
string imageUrl = HttpContext.Current.Server.MapPath("~/images/img1.bmp");
If you're thinking of putting imageUrl into an <img> tag, then no, it won't work. Server.MapPath will return your file or directory as a local Windows file/directory name, so something like "C:\WebRoot\MyWebApplication". If you send this to the browser, obviously, the browser won't pick up the image.
What you can do is something like:
string imageUrl = ResolveClientUrl("~/images/myImage.gif");

File.Exists using the wrong root path?

In my c# class I wrote I have a photo property that returns the photo source if the image exists (nothing or default image otherwise). In my code I use:
public string Photo
{
get
{
string source = "~/images/recipes/" + id + ".jpg";
if (File.Exists(source))
return "~/images/recipes/" + id + ".jpg";
else
return "";
}
}
If I get the FileInfo() information for this image I see that I tries to find this image in the following directory: C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\~\images\recipes
Of course the image is not located in that directory and File.Exists is returning me the wrong value. But how can I fix this?
Try this:
if(File.Exists(System.Web.HttpContext.Current.Server.MapPath(source)))
You need to map the relative path back to a physical path:
string source = HttpContext.Current.Server.MapPath("~/images/recipes/" + id + ".jpg");
You'll have to use:
Server.MapPath(source)
As you can not be 100% sure where the code will be running from, ie. it will be different in development and on a production server. Also are you sure ~/ works in windows? Wont that just be interpreted as a directory named ~? Unless thats what you want.

Categories