-1

How to display an Image if I have the Physical Path of that image in webpage in a particular place?

I tried like below but not working.

            img = rdr["ImagePath"].ToString();
            img = Server.MapPath(img);
             Image1.ImageUrl=img;
Naresh
  • 657
  • 3
  • 16
  • 35

4 Answers4

4

I believe it would be as below:

ImageInstanceName.ImageUrl = Server.MapPath(@"/Images/image.jpg");

Or if you were wanting to do it in markup then @Bibhu's solution should work.

Here is the link to MSDN library on Server.MapPath.

radu florescu
  • 4,315
  • 10
  • 60
  • 92
hdougie
  • 344
  • 4
  • 9
3

You can create an Handler which returns the binary output of that image. and in your image tag refer to this handler

eg. <img src="yourImagehandler.ashx"/>

ajay_whiz
  • 17,573
  • 4
  • 36
  • 44
2

In C# -

Image1.ImageUrl = Server.MapPath("c:\path\to\file.jpg");
Paul McLean
  • 3,450
  • 6
  • 26
  • 36
  • 3
    -1 "The MapPath method maps the specified relative or virtual path **to** the corresponding physical directory on the server" - your code doesn't work. – Ben Foster Jun 28 '11 at 11:21
  • Thank you but I'm trying like this `Image1.ImageUrl = Server.MapPath(img);` But it's not showing the image. – Naresh Jun 28 '11 at 11:38
  • See the link I posted as a comment to your question (this question has been asked before). The above code doesn't work so you shouldn't upvote it. – Ben Foster Jun 28 '11 at 16:32
  • Relative path is in most cases the one good for that. hdougie's answer is more suitable – radu florescu Dec 23 '11 at 12:04
2
  <asp:Image id="Image1" runat="server"
       AlternateText="Image text"
       ImageAlign="left"
       ImageUrl="images/image1.jpg"/>
Bibhu
  • 4,053
  • 4
  • 33
  • 63