Simplicity is prerequisite for reliability. --Edsger W. Dijkstra

The Cambia.Gravatar Nuget Package

Copyright © 2016 Steve Lautenschlager

Gravatars are globally recognized avatars in common use on websites around the globe. This simple nuget package provides a .NET class for easily generating Gravatar URLs.


Gravatars allow you to display an avatar image associated with a user simply by hashing an email address.

Users have complete control of avatar images at Wherever gravatars are used, a user's image will update to use the new one on all websites where gravatars are used.

Here are some samples of my gravatar:

The associated HTML look like this:

  <img src='' />
  <img src='' />
  <img src='' />
  <img src='' />

Get the Cambia.Gravatar Nuget Package (Free)

You can download the Cambia.Gravatar Nuget Package from

Generate Gravatar URL (Basic)

The easiest way to create the gravatar URL using the Cambia.Gravatar nuget package is as follows:

using Cambia;     

Gravatar g = new Gravatar("");
string url = g.GetUrl();

// url =

This is what it displays:

Wait a minute, that doesn't look like a guy named Joe. No, Joe apparently hasn't setup a gravatar image associated with the fake email address above.

Therefore Gravatar delivers the rotated G icon as a default image.

I'll show you how to change the default image below.

Generate Gravatar URL (All Options)

While an email address is all that is required to get a Gravatar URL you make want more control over a few things. Here's the list of options you can tweak:

  • Email
  • Size (in pixels)
  • Maximum Allowed Image Rating (g, pg, r, x)
  • Default Option (type of image to show when none is available)
  • Default URL (the URL of your custom default image)
  • Force the Default (Show the default image even if a valid image is available)

This webpage on the Gravatar website specifies the exact format for Gravatar URLs and the available options:

using Cambia;     

string defaultImage = "";
Gravatar g = new Gravatar("", 256, GravatarRating.R, GravatarDefaultOption.Custom, defaultImage, false);
string url = g.GetUrl();

// url =

Here's what we get with a default image specified:

In the above example we specified a custom URL for when a valid image doesn't exist. Since no valid gravatar image exists, the default Cambia Research icon is displayed which is the URL that we specified.

There are other default options, too, some of which will generate a unique image for an email address so that you don't just have the same boring default image for people who haven't setup gravatar images yet.

g = new Gravatar("", 60, GravatarRating.R, GravatarDefaultOption.Identicon);
g = new Gravatar("", 60, GravatarRating.R, GravatarDefaultOption.Blank);
g = new Gravatar("", 60, GravatarRating.R, GravatarDefaultOption.MonsterId);
g = new Gravatar("", 60, GravatarRating.R, GravatarDefaultOption.MysteryMan);
g = new Gravatar("", 60, GravatarRating.R, GravatarDefaultOption.Option404);
g = new Gravatar("", 60, GravatarRating.R, GravatarDefaultOption.Retro);
g = new Gravatar("", 60, GravatarRating.R, GravatarDefaultOption.Wavatar);
g = new Gravatar("", 60, GravatarRating.R, GravatarDefaultOption.None);

These default options yield the following:

With a different email address you might get:

The fifth item appears to be a broken image. That was the 404 option. In this case the Gravatar site returns HTTP 404 instead of an image.

Static Methods

There are several static methods on the Gravatar class to support generating the gravatar URL or parsing the query parameter values.

// Generate the email hash
string hash = Gravatar.HashEmailForGravatar("");

// Generate the full URL.  Allows you to generate a URL without instantiating a class if you prefer.
string url = Gravatar.GenerateUrl(...)

// Gets the query value for the "d", default, query item.  The following code would return
// blank because that's what you'd insert into the URL
// e.g. query = ?s=80&r=pg&d=blank
string queryValue = Gravatar.GetDefaultOptionValueForUrl(GravatarDefaultOption.Blank);

// Parses the default option value from the url into the associated enumeration.  
// GravatarDefaultOption.None if the value does not match a known default type
GravatarDefaultOption opt = Gravatar.ParseDefaultOptionType("wavatar");

// Parses the rating value from the url into the associated enumeration.  
// GravatarRating.G if the value does not match a known rating.
GravatarRating rating = Gravatar.ParseRatingValue("pg");

// Parses the value extracted from the gravatar URL for the size key, "s".
// Returns the default size of 80 if the value can't be parsed.
int size = ParseSizeValue("256");

// Parses the force default query value, "f".
// Returns true if the value is "y" or "Y", otherwise false
bool forceDefault = Gravatar.ParseForceDefaultValue("true");