Title: Obtaining Gravatar images
Slug: obtaining-gravatar-images
Date: 2009-12-13 05:46:00
Author: Kartones
Lang: en
Tags: Development, C#, Tools
Description: A guide on obtaining Gravatar images by building the service URL.

 <p><a href="http://www.gravatar.com">Gravatar</a> is a website that, after registering and adding an email, allows you to set an avatar (image) associated to that email, so that sites that query them for gravatar images will return your same image to all of them.</p>  <p>Although Community Server doesn't stores emails of user comments, I wanted to see how the avatars are obtained from Gravatar.com.</p>  <p>The url you have to build is well explained <a href="http://en.gravatar.com/site/implement/url">in their website</a>, so I will just place a basic C# code that calculates the MD5 hash and inserts it on the url:</p>  <p><font face="Courier New"><font color="#0000ff">string </font><font color="#000000">email </font><font color="#0000ff">= </font><font color="#808080">"me@mydomain.com"</font></font><font face="Courier New"><font color="#0000ff">;        <br>        <br></font><font color="#000000">System.Security.Cryptography.MD5CryptoServiceProvider CryptoService</font></font><font face="Courier New"><font color="#0000ff">;        <br>byte</font><font color="#000000">[] InputBytes </font><font color="#0000ff">= </font><font color="#000000">System.Text.Encoding.ASCII.GetBytes(email.Replace(</font><font color="#808080">" "</font><font color="#000000">, </font><font color="#808080">""</font><font color="#000000">).ToLower())</font></font><font face="Courier New"><font color="#0000ff">;        <br>string </font><font color="#000000">emailHash</font></font><font face="Courier New"><font color="#0000ff">;        <br>        <br></font><font color="#000000">CryptoService </font><font color="#0000ff">= new </font><font color="#000000">System.Security.Cryptography.MD5CryptoServiceProvider()</font></font><font face="Courier New"><font color="#0000ff">;        <br>                         <br></font><font color="#000000">InputBytes </font><font color="#0000ff">= </font><font color="#000000">CryptoService.ComputeHash(InputBytes)</font></font><font face="Courier New"><font color="#0000ff">;        <br></font><font color="#000000">emailHash </font><font color="#0000ff">= </font><font color="#000000">BitConverter.ToString(InputBytes).Replace(</font><font color="#808080">"-"</font><font color="#000000">, </font><font color="#808080">""</font><font color="#000000">)</font></font><font face="Courier New"><font color="#0000ff">;        <br>        <br>string </font><font color="#000000">gravatarURL </font><font color="#0000ff">= string</font><font color="#000000">.Format(</font><font color="#808080">"http://www.gravatar.com/avatar/{0}.jpg"</font><font color="#000000">, emailHash)</font><font color="#0000ff">;</font></font></p>  <p>And we're done, the final URL pointing to a gravatar.</p>  <p>The following image gets one of my email adresses' gravatar (if I change it from the cigar dog one day on my gravatar account, it will change automatically):</p>  <p><img src="http://www.gravatar.com/avatar/c61d22dc19219891c72b7b08e59ac71f.jpg?s=128"> </p>  <p>There are options (as querystring parameters) as getting the image in a specific resolution, or getting the identicon.</p>
