Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

relative path of image to byte array in C#

Tags:

c#

asp.net

image

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 ?????

like image 284
syed Ahsan Jaffri Avatar asked Oct 20 '13 10:10

syed Ahsan Jaffri


1 Answers

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);
like image 61
Marc Gravell Avatar answered Sep 19 '22 15:09

Marc Gravell