syed Ahsan Jaffri
syed Ahsan Jaffri

Reputation: 1124

relative path of image to byte array in C#

i have relative path of an image as

~/image/noimage.jpg

i wants to read byte array to save in database if that organization's logo is not in db

public byte[] org_logo(int myOrgId)
    {
        byte[] photo ;
        var context = new mp_repositoryEntities();
        var query = from o in context.organizations 
                    where o.organization_id == myOrgId
                    select o.logo;            
        photo = query.FirstOrDefault<byte[]>();
        if (photo == null)
        {
            photo = File.ReadAllBytes("~/image/noimage.jpg");
        }
        return photo;
    }

when i am setting this path to asp image control then it is working fine.

logo.ImageUrl = "~/image/noimage.jpg";

any idea ?????

Upvotes: 12

Views: 25838

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062550

File.ReadAllBytes is not an asp.net api, so the leading ~ means nothing to it. Instead, try:

string path = HttpContext.Current.Server.MapPath("~/image/noimage.jpg");
photo = File.ReadAllBytes(path);

Upvotes: 24

Related Questions