Gravatar is a service that associates your e-mail addresses with a picture of you for use on blog comments, forums and bulletin boards. I’ve been a happy user for years.
A cool thing about Gravatar images is that they can be used from any application either on a website or a desktop application. But still, they are primarily used on blogs and forums.
Today, I played with the thought of a wider use and wrote some code that makes working with Gravatars very easy on both web and the desktop. The reason for this is that I needed a way to collect Gravatar images for a list of users, but only the users that actually have a Gravatar associated with their e-mail address.
The Gravatar service doesn’t have an API you can use to check if an e-mail address is associated or not. But by downloading a Gravatar image, the response header Last-Modifed is 1970-01-01 if no image is found and the default image is returned. This information can be used to only store the images users have uploaded to the Gravatar service.
The class is called Gravatar and is very simple and small. Here is an example of using the class to download a Gravatar image and save it to disk.
string email = "firstname.lastname@example.org";
Gravatar gravatar = new Gravatar(email);
using (FileStream writer = new FileStream("c:\\" + email + ".jpg", FileMode.Create))
writer.Write(gravatar.Image, 0, gravatar.Image.Length);
Download the class below and put it in your App_Code folder. Then you can use the example above from any ASP.NET page or control.