Submit Your Site For Free!

Email Address:
* URL:
*
*Indicates Mandatory Field

Terms & Conditions

DevWebProDK
FlashNewz
DevWebPro










Gravatar Icons In ASP.NET

By Mads Kristensen
Expert Author
Article Date: 2007-03-26

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("&rating=G");
  image.Append("&size=" + size);
  image.Append("&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)%>

Comments

About the Author:
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/



DevWebProDK is an iEntry, Inc. ® publication - All Rights Reserved Privacy Policy and Legal