The Paint.NET can handle PSD files. With the DLL Photoshop.dll, I managed to manipulate the PSD file:
using PhotoshopFile;
One problem is that transparent layers don’t exactly take up the space I need, so to get around it and have dynamic sizes I inserted the icon size into the layer name (32x32-Nome da layer
). This way I can easily work with different sizes in the same file.
PsdFile ps = new PsdFile();
ps.Load(file);
// ordeno as layers pela posição
ps.Layers.OrderBy(l => l.Rect.X).ThenBy(l => l.Rect.Y)
.ToList()
.ForEach(l =>
{
// Separo o nome da layer to tamanho
var nameTokens = l.Name.Split(new char[] { '-' }, 2);
// o primeiro fragmento contém "largura x altura"
var sizeTokens = nameTokens[0].Split('x');
var size = new Size(Convert.ToInt32(sizeTokens[0]), Convert.ToInt32(sizeTokens[1]));
var name = nameTokens[1]; // o segundo fragmento contém o nome
var x = l.Rect.X - Math.Round((size.Width - l.Rect.Width) / 2.0, MidpointRounding.AwayFromZero);
var y = l.Rect.Y - Math.Round((size.Height - l.Rect.Height) / 2.0, MidpointRounding.AwayFromZero);
// gero o CSS para saida, output é uma StringBuilder
output.AppendText(
String.Format(".{0} {{ width: {1}px; height: {2}px; background-position: {3}px {4}px; }}{5}",
name,
size.Width,
size.Height,
x,
y,
Environment.NewLine
)
);
});
This way I can map layers to CSS, no matter if the file contains different icon sizes.
This project on Github.
This should help: http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/
– bfavaretto
I’m going to do a "cross-post" of the OS, it would probably be better if I extracted the information and translated, but as I’m running out of time now, I’ll post the link: http://stackoverflow.com/questions/499597/how-to-extract-layers-from-a-photoshop-file-c-sharp if this violates the rules, just comment that I will remove.
– Diego C Nascimento
Buddy, you can either use Photoshop itself to do this (which I don’t recommend much unless you’re desperate) or use the PS ruler and measure the spaces between the sprites!
– Vinicius Mesel