How to take an image in a folder at the root of the project?

Asked

Viewed 141 times

2

I’m trying to get an image that is inside a folder in the project to use with "Default" if any user does not add the photo at the time of registration, but when trying to pass the url, the error because I.o cannot find the same one.

Follow image location in the project:

inserir a descrição da imagem aqui

Per line of code, I managed to pass like this:

if (usuario.imagem.Length == 0)
{
    string path = @"C:\Pro\028\webIRMA\Content\dist\img\avatarPadrao.png";

    using (Image image = Image.FromFile(path))
    {
        using (MemoryStream m = new MemoryStream())
        {
            image.Save(m, image.RawFormat);
            byte[] imageBytes = m.ToArray();;
            Session["ImagePerfil"] = imageBytes;
        }
    }
}
else
{
    Session["ImagePerfil"] = usuario.imagem;
}

But this is localhost, when it is passed to another server, in the case of production can change the site of the project,then tried something as below, but without success:

string path = @"~/webIRMA/Content/dist/img/avatarPadrao.png";

1 answer

1


Use the HttpContext.Current.Server.MapPath

string path = HttpContext.Current.Server.MapPath("Content\dist\img\avatarPadrao.png");

It is necessary to add the reference to the System.Web to use it.

Browser other questions tagged

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