Play with Colors - Part 2 - Check the font on a specific background using .NET reflection

by Rahul 6/13/2008 2:30:25 AM

I have already shown how to use .NET reflection to get the information about all the colors in .NET. Let's say you want to check out how a specific colored font would appear on any background, you may like to check this sample.

Use the code which follows to get an output like the one below.

image
There are various ways to get this output, but I guess this code is pretty small and will show you some of the things that you could do with System.Reflection in .NET.

Here is the code...

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Import Namespace="System.Drawing" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        string strColor = string.Empty;
        if (Request.QueryString.Get("color") != null)
            strColor = Request.QueryString.Get("color");
        else
            strColor = "White";
        Response.Write("<BR><BR><BR><BR>");
        Response.Write("<Table width='100%' border=1 cellspacing=1 cellpadding=5>");
        Response.Write("<TR bgcolor='LightCyan'><TD width='15%'><B>Color Name</B></TD><TD width='30%'>" +
            "<B>Color</B></TD><TD width='15%'>Color Code</TD>");
        //Get the assembly's information
        Color x = new Color();
        Color ColorInformation;
        Type strType = x.GetType();
        string strColorHexCode = string.Empty;
        string strColorName = string.Empty;
        string strPath = Request.ApplicationPath + "/samples/color/color2.aspx?color=";
        MemberInfo[] arrMemberInfo = strType.GetProperties();
        //Lets Enumerate the colors now
        foreach (MemberInfo myMemberInfo in arrMemberInfo)
        {
            //Get the RGB components from the color and convert them to Hex with 2 places.
ColorInformation = Color.FromName(myMemberInfo.Name); strColorName = myMemberInfo.Name; strColorHexCode = "#" + ColorInformation.R.ToString("X2") + ColorInformation.G.ToString("X2") + ColorInformation.B.ToString("X2"); Response.Write("<TR>"); Response.Write("<TD><a href='" + strPath + strColorName + "'>"); Response.Write(strColorName + "</a>"); Response.Write("<TD align='center' bgcolor='" + strColorName); Response.Write("'><font color='" + strColor + "'>---------- "); Response.Write(strColor + " on " + strColorName + " BackGround ----------</font></TD>"); Response.Write("<TD><a href='" + strPath + strColorName + "'>"); Response.Write(strColorHexCode + "</a></TD></TR>"); if (myMemberInfo.Name == "YellowGreen") break; } Response.Write("</Table>"); } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Color Sample 2</title> </head> <body style ="font-family:verdana"> <div style="position:absolute; top:10"> <form id="form1" runat="server"> <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="http://www.dotnetscraps.com/pics/dotnetscraps-logo.gif" PostBackUrl="http://www.dotnetscraps.com" /> </form> </div> </body> </html>

Check this sample live.

Hope this helps Wave
Rahul



blog comments powered by Disqus

Comments

Search


Tags



Categories

Calendar

<<  March 2010  >>
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

All Items
Sign in

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2010, Rahul Soni

Powered by BlogEngine.NET 1.4.5.0