Get Gravatar Image
using System.Text.RegularExpressions;
using System;
using System.Security.Cryptography;
using System.Text;
public class Base
{
public static string GetGravatarImage(string email, int size)
{
if (isValidEmail(email))
{
return string.Format("http://www.gravatar.com/avatar/{0}?s={1}", GetMd5Hash(email), size.ToString());
}
else
{
return "";
}
}
public static Guid GetMd5Hash(string inString)
{
var unicodeEncoding = new UnicodeEncoding();
var message = unicodeEncoding.GetBytes(inString);
MD5 hashString = new MD5CryptoServiceProvider();
return new Guid(hashString.ComputeHash(message));
}
private static bool isValidEmail(string email)
{
Regex r = new Regex(@"^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$");
if (!string.IsNullOrEmpty(email))
return r.IsMatch(email);
else
return false;
}
}
Related examples in the same category