Paint.NET can handle PSD files. With the DLL PhotoShop.dll , I was able to manipulate the PSD file:
using PhotoshopFile;
One problem is that transparent layers do not exactly fill the space I need, so to get around this and have dynamic sizes I've 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
)
);
});
In this way I can map layers to CSS, regardless of whether the file contains different icon sizes.
This project on Github .