示例#1
0
 public static Image GetImageFromCache(string imageFileName, string email, int cacheDays,
                                       int imageSize, string imageCachePath, FallBackService fallBack)
 {
     try
     {
         if (cache == null)
         {
             cache = new DirectoryImageCache(imageCachePath); //or: new IsolatedStorageImageCache();
         }
         // If the user image is not cached yet, download it from gravatar and store it in the isolatedStorage
         if (!cache.FileIsCached(imageFileName) ||
             cache.FileIsExpired(imageFileName, cacheDays))
         {
             return(null);
         }
         if (cache.FileIsCached(imageFileName))
         {
             return(cache.LoadImageFromCache(imageFileName, null));
         }
     }
     catch (Exception ex)
     {
         //catch IO errors
         Trace.WriteLine(ex.Message);
     }
     return(null);
 }
示例#2
0
        public static void GetImageFromGravatar(string imageFileName, string email, int authorImageSize, FallBackService fallBack)
        {
            try
            {
                var baseUrl = String.Concat("http://www.gravatar.com/avatar/{0}?d=identicon&s=",
                                            authorImageSize, "&r=g");

                if (fallBack == FallBackService.Identicon)
                    baseUrl += "&d=identicon";
                if (fallBack == FallBackService.MonsterId)
                    baseUrl += "&d=monsterid";
                if (fallBack == FallBackService.Wavatar)
                    baseUrl += "&d=wavatar";

                //hash the email address
                var emailHash = MD5.CalcMD5(email.ToLower());

                //format our url to the Gravatar
                var imageUrl = String.Format(baseUrl, emailHash);

                var webClient = new WebClient { Proxy = WebRequest.DefaultWebProxy };
                webClient.Proxy.Credentials = CredentialCache.DefaultCredentials;

                var imageStream = webClient.OpenRead(imageUrl);

                cache.CacheImage(imageFileName, imageStream);
            }
            catch (Exception ex)
            {
                //catch IO errors
                Trace.WriteLine(ex.Message);
            }
        }
示例#3
0
        public static void LoadCachedImage(string imageFileName, string email, Bitmap defaultBitmap, int cacheDays,
                                             int imageSize, string imageCachePath, Action<Image> onChangedImage,
                                             FallBackService fallBack)
        {
            try
            {
                if (cache == null)
                    cache = new DirectoryImageCache(imageCachePath); //or: new IsolatedStorageImageCache();

                // If the user image is not cached yet, download it from gravatar and store it in the isolatedStorage
                if (!cache.FileIsCached(imageFileName) ||
                    cache.FileIsExpired(imageFileName, cacheDays))
                {
                    onChangedImage(defaultBitmap);

                    GetImageFromGravatar(imageFileName, email, imageSize, fallBack);
                }
                if (cache.FileIsCached(imageFileName))
                {
                    onChangedImage(cache.LoadImageFromCache(imageFileName,
                                                                 defaultBitmap));
                }
                else
                {
                    onChangedImage(defaultBitmap);
                }
            }
            catch (Exception ex)
            {
                //catch IO errors
                Trace.WriteLine(ex.Message);
            }
        }
示例#4
0
        /// <summary>
        ///   Update the Gravatar anytime an attribute is changed
        /// </summary>
        private void UpdateGravatar()
        {
            // resize our control (I'm not using AutoSize for a reason)
            Size = new Size(Settings.AuthorImageSize, Settings.AuthorImageSize);
            _gravatarImg.Size = new Size(Settings.AuthorImageSize, Settings.AuthorImageSize);

            if (!Settings.ShowAuthorGravatar || string.IsNullOrEmpty(Email))
            {
                RefreshImage(Resources.User);
                return;
            }

            FallBackService gravatarFallBack = FallBackService.Identicon;

            try
            {
                gravatarFallBack = (FallBackService)Enum.Parse(typeof(FallBackService), Settings.GravatarFallbackService);
            }
            catch
            {
                Settings.GravatarFallbackService = gravatarFallBack.ToString();
            }

            ThreadPool.QueueUserWorkItem(o =>
                                         GravatarService.LoadCachedImage(
                                             ImageFileName,
                                             Email,
                                             Resources.User,
                                             Settings.AuthorImageCacheDays,
                                             Settings.AuthorImageSize,
                                             Settings.GravatarCachePath,
                                             RefreshImage,
                                             gravatarFallBack));
        }
        public static void CacheImage(string imageFileName, string email, int imageSize,
                                      FallBackService fallBack)
        {
            try
            {
                if (cache == null)
                {
                    return;
                }

                if (IsValidEmail(email) && !cache.FileIsCached(imageFileName))
                {
                    //Lock added to make sure gravatar doesn't block this ip..
                    lock (gravatarServiceLock)
                    {
                        GetImageFromGravatar(imageFileName, email, imageSize, fallBack);
                    }
                }
            }
            catch (Exception ex)
            {
                //catch IO errors
                Trace.WriteLine(ex.Message);
            }
        }
示例#6
0
        public static void LoadCachedImage(string imageFileName, string email, Bitmap defaultBitmap, int cacheDays,
                                           int imageSize, string imageCachePath, Action <Image> onChangedImage,
                                           FallBackService fallBack)
        {
            try
            {
                if (cache == null)
                {
                    cache = new DirectoryImageCache(imageCachePath); //or: new IsolatedStorageImageCache();
                }
                // If the user image is not cached yet, download it from gravatar and store it in the isolatedStorage
                if (!cache.FileIsCached(imageFileName) ||
                    cache.FileIsExpired(imageFileName, cacheDays))
                {
                    onChangedImage(defaultBitmap);

                    GetImageFromGravatar(imageFileName, email, imageSize, fallBack);
                }
                if (cache.FileIsCached(imageFileName))
                {
                    onChangedImage(cache.LoadImageFromCache(imageFileName,
                                                            defaultBitmap));
                }
                else
                {
                    onChangedImage(defaultBitmap);
                }
            }
            catch (Exception ex)
            {
                //catch IO errors
                Trace.WriteLine(ex.Message);
            }
        }
示例#7
0
        public void CacheAvatar(string email)
        {
            FallBackService gravatarFallBack = FallBackService.Identicon;

            try
            {
                gravatarFallBack =
                    (FallBackService)Enum.Parse(typeof(FallBackService), Settings.GravatarFallbackService);
            }
            catch
            {
                Settings.GravatarFallbackService = gravatarFallBack.ToString();
            }
            GravatarService.CacheImage(email + ".png", email, Settings.AuthorImageSize,
                                       gravatarFallBack);
        }
示例#8
0
        public static void LoadCachedImage(string imageFileName, string email, Bitmap defaultBitmap, int cacheDays,
                                           int imageSize, string imageCachePath, Action <Image> onChangedImage,
                                           FallBackService fallBack)
        {
            Image image = GetImageFromCache(imageFileName, email, cacheDays, imageSize, imageCachePath, fallBack);

            try
            {
                if (image == null)
                {
                    onChangedImage(defaultBitmap);

                    //Lock added to make sure gravatar doesn't block this ip..
                    lock (gravatarServiceLock)
                    {
                        if (GetImageFromCache(imageFileName, email, cacheDays, imageSize, imageCachePath, fallBack) == null)
                        {
                            GetImageFromGravatar(imageFileName, email, imageSize, fallBack);
                        }
                    }
                }
                image = GetImageFromCache(imageFileName, email, cacheDays, imageSize, imageCachePath, fallBack);
                if (image != null)
                {
                    onChangedImage(image);
                }
                else
                {
                    onChangedImage(defaultBitmap);
                }
            }
            catch (Exception ex)
            {
                //catch IO errors
                Trace.WriteLine(ex.Message);
            }
        }
        public static void GetImageFromGravatar(string imageFileName, string email, int authorImageSize, FallBackService fallBack)
        {
            try
            {
                var imageUrl = BuildGravatarUrl(email,
                    authorImageSize,
                    false,
                    Rating.G,
                    fallBack);

                var webClient = new WebClient { Proxy = WebRequest.DefaultWebProxy };
                webClient.Proxy.Credentials = CredentialCache.DefaultCredentials;

                var imageStream = webClient.OpenRead(imageUrl);

                cache.CacheImage(imageFileName, imageStream);
            }
            catch (Exception ex)
            {
                //catch IO errors
                Trace.WriteLine(ex.Message);
            }
        }
示例#10
0
        /// <summary>
        /// Builds a <see cref="System.Uri"/> corresponding to a given email address.
        /// </summary>
        /// <param name="email">The email address for which to build the <see cref="System.Uri"/>.</param>
        /// <param name="size">The size of the image to request.  The default is 32.</param>
        /// <param name="useHttps">Indicates whether or not the request should be performed over Secure HTTP.</param>
        /// <param name="rating">The mazimum rating of the returned image.</param>
        /// <param name="fallBack">The Gravatar service that will be used for fall-back.</param>
        /// <returns>The constructed <see cref="System.Uri"/>.</returns>
        private static Uri BuildGravatarUrl(string email, int size, bool useHttps, Rating rating, FallBackService fallBack)
        {
            var builder = new UriBuilder("http://www.gravatar.com/avatar/");

            if (useHttps)
            {
                builder.Scheme = "https";
            }

            builder.Path += HashEmail(email);

            string d;
            if (!fallBackStrings.TryGetValue(fallBack, out d))
            {
                d = "404";
            }

            var query = string.Format("s={0}&r={1}&d={2}",
                size.ToString(),
                rating.ToString().ToLowerInvariant(),
                d);

            builder.Query = query;

            return builder.Uri;
        }
示例#11
0
        public static void LoadCachedImage(string imageFileName, string email, Bitmap defaultBitmap, int cacheDays,
                                             int imageSize, string imageCachePath, Action<Image> onChangedImage,
                                             FallBackService fallBack)
        {
            Image image = GetImageFromCache(imageFileName, email, cacheDays, imageSize, imageCachePath, fallBack);

            try
            {
                if (image == null)
                {
                    onChangedImage(defaultBitmap);

                    //Lock added to make sure gravatar doesn't block this ip..
                    lock (gravatarServiceLock)
                    {
                        if (GetImageFromCache(imageFileName, email, cacheDays, imageSize, imageCachePath, fallBack) == null)
                            GetImageFromGravatar(imageFileName, email, imageSize, fallBack);
                    }
                }
                image = GetImageFromCache(imageFileName, email, cacheDays, imageSize, imageCachePath, fallBack);
                if (image != null)
                {
                    onChangedImage(image);
                }
                else
                {
                    onChangedImage(defaultBitmap);
                }
            }
            catch (Exception ex)
            {
                //catch IO errors
                Trace.WriteLine(ex.Message);
            }
        }
示例#12
0
        public static void CacheImage(string imageFileName, string email, int imageSize, 
                                        FallBackService fallBack)
        {
            try
            {
                if (cache == null)
                    return;

                if (IsValidEmail(email) && !cache.FileIsCached(imageFileName))
                {
                    //Lock added to make sure gravatar doesn't block this ip..
                    lock (gravatarServiceLock)
                    {
                        GetImageFromGravatar(imageFileName, email, imageSize, fallBack);
                    }
                }
            }
            catch (Exception ex)
            {
                //catch IO errors
                Trace.WriteLine(ex.Message);
            }
        }
示例#13
0
        public static Image GetImageFromCache(string imageFileName, string email, int cacheDays,
                                             int imageSize, string imageCachePath, FallBackService fallBack)
        {
            try
            {
                if (cache == null)
                    cache = new DirectoryImageCache(imageCachePath); //or: new IsolatedStorageImageCache();

                // If the user image is not cached yet, download it from gravatar and store it in the isolatedStorage
                if (!cache.FileIsCached(imageFileName) ||
                    cache.FileIsExpired(imageFileName, cacheDays))
                {
                    return null;
                }
                if (cache.FileIsCached(imageFileName))
                {
                    return cache.LoadImageFromCache(imageFileName, null);
                }

            }
            catch (Exception ex)
            {
                //catch IO errors
                Trace.WriteLine(ex.Message);
            }
            return null;
        }
示例#14
0
        public static void GetImageFromGravatar(string imageFileName, string email, int authorImageSize, FallBackService fallBack)
        {
            try
            {
                var imageUrl = BuildGravatarUrl(email,
                                                authorImageSize,
                                                false,
                                                Rating.G,
                                                fallBack);

                var webClient = new WebClient {
                    Proxy = WebRequest.DefaultWebProxy
                };
                webClient.Proxy.Credentials = CredentialCache.DefaultCredentials;

                var imageStream = webClient.OpenRead(imageUrl);

                cache.CacheImage(imageFileName, imageStream);
            }
            catch (Exception ex)
            {
                //catch IO errors
                Trace.WriteLine(ex.Message);
            }
        }
示例#15
0
        /// <summary>
        /// Builds a <see cref="System.Uri"/> corresponding to a given email address.
        /// </summary>
        /// <param name="email">The email address for which to build the <see cref="System.Uri"/>.</param>
        /// <param name="size">The size of the image to request.  The default is 32.</param>
        /// <param name="useHttps">Indicates whether or not the request should be performed over Secure HTTP.</param>
        /// <param name="rating">The mazimum rating of the returned image.</param>
        /// <param name="fallBack">The Gravatar service that will be used for fall-back.</param>
        /// <returns>The constructed <see cref="System.Uri"/>.</returns>
        private static Uri BuildGravatarUrl(string email, int size, bool useHttps, Rating rating, FallBackService fallBack)
        {
            var builder = new UriBuilder("http://www.gravatar.com/avatar/");

            if (useHttps)
            {
                builder.Scheme = "https";
            }

            builder.Path += HashEmail(email);

            string d;

            if (!fallBackStrings.TryGetValue(fallBack, out d))
            {
                d = "404";
            }

            var query = string.Format("s={0}&r={1}&d={2}",
                                      size,
                                      rating.ToString().ToLowerInvariant(),
                                      d);

            builder.Query = query;

            return(builder.Uri);
        }
示例#16
0
        public static void GetImageFromGravatar(string imageFileName, string email, int authorImageSize, FallBackService fallBack)
        {
            try
            {
                var baseUrl = String.Concat("http://www.gravatar.com/avatar/{0}?d=identicon&s=",
                                            authorImageSize, "&r=g");

                if (fallBack == FallBackService.Identicon)
                {
                    baseUrl += "&d=identicon";
                }
                if (fallBack == FallBackService.MonsterId)
                {
                    baseUrl += "&d=monsterid";
                }
                if (fallBack == FallBackService.Wavatar)
                {
                    baseUrl += "&d=wavatar";
                }

                //hash the email address
                var emailHash = MD5.CalcMD5(email.ToLower());

                //format our url to the Gravatar
                var imageUrl = String.Format(baseUrl, emailHash);

                var webClient = new WebClient {
                    Proxy = WebRequest.DefaultWebProxy
                };
                webClient.Proxy.Credentials = CredentialCache.DefaultCredentials;

                var imageStream = webClient.OpenRead(imageUrl);

                cache.CacheImage(imageFileName, imageStream);
            }
            catch (Exception ex)
            {
                //catch IO errors
                Trace.WriteLine(ex.Message);
            }
        }