Beispiel #1
0
 public void TestCropValues()
 {
     ResizeSettings s = new ResizeSettings();
     s.CropBottomRight = new System.Drawing.PointF(50, 50);
     s.CropTopLeft = new System.Drawing.PointF(0, 0);
     Assert.AreEqual<string>( "?crop=0,0,50,50", s.ToString());
     s.CropMode = CropMode.Auto;
     Assert.AreEqual<string>("?crop=auto", s.ToString());
 }
        public void TestCropValues()
        {
            ResizeSettings s = new ResizeSettings();

            s.CropBottomRight = new System.Drawing.PointF(50, 50);
            s.CropTopLeft     = new System.Drawing.PointF(0, 0);
            Assert.AreEqual <string>("?crop=0,0,50,50", s.ToString());
            s.CropMode = CropMode.Auto;
            Assert.AreEqual <string>("?crop=auto", s.ToString());
        }
Beispiel #3
0
        /// <summary>
        /// Loads or caches a bitmap, using asp.net's cache (when available)
        /// </summary>
        /// <param name="query"></param>
        /// <param name="virtualPath"></param>
        /// <param name="onlyLoadIfCacheExists">Whether to load the image when
        /// no cache is available.  Pass <c>true</c> for pre-fetching, and
        /// <c>false</c> if the image is needed immediately.</param>
        /// <returns>Returns the Bitmap.  If no cache is available, and
        /// <c>onlyLoadIfCacheExists</c> is <c>true</c>, returns <c>null</c>
        /// rather than loading the Bitmap.</returns>
        public Bitmap GetMemCachedBitmap(string virtualPath, ResizeSettings query, bool onlyLoadIfCacheExists = false)
        {
            //If not ASP.NET, don't cache.
            if (HttpContext.Current == null)
            {
                return(onlyLoadIfCacheExists ? null : c.CurrentImageBuilder.LoadImage(virtualPath, query));
            }

            string key = virtualPath.ToLowerInvariant() + query.ToString();
            Bitmap b   = HttpContext.Current.Cache[key] as Bitmap;

            if (b != null)
            {
                return(b);
            }
            try
            {
                b = c.CurrentImageBuilder.LoadImage(virtualPath, query);
            }
            catch (FileNotFoundException fe)
            {
                throw new ImageProcessingException(500, "Failed to located watermark " + virtualPath, "Failed to located a watermarking file", fe);
            }
            //Query VPPs for cache dependency. TODO: Add support for IVirtualImageProviders to customize cache dependencies.
            CacheDependency cd = null;

            if (HostingEnvironment.VirtualPathProvider != null)
            {
                cd = HostingEnvironment.VirtualPathProvider.GetCacheDependency(virtualPath, new string[] { }, DateTime.UtcNow);
            }

            HttpContext.Current.Cache.Insert(key, b, cd);
            return(b);
        }
Beispiel #4
0
 private string CreateSettingsHash(ResizeSettings settings)
 {
     if (settings.Count == 2 && settings.MaxWidth > 0 && settings.MaxWidth == settings.MaxHeight)
     {
         return(settings.MaxWidth.ToString());
     }
     return(settings.ToString().Hash(Encoding.ASCII));
 }
        public string GetImageCachePath(string url, ResizeSettings settings)
        {
            string imageDir = Path.GetDirectoryName(url);

            if (imageDir == null)
            {
                imageDir = string.Empty;
            }
            string hasedProperties = CreateMd5Hash(settings.ToString());
            string cachedFileName  = string.Format("{0}/{1}-{2}.{3}",
                                                   GetFolderHash(imageDir),
                                                   Path.GetFileNameWithoutExtension(url),
                                                   hasedProperties,
                                                   GetCleanFileExtension(url));
            string cachedImagePath = string.Concat(Consts.CacheFolderPath, "/", cachedFileName.TrimStart('/', '\\'));

            return(cachedImagePath);
        }
 public string GetImageCachePath(string url, ResizeSettings settings)
 {
     string imageDir = Path.GetDirectoryName(url);
     if (imageDir == null)
     {
         imageDir = string.Empty;
     }
     string hasedProperties = CreateMd5Hash(settings.ToString());
     string cachedFileName = string.Format("{0}/{1}-{2}.{3}",
                                           GetFolderHash(imageDir),
                                           Path.GetFileNameWithoutExtension(url),
                                           hasedProperties,
                                           GetCleanFileExtension(url));
     string cachedImagePath = string.Concat(Consts.CacheFolderPath, "/", cachedFileName.TrimStart('/', '\\'));
     return cachedImagePath;
 }
Beispiel #7
0
        /// <summary>
        /// Loads or caches a bitmap, using asp.net's cache (when available)
        /// </summary>
        /// <param name="query"></param>
        /// <param name="virtualPath"></param>
        /// <param name="onlyLoadIfCacheExists">Whether to load the image when
        /// no cache is available.  Pass <c>true</c> for pre-fetching, and
        /// <c>false</c> if the image is needed immediately.</param>
        /// <returns>Returns the Bitmap.  If no cache is available, and
        /// <c>onlyLoadIfCacheExists</c> is <c>true</c>, returns <c>null</c>
        /// rather than loading the Bitmap.</returns>
        public Bitmap GetMemCachedBitmap(string virtualPath, ResizeSettings query, bool onlyLoadIfCacheExists = false)
        {
            //If not ASP.NET, don't cache.
            if (HttpContext.Current == null) return onlyLoadIfCacheExists ? null : c.CurrentImageBuilder.LoadImage(virtualPath, query);

            string key = virtualPath.ToLowerInvariant() + query.ToString();
            Bitmap b = HttpContext.Current.Cache[key] as Bitmap;
            if (b != null) return b;
            try
            {
                b = c.CurrentImageBuilder.LoadImage(virtualPath, query);
            }
            catch (FileNotFoundException fe)
            {
                throw new ImageProcessingException(500, "Failed to located watermark " + virtualPath, "Failed to located a watermarking file", fe);
            }
            //Query VPPs for cache dependency. TODO: Add support for IVirtualImageProviders to customize cache dependencies.
            CacheDependency cd = null;
            if (HostingEnvironment.VirtualPathProvider != null) cd = HostingEnvironment.VirtualPathProvider.GetCacheDependency(virtualPath, new string[] { }, DateTime.UtcNow);

            HttpContext.Current.Cache.Insert(key, b, cd);
            return b;
        }
        private string CreateSettingsHash(ResizeSettings settings)
        {
            if (settings.Count == 2 && settings.MaxWidth > 0 && settings.MaxWidth == settings.MaxHeight)
            {
                return settings.MaxWidth.ToString();
            }
			return settings.ToString().Hash(Encoding.ASCII);
        }
Beispiel #9
0
        /// <summary>
        /// Loads a bitmap, cached using asp.net's cache
        /// </summary>
        /// <param name="query"></param>
        /// <param name="virtualPath"></param>
        /// <returns></returns>
        public Bitmap GetMemCachedBitmap(string virtualPath, ResizeSettings query)
        {
            //If not ASP.NET, don't cache.
            if (HttpContext.Current == null) return c.CurrentImageBuilder.LoadImage(virtualPath, query);

            string key = virtualPath.ToLowerInvariant() + query.ToString();
            Bitmap b = HttpContext.Current.Cache[key] as Bitmap;
            if (b != null) return b;

            b = c.CurrentImageBuilder.LoadImage(virtualPath, query);
            //Query VPPs for cache dependency. TODO: Add support for IVirtualImageProviders to customize cache dependencies.
            CacheDependency cd = null;
            if (HostingEnvironment.VirtualPathProvider != null) cd = HostingEnvironment.VirtualPathProvider.GetCacheDependency(virtualPath, new string[] { }, DateTime.UtcNow);

            HttpContext.Current.Cache.Insert(key, b, cd);
            return b;
        }