UPDATE: See here
In the near future, I have a web project coming out that I've been working on for the past month or so. I'm not going to give too much detail on that just yet, however, in working on this project, I had to come up with a way for users to have profile images. I would rather not host these images on my server, I decided to leverage Gravatar (Globally Recognized Avatars).
There are a couple pre-existing options for dealing with gravatar in the .NET framework, however, I'm using the MVC framework — standard ASP.NET controls are out of the question. After a brief look on the Gravatar site (specifically the page dealing with how the URL is constructed), it seemed like this would be a pretty easy task to write a class that I could use to get gravatar image paths. Below is the main part of the class I created to retrieve gravatar information based on an e-mail address:
public static string GetGravatarURL(string email, string size)
{
return (string.Format("http://www.gravatar.com/avatar/{0}?s={1}&r=PG",
EncryptMD5(email), size));
}
public static string GetGravatarURL(string email, string size, string defaultImagePath)
{
return GetGravatarURL(email, size) + string.Format("&default={0}", defaultImagePath);
}
private static string EncryptMD5(string Value)
{
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] valueArray = System.Text.Encoding.ASCII.GetBytes(Value);
valueArray = md5.ComputeHash(valueArray);
string encrypted = "";
for (int i = 0; i < valueArray.Length; i++)
encrypted += valueArray[i].ToString("x2").ToLower();
return encrypted;
}
All we're really doing is creating a URL based on the Gravatar path, an MD5 encrypted version of an e-mail address and some user specified parameters. For the methods, Size is how many pixels the image should be (up to 500) and defaultImagePath is the url to use as an alternate image if the user does not have a Gravatar image. There is a rating parameter also but for my site, I'm setting it to PG always.
This is not all that difficult but hopefully useful. I plan to continue writing examples as I make progress on the web application I mentioned.