Click here to Skip to main content
15,893,594 members
Articles / Web Development / HTML

Generate CSS sprites and thumbnail images on the fly in ASP.NET sites

Rate me:
Please Sign up or sign in to vote.
4.82/5 (40 votes)
9 Jun 2012CPOL64 min read 117.6K   2.8K   85  
Reduces page load times of ASP.NET web sites by combining page images and CSS background images into CSS sprites. Compresses and physically resizes images to make thumbnails. Caters for repeating background images.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace CssSpriteGenerator
{
    /// <summary>
    /// Used to store a collection of image attributes, such as src and style.
    /// The key is the name of the attribute. *** That name must be all lowercase. ***
    /// The value is the value of the attribute.
    /// </summary>
    public class ImageAttributeDictionary : Dictionary<string, string>
    {
        /// <summary>
        /// Returns a string with attribute-value pairs, such that the string can be dropped in an html tag.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return ToString(null, null);
        }

        /// <summary>
        /// Version of ToString
        /// </summary>
        /// <param name="excludes">
        /// If this is null, no filtering. If not null, an attribute will not be added to output if it is excludes.
        /// </param>
        /// <param name="copiedImgAttributesRegex">
        /// If this is null, no filtering. If not null, only attributes that match this regular expression make it to the output.
        /// </param>
        /// <returns></returns>
        public string ToString(List<string> excludes, Regex copiedImgAttributesRegex)
        {
            StringBuilder resultSb = new StringBuilder();

            foreach (KeyValuePair<String, String> imageAttribute in this)
            {
                string attributeName = imageAttribute.Key.ToLower();
                string attributeValue = imageAttribute.Value;

                if ((copiedImgAttributesRegex == null) ||
                    (copiedImgAttributesRegex.IsMatch(attributeName)))
                {
                    if ((excludes == null) || (!excludes.Contains(attributeName)))
                    {
                        resultSb.AppendFormat(@" {0}=""{1}""", attributeName, attributeValue);
                    }
                }
            }

            return resultSb.ToString();
        }

        /// <summary>
        /// Returns the value of the given attribute.
        /// If the attribute doesn't exist, returns empty string.
        /// </summary>
        /// <param name="attributeName">
        /// Make sure this is all lowercase!
        /// </param>
        /// <returns></returns>
        public string AttributeValue(string attributeName)
        {
            string attributeValue;
            if (TryGetValue(attributeName, out attributeValue))
            {
                return attributeValue;
            }

            return "";
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Australia Australia
Twitter: @MattPerdeck
LinkedIn: au.linkedin.com/in/mattperdeck
Current project: JSNLog JavaScript Logging Package

Matt has over 9 years .NET and SQL Server development experience. Before getting into .Net, he worked on a number of systems, ranging from the largest ATM network in The Netherlands to embedded software in advanced Wide Area Networks and the largest ticketing web site in Australia. He has lived and worked in Australia, The Netherlands, Slovakia and Thailand.

He is the author of the book ASP.NET Performance Secrets (www.amazon.com/ASP-NET-Site-Performance-Secrets-Perdeck/dp/1849690685) in which he shows in clear and practical terms how to quickly find the biggest bottlenecks holding back the performance of your web site, and how to then remove those bottlenecks. The book deals with all environments affecting a web site - the web server, the database server and the browser.

Matt currently lives in Sydney, Australia. He recently worked at Readify and the global professional services company PwC. He now works at SP Health, a global provider of weight loss web sites such at CSIRO's TotalWellBeingDiet.com and BiggestLoserClub.com.

Comments and Discussions