private void EpisodeImageContent(HttpListenerContext context)
{
Regex epidPattern = new Regex(@"^\?epid=([0-9]+)$");
Match match = epidPattern.Match(context.Request.Url.Query);
int epid;
if (!int.TryParse(match.Groups[1].Value, out epid))
{
// Request doesn't contain a valid episode id
this.ErrorPage404(context);
return;
}
Bitmap image = Model.Episode.GetImage(epid);
if (image == null)
{
// Specified episode does not have an image
this.ErrorPage404(context);
return;
}
context.Response.ContentType = "image/png";
// Find the appropriate codec for saving the image as a png
System.Drawing.Imaging.ImageCodecInfo pngCodec = null;
foreach (System.Drawing.Imaging.ImageCodecInfo codec in System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders())
{
if (codec.MimeType == context.Response.ContentType)
{
pngCodec = codec;
break;
}
}
try
{
image.Save(context.Response.OutputStream, pngCodec, null);
}
catch (HttpListenerException)
{
// Don't worry about dropped connections etc.
}
}