Gravatar is a nifty service. But if someone on your site doesn’t have a Gravatar, Gravatar will display a default icon.  You have some choices in what gets displayed (documented here), but what if you want nothing displayed?  Well, you can have Gravatar return you a 404, but you need to handle that 404 somehow. Not a big deal, but here’s how I did it.

First off, I started by using this code, which is an ASP.NET control that encapsulates all the Gravatar logic. Then, I mucked around with the override of Render, adding the following:

if (!string.IsNullOrEmpty(DefaultImage))
{
    imageUrl += "&default=404"; // +HttpUtility.UrlEncode(DefaultImage);
}
using (WebClient client = new WebClient())
{
    try
    {
        client.DownloadData(imageUrl);

    }
    catch (WebException webException)
    {
        noImage = true;
    }

}



if (noImage)
{
return;

}

Yes, it means I have to make a call to Gravatar in order to determine to not provide an image, but it is worth it. If performance is an issue, an optimization would be to cache the emails that have already been checked. In fact, I’ll probably add that.  No more default gravatars!