protected void RenderGravater(HtmlTextWriter output) { // output the default attributes: AddAttributesToRender(output); //if (UseLink) //{ // LinkUrl = GetLinkUrl(); //} // if the size property has been specified, ensure it is a short, and in the range // 1..512: try { // if it's not in the allowed range, throw an exception: if (Size < 1 || Size > 512) { throw new ArgumentOutOfRangeException(); } } catch { Size = 80; } // default the image url: string imageUrl = GravatarBaseUrl; if (!string.IsNullOrEmpty(Email)) { // build up image url, including MD5 hash for supplied email: MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); UTF8Encoding encoder = new UTF8Encoding(); MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider(); byte[] hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(Email)); StringBuilder sb = new StringBuilder(hashedBytes.Length * 2); for (int i = 0; i < hashedBytes.Length; i++) { sb.Append(hashedBytes[i].ToString("X2")); } imageUrl += sb.ToString().ToLower(); imageUrl += ".jpg?r=" + MaxAllowedRating.ToString(); imageUrl += "&s=" + Size.ToString(); } // output default parameter if specified if ((UseInternalDefaultForGravatar) && (DefaultInternalAvatar.Length > 0)) { string defaultImageUrl = WebUtils.ResolveServerUrl(DefaultInternalAvatar); if (!defaultImageUrl.Contains("localhost")) { imageUrl += "&default=" + HttpUtility.UrlEncode(defaultImageUrl); } } // if we need to output the site link: if (OutputGravatarSiteLink) { output.AddAttribute(HtmlTextWriterAttribute.Href, GetLinkUrl()); output.AddAttribute(HtmlTextWriterAttribute.Title, LinkTitle); if (CssClass.Length > 0) { output.AddAttribute(HtmlTextWriterAttribute.Class, CssClass); } if (Target.Length > 0) { output.AddAttribute(HtmlTextWriterAttribute.Target, Target); } output.RenderBeginTag(HtmlTextWriterTag.A); } // output required attributes/img tag: output.AddAttribute(HtmlTextWriterAttribute.Width, Size.ToString()); output.AddAttribute(HtmlTextWriterAttribute.Height, Size.ToString()); output.AddAttribute(HtmlTextWriterAttribute.Src, imageUrl); output.AddAttribute(HtmlTextWriterAttribute.Alt, "Gravatar"); output.RenderBeginTag("img"); output.RenderEndTag(); // if we need to output the site link: if (OutputGravatarSiteLink) { output.RenderEndTag(); } }
protected override void Render(HtmlTextWriter output) { if (HttpContext.Current == null) { output.Write("[" + this.ID + "]"); return; } // output the default attributes: AddAttributesToRender(output); // if the size property has been specified, ensure it is a short, and in the range // 1..512: try { // if it's not in the allowed range, throw an exception: if (Size < 1 || Size > 512) { throw new ArgumentOutOfRangeException(); } } catch { Size = 80; } // default the image url: //string imageUrl = "http://www.gravatar.com/avatar.php?"; // changes by Joe Audette 2008-08-13 string imageUrl = GravatarBaseUrl; if (!string.IsNullOrEmpty(Email)) { // build up image url, including MD5 hash for supplied email: MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); UTF8Encoding encoder = new UTF8Encoding(); MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider(); byte[] hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(Email)); StringBuilder sb = new StringBuilder(hashedBytes.Length * 2); for (int i = 0; i < hashedBytes.Length; i++) { sb.Append(hashedBytes[i].ToString("X2")); } // output parameters: //imageUrl += "gravatar_id=" + sb.ToString().ToLower(); //imageUrl += "&rating=" + MaxAllowedRating.ToString(); //imageUrl += "&size=" + Size.ToString(); // changes by Joe Audette 2008-08-13 imageUrl += sb.ToString().ToLower(); imageUrl += ".jpg?r=" + MaxAllowedRating.ToString(); imageUrl += "&s=" + Size.ToString(); } // output default parameter if specified if (!string.IsNullOrEmpty(DefaultImage)) { imageUrl += "&default=" + HttpUtility.UrlEncode(DefaultImage); } // if we need to output the site link: if (OutputGravatarSiteLink) { output.AddAttribute(HtmlTextWriterAttribute.Href, LinkUrl); output.AddAttribute(HtmlTextWriterAttribute.Title, LinkTitle); if (CssClass.Length > 0) { output.AddAttribute(HtmlTextWriterAttribute.Class, CssClass); } if (target.Length > 0) { output.AddAttribute(HtmlTextWriterAttribute.Target, target); } output.RenderBeginTag(HtmlTextWriterTag.A); } // output required attributes/img tag: output.AddAttribute(HtmlTextWriterAttribute.Width, Size.ToString()); output.AddAttribute(HtmlTextWriterAttribute.Height, Size.ToString()); output.AddAttribute(HtmlTextWriterAttribute.Src, imageUrl); output.AddAttribute(HtmlTextWriterAttribute.Alt, "Gravatar"); output.RenderBeginTag("img"); output.RenderEndTag(); // if we need to output the site link: if (OutputGravatarSiteLink) { output.RenderEndTag(); } }