c# - display image from byte array by api controller in angular js -
i have application server side asp.net. want display images stored array of bytes on ِdatabase api controller in app. angular js app. on server side, image stored in database follows:
[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); } and displayed in way.
[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 want. return file(myresult.toarray(), "image/jpeg"); } } how use method display images in api controller?
posting solution here per comments on op. solution serialize byte array json , return jsonresult effect of following;
[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 want. return json(new { myresult.toarray() }); } }
Comments
Post a Comment