Sharker Khaleed Mahmud Silverlight Tips & Tricks

July 22, 2010

79. Get your Gravatar Image in Silverlight

Filed under: Silverlight — Tags: — Sharker Khaleed Mahmud | shamrat231 @ 7:35 AM

[tweetmeme source=”shamrat231” only_single=false]

You can see the live example here [live demo]

The SOURCE CODE(.zip) is at the end of the page for download.

I recently visited Justin article of Silverlight COM features. If you have not read that article, I definitely recommend to have a look at it. Anyway, at the end of the page, there is a reply section. When you type your email address, it gets your avatar. The whole site is in Silverlight.

In this entry, I will show you, how to do that for your Silverlight application. According to gravatar site, you need to create a MD5 hash. All URLs on Gravatar are based on the use of the hashed value of an email address. Images and profiles are both accessed via the hash of an email, and it is considered the primary way of identifying an identity within the system. To ensure a consistent and accurate hash, the following steps should be taken to create a hash:

1.Trim leading and trailing whitespace from an email address
2.Force all characters to lower-case
3.md5 hash the final string

Great, so basically, if I do the above three steps, I am good to go. You can get your MD5 class here by Reid Borsuk and Jenny Zheng.

Now at code level, all that is required is on button click event to generate an url…something like this

Lets go at code level and use the MD5 class to generate a proper url for the image control.

MainPage.xaml

    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            Loaded += (s, e) => {
                tbEmailAddress.Text = “sample@sample.com“;
            };
        }

        private string GetUrl()
        {

            //Jeff Wilcox wpf class code….
            StringBuilder sb = new StringBuilder();
            byte[] text = Encoding.UTF8.GetBytes(tbEmailAddress.Text.ToLower().Trim());
            byte[] md5hash = new MD5Managed().ComputeHash(text);

            foreach (byte b in md5hash)
            {
                sb.Append(b.ToString(“X2”));
            }  

            return sb.ToString();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string url = “http://www.gravatar.com/avatar/” + GetUrl().ToLower() + “.jpg”;
            imgGravatar.Source = new BitmapImage(
                    new Uri(url, UriKind.Absolute));
        }
    }

In the above code, the email address is being converted to MD5 hash and a url is being generated. That url is being assigned to the Image control.

Now, run the .aspx page.

As always, you can download the source code from here [download link

Sharker Khaleed Mahmud
Web Developer

Advertisements

2 Comments »

  1. That’s a cool app, Sharker. Gonna have gravatar pics all over this place.

    Comment by MD5 Hash Generator — July 22, 2010 @ 5:35 PM

  2. […] 79. Get your Gravatar Image in Silverlight « Sharker Khaleed … […]

    Pingback by Latest Gravatar news – Gravatar Module for Phorum « Gravatar Blog — February 12, 2011 @ 4:37 AM


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: