Creating a Gravatar ASP.NET Control

by David Kiff 14. February 2009 10:30

Introduction

This article provides an ASP.NET Gravatar control that allows you to add Gravatars to your .NET enabled sites easily.

If you are using a framework, such as Joomla for your website, there might already be a plug-in available (http://en.gravatar.com/site/implement).

What is a Gravatar?

Gravatar or Globally Recognised Avatar is a small image of you, identified by your email address. This means supporting websites, such as the many blogging websites, can display an image of you when you post a comment.

The idea was thought up by a guy named Tom Werner. If you have not got a Gravatar yet, follow this link: http://gravatar.com

How do I obtain a Gravatar Image?

The process of obtaining a Gravatar image is very straight forward. You take the users email address and create an MD5 Hash of it which you append to a URL Provided by Gravatar.com like so:
http://www.gravatar.com/avatar/MD5HashOfEmailAddressHere

This URL is then the link to the image which can be obtained by placing it within an anchor tag:
<a href="http://www.gravatar.com/avatar/MD5HashOfEmailAddressHere" />

There are other parameters you can pass such as size, rating and default image (should the email address not be registered at Gravatar.com). These optional parameters are passed via querystring and are appended to the URL.

Show me the code!

What namespaces do I need?
using System;
using System.Security.Cryptography; //For creating the MD5 Hash
using System.Text; //For the StringBuilders
using System.Web; //For the HttpUtility class
using System.Web.UI.WebControls; //To inherit from the web UI control Image
How does it work?

Very simply! Just before it renders the control it builds the encoded URL and sets it on the base Image control.

When render is called it will use this image for the href part ofr the rendered anchor tag.

protected override void OnPreRender(EventArgs e)
{
  base.OnPreRender(e);
  ImageUrl = GetEncodedImageUrl();
}
How do you use this control?
Select the "Download code" at the bottom of this article and unzip the contents. You will find a demo folder and a Control folder. The Demo folder contains a working example of how the control can be used, with source code. The control folder contains just the .dll for those who just want to reference it. To reference the .dll and use it within your application follow these steps:
  1. Open your project in Visual Studio
  2. Right click on the project containing your aspx files
  3. Select "Add Reference"
  4. Select the browse tab and select the DavidKiff.Controls.dll
  5. Add the following to the top of a page you would like to display the Gravatars:

    <%@ Register Assembly="DavidKiff.Controls" Namespace="DavidKiff.Controls" TagPrefix="dk" %>

    or add this to your web.config file:

    <pages>
       <controls>
          <add tagPrefix="dk" namespace="DavidKiff.Controls" assembly="DavidKiff.Controls"/>
       </controls>
    </pages>
  6. Add the control to your page, like so: <dk:Gravatar runat="server" EmailAddress="example@DavidKiff.co.uk" Size="150" Rating="pg" DefaultImage="identicon" />

Tags: ,

Gravatar | ASP.NET

Comments

2/17/2009 5:23:00 AM #

Sujith PV

Hi  

Very nice article.    

Regards  
Sujith PV

Sujith PV

2/17/2009 10:33:00 AM #

Ashraf

Nice one man, Very useful for community sites images.

Ashraf

2/17/2009 11:12:00 AM #

Thanigainathan.S

Hi,    This is a very nice thing. But what happens if two of thewm have the same email ids ?    Thanks,  Thani

Thanigainathan.S

2/17/2009 6:28:00 PM #

David Kiff

Hi Thaniganathan.S,

You have one Gravatar per email address.  If you have two email addresses that are the same, it will just display the same image twice.  

Does that answer your question?

David Kiff United Kingdom

6/24/2009 7:31:00 AM #

Danny

Can't wait to try this one.

Danny

8/18/2009 5:09:17 PM #

Bhavna Rana

Yep,

a Html.Gravatar("some@email.com") that also renders the img tag and not only the url of the image.And you can accept an anonymous type (as the other Html helpers do) so that devs can supply arbitrary html attributes (like class, style, onclick events and so on)

Bhavna Rana India

9/15/2009 10:54:28 PM #

Anonymous

Nice Coding

Anonymous United States

9/27/2009 5:18:15 PM #

paffy

Cool site!! Thanks for info!!!

paffy United States

9/27/2009 5:18:27 PM #

paffy

cool!!!

paffy United States

9/27/2009 10:08:51 PM #

Интернет Маркетинг

Thanks mate!I was looking for that solution nearly all day!I love your blog,awesome!!!
Can I copy this article on my blog?If,yes we can exchange useful articles in the future?

Интернет Маркетинг

10/14/2009 9:34:43 PM #

Anonymous

Hmmm interesting stuff

Anonymous United States

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading