Image Formatters

Topics: ASP.NET Web API
Mar 30, 2012 at 3:53 PM

Hi,

First of all congratulations for open-sourcing! Really looking forward to following the progress of the project, and hopefully contributing!

I have a WebApi that needs to serve images. Sometimes these images are read from disk/db as byte[]s and sometimes they are rendered by the server into System.Drawing.Bitmaps.

Do you have any plans to support media type formatters for image types? If not, can you envisage how they might work in order for me to maybe contribute?

Would the two use-cases (byte[] vs Bitmap) have separate formatters? I think a general BinaryMediaTypeFormatter would be useful for many cases, accepting any mime type.

I've not thought about Requests, only Responses so far...

Coordinator
Mar 30, 2012 at 5:36 PM

You generally have a formatter per content type. A given formatter can support as many CLR types as it wants for that content type.

We don’t currently have any plans for an image formatter, but it’s pretty easy to write one. Take a look at the contact manager sample. It has a simple image/png formatter.

Daniel Roth

Mar 30, 2012 at 5:47 PM

Ah ok, so there'd be no interest in a generic image/binary formatter? Seems a little wasteful to have to write multiple identical binary formatters for different content types, when they all do exactly the same thing: serialize a binary array.

I have actually written a nnumber of image formatters already; a single one for each image type, with a base class providing the common behaviour which is what you're suggesting I think.

Thanks!

Coordinator
Mar 30, 2012 at 6:12 PM

One of the great things about HTTP is that there are a lot of useful media types out there. We can’t support them all, so our strategy is to support the most common formats out of the box and then provide a great model for the community to build whatever formatters they need. NuGet is a great way to share your formatter implementation with the community.

Still, images are very common. Do you want to open an issue in our Issue Tracker with this suggestion so folks can vote on it?

Sounds like you have landed on a reasonable approach. Note that you can also support multiple media types on a given formatter.

Daniel Roth