Gravatar

http://lifeofajackass.com/wp-content/plugins/downloads-manager/img/icons/default.gif download: Gravatar (61.36KB)
added: 12/03/2009
clicks: 4130
description: A small .NET control that allows you to pull in a users Gravatar.

For those that have been here before you’ll notice a huge change in the code. I went ahead and did this as a control class. There are 4 properties to be set: EMAIL, RATING, SIZE, ICONSET

You can access the code from SVN or download the project above.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Gravatar
{
    public partial class Gravatar : UserControl
    {
        /// <summary>
        /// Base URL for the Gravatar image
        /// </summary>
        private string BaseURL = "http://www.gravatar.com/avatar/{0}?d={1}&s={2}&r={3}";

        public Gravatar()
        {
            InitializeComponent();
        }

        #region Properties, very ugly
        /// <summary>
        /// Email Property
        /// </summary>
        string theEmail;

        public string email
        {
            get { return theEmail; }
            set
            {
                theEmail = value;
                UpdateGravatar();
            }
        }

        /// <summary>
        /// Rating Property
        /// </summary>
        public enum Rating
        {
            g, pg, r, x
        }

        private Rating theRating;

        public Rating rating
        {
            get { return theRating; }
            set
            {
                theRating = value;
                UpdateGravatar();
            }
        }

        /// <summary>
        /// Icon Set Property
        /// </summary>
        public enum IconSet
        {
            identicon, monsterid, wavatar
        }

        private IconSet theIconSet;

        public IconSet iconset
        {
            get { return theIconSet; }
            set
            {
                theIconSet = value;
                UpdateGravatar();
            }
        }

        /// <summary>
        /// Size Property
        /// </summary>
        int theSize = 80;
        public int size
        {
            get { return theSize; }
            set
            {
                theSize = value;
                UpdateGravatar();
            }
        }
        #endregion

        /// <summary>
        /// Small MD5 Function
        /// </summary>
        /// <param name="theEmail"></param>
        /// <returns>Hash of the email address passed.</returns>
        public string MD5(string theEmail)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5Obj =
                new System.Security.Cryptography.MD5CryptoServiceProvider();

            byte[] bytesToHash = System.Text.Encoding.ASCII.GetBytes(theEmail);

            bytesToHash = md5Obj.ComputeHash(bytesToHash);

            string strResult = "";

            foreach (byte b in bytesToHash)
            {
                strResult += b.ToString("x2");
            }

            return strResult;
        }

        /// <summary>
        /// Update the Gravatar anytime an attribute is changed
        /// </summary>
        private void UpdateGravatar()
        {
            //hash the email address
            string hE = MD5(theEmail);
            //format our url to the Gravatar
            string tU = string.Format(BaseURL, hE, iconset, size, rating);
            //resize our control (I'm not using AutoSize for a reason)
            this.Size = new System.Drawing.Size(size, size);
            imgGravatar.Size = new System.Drawing.Size(size, size);
            //load the Gravatar into our picture box. (maybe later I will load this
            //into memory and paint it to the control instead.
            imgGravatar.ImageLocation = tU;
        }
    }
}

84 Responses to Gravatar

  1. Guest says:

    Its interesting,good work.

  2. SEO says:

    many thanks!!

  3. а ведь действительно!)

  4. jasonelabusador says:

    palomo

  5. mike says:

    nice work

  6. How the URL is constructed

    Step 1, The URL base: A gravatar is a dynamic image resource that is requested from our server. The request URL is presented here, broken into its segments. The URL always begins with

    http://www.gravatar.com/avatar/

  7. Thanks for this great design pack I can use it all!

  8. scholarship says:

    Very cool! Can you generate those as named parameters for.NET users? That’d be pretty cool.