Click here to Skip to main content
15,881,757 members
Articles / Desktop Programming / MFC

Image Sprites and CSS Classes creator

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
1 Feb 2011CPOL2 min read 18.2K   9   2
Image Sprites and CSS Classes creator

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 or 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 and Limitations

  1. All images on the folder have the same size, e.g.: 16x16, 32x32, ...
  2. The size of the images will be the size of the first image loaded into the sprite.
  3. The big sprite image will always be square, having the minimum size needed to fit all images inside.
  4. All the images on the folder will be included on the sprite.
  5. Only jpg, jpeg, png & bmp extensions are supported.
  6. The name of the images is used on the CSS class name.
  7. The spaces on images file name will be replaced with -
  8. The result 2 files, one *.png and *.css where * is a generated GUID

Options

This is a console application so you can pass some customization arguments.

  1. /h :: Help
  2. /source :: Specify the images source directory path
  3. /destination :: Destination dir path
  4. /classprefix :: CSS class name prefix
Example: SpriteCreator.exe /source shadow /classprefix app-ui-

This article was originally posted at http://www.instanceofanobject.com/feeds/posts/default

License

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


Written By
Architect
Switzerland Switzerland
Senior IT Consultant working in Switzerland as Senior Software Engineer.

Find more at on my blog.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Dalek Dave16-Mar-11 6:49
professionalDalek Dave16-Mar-11 6:49 
Short, sweet and useful.
Generalnice tool Pin
aldo hexosa1-Feb-11 17:47
professionalaldo hexosa1-Feb-11 17:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.