0

I would like to return an image that is dynamically created instead of retrieving from a database or from file. Let's say I have a simple ZipCode class. I would like to have a property that would return an image representation of the zipcode. The JSON returns "ZipAsImage":"System.Drawing.Bitmap". Yes I am new to the programming in the web world. Yes I have looked for other examples but they seemed to only return an image. Thanks for any help.

public class ZipCode
{
    public int ZipCodeId { get; set; }
    [NotMapped]
    public Image ZipAsImage
    {
        get
        {
            // Create a blank iamge for now to test.
            return new Bitmap(50, 50);
        }
        set { }
    }
}

My ApiController class

public class ZipCodesController : ApiController
{
    private MyContext db = new MyContext();

    // GET api/ZipCodesController
    public IEnumerable<ZipCode> GetZipCodes()
    {
        return db.zipcodes.AsEnumerable();
    }

    // GET api/ZipCodesController/5
    public ZipCode GetZipCode(int id)
    {
        ZipCode zipcode = db.ZipCodes.Find(id);
        if (zipcode == null)
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        }

        return zipcode;
    }
...

My Controller class

public class ZipCodesController : Controller
{
    private MyContext db = new MyContext();

    //
    // GET: /ZipCodes/

    public ActionResult Index()
    {
        var zipcodes = db.ZipCodes.Include(z => z.State);
        return View(zipcodes.ToList());
    }

    //
    // GET: /ZipCodes/Details/5

    public ActionResult Details(int id = 0)
    {
        ZipCode zipcode = db.ZipCodes.Find(id);
        if (zipcode == null)
        {
            return HttpNotFound();
        }
        return View(zipcode);
    }
...
3
  • You need to become familiar with the classes in the System.Drawing namespace. If you do not want to get the image from a file or database, then you will have to draw it using code. You can then return it from either an MVC Controller or an ApiController. Commented Feb 13, 2013 at 12:30
  • Hello danludwig, Yes I am familiar with drawing I just put a blank image in for testing. My question is how to return the image with the data of ZipCodeId. How do I modify my GetZipCodes and ActionResult Index routines to do this? Thanks. Commented Feb 13, 2013 at 14:20
  • So you want to return binary image content and text data simultaneously, in the same response? Commented Feb 13, 2013 at 14:47

1 Answer 1

0

I dont undersatnd the point where you are showing the MVC Contoller.Am i missing something there? However For Webapi (ApiController) You can re write your Method as follows. This should pass the enitre Object in one response.

 public HttpResponseMessage GetZipCode(int id)
{
    ZipCode zipcode = db.ZipCodes.Find(id);
    if (zipcode == null)
    {
        return Request.CreateResponse(HttpStatusCode.NotFound)
    }

    return Request.CreateResponse(HttpStatusCode.OK,zipcode);
}

Sending Binary Images. You need to set Straemcontent and content-type. I found a link that can help you with that Images with Webapi . Look through the entire thread . You might figure out quickly whats missing...

Sign up to request clarification or add additional context in comments.

2 Comments

My JSON response file shows "ZipAsImage":"System.Drawing.Bitmap". Which of course is the string representation. My current code produces the same. How do I get the binary data instead? Thanks.
See my edit. I posted a link that might be of some help. There is also a suggestion of using HttpHandler as well .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.