C# 通过api控制器在angular js中显示字节数组中的图像

C# 通过api控制器在angular js中显示字节数组中的图像,c#,asp.net,angularjs,api,cordova,C#,Asp.net,Angularjs,Api,Cordova,我有一个服务器端是asp.net的应用程序。我想通过应用程序中的api控制器在ِ数据库中显示以字节数组形式存储的图像。这是一个有角度的js应用程序。 在服务器端,映像存储在数据库中,如下所示: [HttpPost] public ActionResult Create(House house,HttpPostedFileBase Photon) { if (ModelState.IsValid) { house.Id = Guid.NewGuid();

我有一个服务器端是asp.net的应用程序。我想通过应用程序中的api控制器在ِ数据库中显示以字节数组形式存储的图像。这是一个有角度的js应用程序。 在服务器端,映像存储在数据库中,如下所示:

[HttpPost]
public ActionResult Create(House house,HttpPostedFileBase Photon)
{
    if (ModelState.IsValid)
    {
        house.Id = Guid.NewGuid();

        if (Photon != null && Photon.ContentType.StartsWith("image"))
        {
            mImage img = new mImage
            {
                Photo = new byte[Photon.ContentLength],
                PhotoType = Photon.ContentType,
                photoName = Photon.FileName,
                HouseId = house.Id
            };
            Photon.InputStream.Read(img.Photo, 0, Photon.ContentLength);
            db.mImages.Add(img);
        }
        var area = db.Areas.Find(house.AreaId);
        house.visit = 0;
        db.Houses.Add(house);
        db.SaveChanges();

        return RedirectToAction("Index");
    }

    ViewBag.AreaId = new SelectList(db.Areas, "Id", "name", house.AreaId);
    return View(house);
}
并以这种方式显示

[AllowAnonymous]
public ActionResult ShowPhoto(Guid id)
{
    using (var db = new myDB())
    {
        var p = db.mImages.Find(id);
        System.IO.MemoryStream myMemStream = new System.IO.MemoryStream(p.Photo);
        System.Drawing.Image fullsizeImage = System.Drawing.Image.FromStream(myMemStream);
        System.Drawing.Image newImage = fullsizeImage.GetThumbnailImage(150, 150, null, IntPtr.Zero);
        System.IO.MemoryStream myResult = new System.IO.MemoryStream();
        newImage.Save(myResult, System.Drawing.Imaging.ImageFormat.Jpeg);  //Or whatever format you want.

        return File(myResult.ToArray(), "image/Jpeg");
    }
}

如何使用此方法在api控制器中显示图像?

根据OP上的注释在此处发布解决方案。解决方案是将字节数组序列化为json,并将其作为
JsonResult
返回,效果如下:

[AllowAnonymous]
public ActionResult ShowPhoto(Guid id)
{
    using (var db = new myDB())
    {
        var p = db.mImages.Find(id);
        System.IO.MemoryStream myMemStream = new System.IO.MemoryStream(p.Photo);
        System.Drawing.Image fullsizeImage = System.Drawing.Image.FromStream(myMemStream);
        System.Drawing.Image newImage = fullsizeImage.GetThumbnailImage(150, 150, null, IntPtr.Zero);
        System.IO.MemoryStream myResult = new System.IO.MemoryStream();
        newImage.Save(myResult, System.Drawing.Imaging.ImageFormat.Jpeg);      //Or whatever format you want.

        return Json(new { myResult.ToArray() });
    }
}

我是否遗漏了什么,或者您是否可以在
img
标签中引用is作为URL,就像这样<代码>(替换为实际guid)?我在asp.net mvc控制器中执行此操作,但我希望通过api控制器在cordova应用程序中显示图像,我如何使用保存在数据库中的图像,它们像字节数组一样保存?在这种情况下,如果使用WebApi返回字节数组,则方法的返回类型为
byte[]
。如果您是从普通控制器提供服务,那么您需要序列化字节数组(通常为JSON),并将其作为
JsonResult
返回。还是我误解了这个要求?非常感谢你,我的朋友,我用json解决了这个问题