How to locate an image without informing the absolute path?

Asked

Viewed 1,326 times

3

How to locate an image without entering the absolute path?

Image img = System.Drawing.Image.FromFile("~/Content/images/SemFoto.jpg");

I tried several ways and could not capture the image without informing the absolute path. The code line above is in a action within a controller.

  • 2

    I don’t understand. If you need to upload an image, you need to inform the physical path on the server.

  • You meant, "How to locate an image by informing the relative path instead of the absolute path"?

  • @Ciganomorrisonmendez need to pick up the image through the logical path from where the image is. Not the physical path referring to the disc.

    • Physical Path (Physical Path): is a path of the OS type that points to a path or file on the disk. * Logical path (Logical Path): is a web path that is relative to the server root.
  • Well, then @bigown’s answer is what you’re looking for.

  • @Gypsy omorrisonmendez Onesendai the response of the bigown solved. Thanks for the force.

Show 1 more comment

1 answer

5


I think this is what you want:

Image img = System.Drawing.Image.FromFile(
           System.Web.HttpContext.Current.Server.MapPath("~/Content/images/SemFoto.jpg"));

Documentation.

There are some other ways to do this:

System.Web.Hosting.HostingEnvironment.MapPath(path);

I put in the Github for future reference.

Documentation.

The exact way of use depends on how you need it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.