Implement Gravatar icons in ASP.NET

by Mads Kristensen 26. March 2007 02:13

Gravatar icons can be used on all websites that lets users write comments. It could be in a forum or on a blog. The image is retrieved from the Gravatar website and is based on the e-mail address of the person that makes the comment.

It is very easy to set up because it is just a normal image where the src attribute point to the Gravatar website. Here is a method that writes out such an image. It takes an e-mail address and a size. The default size used by Gravatar is 80 (80x80 pixels), but you can specify a smaller size if you’d like.

protected string Gravatar(string email, int size)
{
  System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  byte[] result = md5.ComputeHash(Encoding.ASCII.GetBytes(email));

  System.Text.StringBuilder hash = new System.Text.StringBuilder();
  for (int i = 0; i < result.Length; i++)
    hash.Append(result[i].ToString("x2"));

  System.Text.StringBuilder image = new System.Text.StringBuilder();
  image.Append("<img src=\"");
  image.Append("http://www.gravatar.com/avatar.php?");
  image.Append("gravatar_id=" + hash.ToString());
  image.Append("&amp;rating=G");
  image.Append("&amp;size=" + size);
  image.Append("&amp;default=");
  image.Append(Server.UrlEncode("http://example.com/noavatar.gif"));
  image.Append("\" alt=\"\" />");
  return image.ToString();
}

On the webpage you could then write out the Gravatar image like so:

<%=Gravatar("post@example.com", 60)%>

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Tags:

ASP.NET

Comments

3/26/2007 8:51:12 PM #

Sean Kearney

Hi Mads,
Very quick and easy implementation, nice!

I created a control for Gravatars not all the long ago. Check it out here:
www.carknee.com/.../asp-net-gravatar-control.aspx

-Sean

Sean Kearney |

3/26/2007 9:40:14 PM #

Mads Kristensen

Sean, I looked at your control before creating this snippet. I just needed a single method instead of a control.

Mads Kristensen |

3/29/2007 2:28:27 AM #

 Mike

One small suggestion:

string hash = BitConverter.ToString(result);

Mike |

3/29/2007 8:53:24 PM #

 Thomas Hansen

We've got a "DynamicImage" control which is pretty nice for doing that... Smile
Check it out at:
ajaxwidgets.com/.../DynamicImage.aspx

.t

Thomas Hansen |

4/5/2007 4:06:58 PM #

Fredrik Kalseth

This looks pretty cool Smile

But what about security issues? Could I not just type in the email address of someone else, and pretend to be them?

Fredrik Kalseth |

5/12/2007 12:23:17 AM #

Sean Kearney

Fredrik,
What I did was create a "random" email that I use for Gravatars so it would be almost impossible for someone to guess.

Sean Kearney United States |

1/6/2009 11:36:07 PM #

Iván

Thank you!, it took me 1 minute to make it work! (you saved me some time on this Smile

Iván Spain |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008