Created
November 2, 2011 21:05
-
-
Save einarwh/1334920 to your computer and use it in GitHub Desktop.
A custom HTTP handler to produce Pix-It images.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PixItHandler : IHttpHandler | |
{ | |
public bool IsReusable | |
{ | |
get { return true; } | |
} | |
public void ProcessRequest(HttpContext context) | |
{ | |
string json = ReadJson(context.Request.InputStream); | |
var data = ToPixItData(json); | |
WriteResponse(context.Response, ToBuffer(CreateImage(data))); | |
} | |
private static PixItData ToPixItData(string json) | |
{ | |
JObject o = JObject.Parse(json); | |
int size = o.SelectToken("pixelSize").Value<int>(); | |
int wide = o.SelectToken("pixelsWide").Value<int>(); | |
int high = o.SelectToken("pixelsHigh").Value<int>(); | |
JToken bg = o.SelectToken("background"); | |
Color? bgColor = null; | |
if (bg != null) | |
{ | |
string bgStr = bg.Value<string>(); | |
bgColor = ColorTranslator.FromHtml(bgStr); | |
} | |
JToken payload = o.SelectToken("payload"); | |
var dict = new Dictionary<Color, IEnumerable<Pixel>>(); | |
foreach (var token in payload) | |
{ | |
var list = new List<Pixel>(); | |
foreach (var xyArray in token.SelectToken("pixels")) | |
{ | |
int x = xyArray[0].Value<int>(); | |
int y = xyArray[1].Value<int>(); | |
list.Add(new Pixel(x, y)); | |
} | |
string cs = token.SelectToken("color").Value<string>(); | |
Color clr = ColorTranslator.FromHtml(cs); | |
dict[clr] = list; | |
} | |
return new PixItData(wide, high, size, dict); | |
} | |
private static Image CreateImage(PixItData data) | |
{ | |
int width = data.PixelSize * data.PixelsWide; | |
int height = data.PixelSize * data.PixelsHigh; | |
var image = new Bitmap(width, height); | |
using (Graphics g = Graphics.FromImage(image)) | |
{ | |
if (data.Background.HasValue) | |
{ | |
Color bgColor = data.Background.Value; | |
using (var brush = new SolidBrush(bgColor)) | |
{ | |
g.FillRectangle(brush, 0, 0, | |
data.PixelSize * data.PixelsWide, | |
data.PixelSize * data.PixelsHigh); | |
} | |
} | |
foreach (Color color in data.Data.Keys) | |
{ | |
using (var brush = new SolidBrush(color)) | |
{ | |
foreach (Pixel p in data.Data[color]) | |
{ | |
g.FillRectangle(brush, | |
p.X*data.PixelSize, | |
p.Y*data.PixelSize, | |
data.PixelSize, | |
data.PixelSize); | |
} | |
} | |
} | |
} | |
return image; | |
} | |
private static string ReadJson(Stream s) | |
{ | |
s.Position = 0; | |
using (var inputStream = new StreamReader(s)) | |
{ | |
return inputStream.ReadToEnd(); | |
} | |
} | |
private static byte[] ToBuffer(Image image) | |
{ | |
using (var ms = new MemoryStream()) | |
{ | |
image.Save(ms, ImageFormat.Png); | |
return ms.ToArray(); | |
} | |
} | |
private static void WriteResponse(HttpResponse response, | |
byte[] buffer) | |
{ | |
response.ContentType = "image/png"; | |
response.BinaryWrite(buffer); | |
response.Flush(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment