Skip to content

Instantly share code, notes, and snippets.

@davepermen
Forked from anonymous/Main.cs
Created April 11, 2013 00:31
Show Gist options
  • Save davepermen/5359694 to your computer and use it in GitHub Desktop.
Save davepermen/5359694 to your computer and use it in GitHub Desktop.
Windows Phone 8 LiveTile Generator using NancyFx
<!DOCTYPE html>
<meta charset="utf-8" />
<style>
body {
background: black;
}
* {
color: white;
font: 40px 'Segoe UI';
}
h1 {
font-size: 2em;
}
img {
border: 2px solid white;
}
</style>
<h1>Windows Phone 8<h2>
<p>
<a href="http://livetile.azurewebsites.net/wp8/@Model.text">http://livetile.azurewebsites.net/wp8/@Model.text</a>
</p>
<img src="http://livetile.azurewebsites.net/wp8/@Model.text" />
using Nancy;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.IO;
using System.Web;
namespace Live_Tile_Generator
{
public class Main : NancyModule
{
public Main()
{
Get["/wp8/{text}"] = parameters =>
{
var text = HttpUtility.UrlDecode((string)parameters.text);
using (var bitmap = new Bitmap(336, 336))
{
using (var g = Graphics.FromImage(bitmap))
{
g.Clear(Color.Transparent);
g.TextRenderingHint = TextRenderingHint.AntiAlias;
for (var fontsize = 80; fontsize > 10; fontsize -= 10)
{
using (var f = new Font("Segoe UI", fontsize))
{
var size = g.MeasureString(text, f);
if (size.Width < 336)
{
g.DrawString(text, f, Brushes.White, new PointF((336 - size.Width) / 2, (336 - size.Height) / 2));
break;
}
}
}
}
var s = new MemoryStream(); // nancy closes it for us
{
bitmap.Save(s, ImageFormat.Png);
s.Position = 0;
return Response.FromStream(s, "image/png");
}
}
};
Get["/demo/{text}"] = parameters => View["demo", parameters];
Get["/"] = _ => Response.AsRedirect("/demo/demotext");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment