public Stream Get(DrawImage request) { var width = request.Width.GetValueOrDefault(640); var height = request.Height.GetValueOrDefault(360); var bgColor = request.Background != null?Color.FromName(request.Background) : Color.ForestGreen; var fgColor = request.Foreground != null?Color.FromName(request.Foreground) : Color.White; var image = new Bitmap(width, height); using (var g = Graphics.FromImage(image)) { g.Clear(bgColor); var drawString = "Hello, {0}!".Fmt(request.Name); var drawFont = new Font("Times", request.FontSize.GetValueOrDefault(40)); var drawBrush = new SolidBrush(fgColor); var drawRect = new RectangleF(0, 0, width, height); var drawFormat = new StringFormat { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center }; g.DrawString(drawString, drawFont, drawBrush, drawRect, drawFormat); var imageFmt = request.Format.ToImageFormat(); base.Response.ContentType = imageFmt.ToImageMimeType(); var ms = new MemoryStream(); image.Save(ms, imageFmt); return(ms); } }
public Stream Get(DrawImage request) { var width = request.Width.GetValueOrDefault(640); var height = request.Height.GetValueOrDefault(360); var bgColor = request.Background != null ? Color.FromName(request.Background) : Color.ForestGreen; var fgColor = request.Foreground != null ? Color.FromName(request.Foreground) : Color.White; var image = new Bitmap(width, height); using (var g = Graphics.FromImage(image)) { g.Clear(bgColor); var drawString = "Hello, {0}!".Fmt(request.Name); var drawFont = new Font("Times", request.FontSize.GetValueOrDefault(40)); var drawBrush = new SolidBrush(fgColor); var drawRect = new RectangleF(0, 0, width, height); var drawFormat = new StringFormat { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center }; g.DrawString(drawString, drawFont, drawBrush, drawRect, drawFormat); var imageFmt = request.Format.ToImageFormat(); base.Response.ContentType = imageFmt.ToImageMimeType(); var ms = new MemoryStream(); image.Save(ms, imageFmt); return ms; } }