using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication1.Controllers {
  public class ExempleNonHtmlController : Controller {
    static readonly object VerrouContextePourMesurer = new object();
    static readonly Graphics ContextePourMesurer = Graphics.FromImage( new Bitmap( 1, 1 ) );
    public ActionResult Index() {
      ViewBag.Texte = "Lancez-vous !";
      ViewBag.Fonte = "Arial";
      return View( FontFamily.Families.Select( f => f.Name ) );
    }
    private MemoryStream FluxASortir;
    public ActionResult Tracer( string fonte, string texte ) {
      Font f = new Font( FontFamily.Families.First( ff => ff.Name == fonte ), 48.0f, FontStyle.Regular );
      Size taille;
      lock ( ExempleNonHtmlController.VerrouContextePourMesurer ) {
        var tailleF = ContextePourMesurer.MeasureString( texte, f );
        taille = new Size( (int)tailleF.Width + 20, (int)tailleF.Height + 20 );
      }
      using( Bitmap image = new Bitmap( taille.Width, taille.Height ) ) {
        using( var dc = Graphics.FromImage( image ) ) {
          dc.FillRectangle( Brushes.Transparent, 0, 0, image.Width, image.Height );
          dc.DrawString( texte, f, Brushes.Red, 10, 10 );
        }
        FluxASortir = new MemoryStream();
        image.Save( FluxASortir, ImageFormat.Png );
        FluxASortir.Seek( 0, SeekOrigin.Begin );
        return File( FluxASortir, "image/png" );
      }
    }
    protected override void Dispose( bool disposing ) {
      if( disposing && FluxASortir != null )
				FluxASortir.Dispose();
      base.Dispose( disposing );
    }
  }
}