gravatar

Gravatar is a popular avatar service that links email addresses to the avatar. You do not need to register an account to be able to display gravatars on your site. All you need to do is construct the URL of the image using the user's email.

To construct the URL concatenate "http://www.gravatar.com/avatar.php?gravatar_id=" to the MD5 hash of the email. This displays an image using the default size (80 by 80 pixels) and the default rating (G). For example the URL of [email protected] will be:
http://www.gravatar.com/avatar.php?gravatar_id=111d68d06e2d317b5a59c2c6c5bad808

Before calculating the MD5 hash convert all the characters of the email address to lower case. The gravatar is a JPEG image, so if a file extension is required just append to it '.jpg'. There are two other parameters that can be set in the URL; s and r. s is the size of the image, it takes a single number that is the size of the width and height of the image. If the value of s is less than 1 or greater than 600 the image returned will be the default size. When the size is added to the example above the URL will look like this:
http://www.gravatar.com/avatar.php?gravatar_id=111d68d06e2d317b5a59c2c6c5bad808&s=60

The r parameter is the MPAA style rating. When users upload an image they are asked to choose the rating of the image. The ratings in order are g, pg, r and x. Passing one of the ratings as a parameter will retrieve the image with this rating or a lower one but not higher. Adding rating to the previous example will make it look like this:
http://www.gravatar.com/avatar.php?gravatar_id=111d68d06e2d317b5a59c2c6c5bad808&s=60&r=g

There are two download links at the end of the post. The class listed below, with overloaded functions of GetURL, and a Windows Forms example that uses the class.

class GravatarImage
{
    private const string _url = "http://www.gravatar.com/avatar.php?gravatar_id=";

    /// <summary>
    /// Get the URL of the image
    /// </summary>
    /// <param name="email">The email address</param>
    /// <param name="size"&gr;The size of the image (1 - 600)</param>
    /// <param name="rating">The MPAA style rating(g, pg, r, x)</param>
    /// <returns>The image URL</returns&gr;
    public static string GetURL(string email, int size, string rating)
    {
        email = email.ToLower();
        email = getMd5Hash(email);

        if (size < 1 | size > 600)
        {
            throw new ArgumentOutOfRangeException("size",
                "The image size should be between 20 and 80");
        }

        rating = rating.ToLower();
        if (rating != "g" & rating != "pg" & rating != "r" & rating != "x")
        {
            throw new ArgumentOutOfRangeException("rating",
                "The rating can only be one of the following: g, pg, r, x");
        }

        return _url + email + "&s=" + size.ToString() + "&r=" + rating;
    }

    /// <summary&gr;
    /// Hash an input string and return the hash as a 32 character hexadecimal string
    /// </summary&gr;
    /// <param name="input"&gr;The string to hash</param&gr;
    /// <returns&gr;The MD5 hash</returns&gr;
    public static string getMd5Hash(string input)
    {
        // Create a new instance of the MD5CryptoServiceProvider object.
        MD5 md5Hasher = MD5.Create();

        // Convert the input string to a byte array and compute the hash.
        byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

        // Create a new Stringbuilder to collect the bytes
        // and create a string.
        StringBuilder sBuilder = new StringBuilder();

        // Loop through each byte of the hashed data 
        // and format each one as a hexadecimal string.
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }

        return sBuilder.ToString();  // Return the hexadecimal string.
    }
}