Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear sir,

Here I am working on a asp.net project. I stored my image path in database and image inside a folder named as image. here the image stored successfully but it could not load in an image control.
Here is the code.



C#
if (!string.IsNullOrEmpty(Convert.ToString(dr["imagepath"])))
         {
             Image1.ImageUrl = Convert.ToString(dr["imagepath"]);
         }



Plz help me for shut out this problem.
Thanks & Regards
Bigyan Ranjan Sahoo
Posted
Comments
coded007 28-Aug-14 8:07am    
do you have image in your specified path?
bigyan sahoo 28-Aug-14 21:37pm    
Yes, it is inside image folder in same project location.

If the imagepath column contains the name of a file inside the images directory, then it should be as simple as:
C#
Image1.ImageUrl = "~/images/" + Convert.ToString(dr["imagepath"]);


If the column contains the physical path of the file on the server, then it gets more complicated. Assuming everything is within the same physical folder structure, you might be able to use something like this:
C#
string physicalPath = Convert.ToString(dr["imagepath"]);
string imagesPhysicalPath = Server.MapPath("~/images/");

if (physicalPath.StartsWith(imagesPhyisicalPath, StringComparison.OrdinalIgnoreCase))
{
    string relativePath = physicalPath.Substring(imagesPhysicalPath.Length);
    image.ImageUrl = "~/images/" + relativePath.Replace('\\', '/');
}
 
Share this answer
 
You can use Server.MapPath(path) to get correct server path. Try this code.
if (!string.IsNullOrEmpty(Convert.ToString(dr["imagepath"])))
{
    Image1.ImageURL= Server.MapPath(dr["imagepath"].ToString());
}
 
Share this answer
 
v6
Comments
bigyan sahoo 28-Aug-14 8:42am    
I could not use Imagesource. can you explain briefly, I'm new on this platform.
jekin77 28-Aug-14 8:58am    
Oo, sorry - this example is for Windows application.
so, for ASP.NET you should try this

string getPath = dr["getPath"].ToString();
image1.imageURL= Server.MapPath(getPath);
Richard Deeming 28-Aug-14 9:01am    
Server.MapPath will return the local file-system path of the image on the server. You cannot use that path as the ImageUrl.
jekin77 28-Aug-14 9:06am    
it's correct , or i missunderstoot the location of image ?!?!
Richard Deeming 28-Aug-14 9:09am    
Server.MapPath("~/images/image1.jpg") will return something like C:\inetpub\wwwroot\images\image1.jpg.

That will generate an HTML <img> tag with the src attribute set to C:\inetpub\wwwroot\images\image1.jpg.

If the user happens to have a file in that path on their local system, that image will be displayed. It won't be the one you wanted to display, because that's on the server.

If the user doesn't have a file in that location on their local system, then the image will not display.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900