CSS Sprite Generator
New v2.0
I decided to increment the major version of this tool as it implements new core features and actually brakes background compatibility with the previous command line commands.
So... what's new?
- 3 Bin Packing algorithms to choose from that optimize the sprite image size
- New and refactored command line commands to let you automate the generation process
- Support for inner folders
- Allow setting the output file names
Introduction
Image Sprites are a very good way to feed your application some images. Basically, it consists of combining a set of images in a larger one and then just caching it. When you need one of the small images, all you have to know are the coordinates of it on the large one. With this technique, you just need to load one image for the entire application and reuse it, instead of loading each image at a time.
Here, I'll be covering the implementation with CSS used on WebSites, but you can use this technique wherever you want.
Why This?
Sure you can Google it and find a lot of matches telling you how to use sprites and CSS but where's the "no pain" way of building them and their CSS? All of them will tell you to use Photoshop of any other photo editing tool but this will always take a lot of time. That's when this cute little application comes in. It will generate the sprite image and its CSS in no time, and you can add images later and regenerate it without having to worry about breaking your code!
Using the Code
On the package, you'll find a compiled version and its source code.
Just drop the SpriteCreator.exe file on the folder that has the images to be included on the sprite and run it. DONE!
Assumptions, Limitations and Functionality
- All image files on the folder will be added to the sprite no matter the size.
- All the images on the folder will be included on the sprite.
- Only jpg, jpeg, png & bmp extensions are supported.
- The name of the images is used on the CSS class name.
- The spaces on images file name will be replaced with -
- The result 3 files, one *.png (the sprite), *.css and *.html (the demo usage page) where * is the name you specified or a random GUID.
- CSS file includes a class that applies the background image to the element
- Images are ordered using 3 different algorithms (more about them bellow).
Bin Packing Algorithms
To optimize the distribution of the images on the sprite I used the code from here: Nuclex Game
The Bin packing algorithms are very well documented on their page.
All Bin Packing Algorithm credits must be given to these guys that saved me a lot of time by perfectly implementing these three good bin packing algorithms in C#. On my side I only removed the XNA dependencies and replaced them with pure System.Drawing objects.
Options
This is a console application so you can pass some customization arguments.
- /h, /?, /help :: Help
- /s :: Specify the images source directory path
- /d :: Destination dir path
- /D :: Same as /d but creates the directory if it doesn't exist
- /f :: Destination file name
- /F :: Same as /f but overrides the files if they already exist
- /cp :: CSS class name prefix
- /pl :: Specify the Bin Packaging Level (1, 2 (default) or 3)
Ex: SpriteCreator.exe /s Images /F SiteImages /cp "app-ui-"
History
- v1.0 - The very beginning
- v1.1 - Support for images of different sizes and small improvements.
- v2.0 - Arguments refactoring and Bin Packing Algorithm usage.