<%@ Page Language="c#" %>
<%@ import Namespace="System.Drawing" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<%@ import Namespace="System.Drawing.Drawing2D"%>
<script runat="server">
void Page_Load (object sender ,EventArgs e) {
    // parameters/defaults
    string a    = Request.QueryString["a"]==null?"FFFFFF":Request.QueryString["a"].ToString();
    string b    = Request.QueryString["b"]==null?"6799FF":Request.QueryString["b"].ToString();
    string c    = Request.QueryString["c"]==null?"000000":Request.QueryString["c"].ToString();
    int w        = Request.QueryString["w"]==null?468:Convert.ToInt32(Request.QueryString["w"]);
    int h        = Request.QueryString["h"]==null?60:Convert.ToInt32(Request.QueryString["h"]);
    string d    = Request.QueryString["d"]==null?"ltr":Request.QueryString["d"].ToString();
    string t    = Request.QueryString["t"]==null?"":Request.QueryString["t"].ToString();
    int s        = Request.QueryString["s"]==null?14:Convert.ToInt32(Request.QueryString["s"]);
    
    int angle=0;
    switch (d) {
        case "ttb":  angle=90; break;
        case "rtl":  angle=180;break;
        case "btt":  angle=270;break;
    }
    
    Bitmap oBitmap = new Bitmap(w, h);
    Graphics oGraphics = Graphics.FromImage(oBitmap);
    
    
    // GRADIENT
    Rectangle recBackground = new Rectangle(0, 0, w, h);
    LinearGradientBrush gb = new LinearGradientBrush (recBackground,HexToColor(a),HexToColor(b), (float)angle);
    oGraphics.FillRectangle(gb,recBackground);

    // TEXT
    Font fontText = new Font("Arial", s);
    SolidBrush blackBrush = new SolidBrush(HexToColor(c));
    oGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
    oGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    oGraphics.DrawString(t, fontText, blackBrush , new RectangleF(5,8, (w-10),(h-10)) );

    Response.ContentType = "image/jpeg"; // gif
    oBitmap.Save(Response.OutputStream, ImageFormat.Jpeg); // .Gif
    
    oGraphics.Dispose();
    oBitmap.Dispose();
}

/*
 #####            Helper functions from             ######
 http://www.c-sharpcorner.com/Code/2002/Sept/HexColors.asp
*/
public Color HexToColor(string hexString) {
//  Translates a html hexadecimal definition of a color into a .NET Framework Color.
//  The input string must start with a '#' character and be followed by 6 hexadecimal
//  digits. The digits A-F are not case sensitive. If the conversion was not successfull
//  the color white will be returned.

    Color actColor;
    int r,g,b;
    r=0;
    g=0;
    b=0;
    if ((hexString.StartsWith("#"))&&(hexString.Length==7)) {
        r=HexToInt(hexString.Substring(1,2));
        g=HexToInt(hexString.Substring(3,2));
        b=HexToInt(hexString.Substring(5,2));
        actColor=Color.FromArgb(r,g,b);
    } else if ((hexString.Length==6)) {
        r=HexToInt(hexString.Substring(0,2));
        g=HexToInt(hexString.Substring(2,2));
        b=HexToInt(hexString.Substring(4,2));
        actColor=Color.FromArgb(r,g,b);
    } else {
        actColor=Color.White;
    }
    return actColor;
}

public static int HexToInt(String hexstr) {
    //  This method converts a hexvalues string as 80FF into a integer.
    //    Note that you may not put a '#' at the beginning of string! There
    //  is not much error checking in this method. If the string does not
    //  represent a valid hexadecimal value it returns 0.
        int counter,hexint;
        char[] hexarr;
        hexint=0;
        hexstr=hexstr.ToUpper();
        hexarr=hexstr.ToCharArray();
        for (counter=hexarr.Length-1;counter>=0;counter--)
        {
            if ((hexarr[counter]>='0') && (hexarr[counter]<='9'))
            {
                hexint+=(hexarr[counter]-48)*((int)(Math.Pow(16,hexarr.Length-1-counter)));
            }
            else
            {
                if ((hexarr[counter]>='A') && (hexarr[counter]<='F'))
                {
                    hexint+=(hexarr[counter]-55)*((int)(Math.Pow(16,hexarr.Length-1-counter)));
                }
                else
                {
                    hexint=0;
                    break;
                }
            }
        }
    return hexint;
}
</script>