C#中图像与字节数组的相对路径

我有相对的图像路径

~/image/noimage.jpg

我想读取字节数组以保存在数据库中,如果该组织的徽标不在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;
    }

当我将此路径设置为asp图像控件时,它工作正常.

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

任何的想法 ?????

File.ReadAllBytes不是asp.net api,所以前导〜对它没有任何意义.相反,尝试:

string path = HttpContext.Current.Server.MapPath("~/image/noimage.jpg");
photo = File.ReadAllBytes(path);
翻译自:https://stackoverflow.com/questions/19476299/relative-path-of-image-to-byte-array-in-c-sharp

转载注明原文:C#中图像与字节数组的相对路径